将图像与参考图像OpenCV进行比较

时间:2017-04-06 03:41:55

标签: image opencv matching

我一直致力于基本的手势控制算法。我有一组参考图像(命令)和样本图像(由用户绘制)。我想比较样本图像和所有参考图像,看看用户试图输入什么命令(假设他们只绘制/尝试绘制提供的手势)。

但是,我无法生成具体的匹配算法。我已经尝试过OpenCV Library中的matchShapes和相关函数。到目前为止,只有相关函数提供了不错的结果。

这是我的代码

str.split(/[[:punct:] ]+/).drop(1)

到目前为止,我尝试过组合:Square,Triangle和Circle。 以下是我对这些组合的结果 变异= 0表示100%匹配,相关性= 1表示100%匹配。

#include<iostream>
#include<opencv2/highgui.hpp>
#include<opencv2/imgproc.hpp>
#include<opencv2/core.hpp>

using namespace std;
using namespace cv;

void cannyThreshold(Mat* src, Mat* detect)
{
    blur(*src, *detect, Size(3, 3));
    Canny(*detect, *detect, 5, 15, 3);
}

double correlation(cv::Mat &image_1, cv::Mat &image_2)
{
    // convert data-type to "float"
    cv::Mat im_float_1;
    image_1.convertTo(im_float_1, CV_32F);
    cv::Mat im_float_2;
    image_2.convertTo(im_float_2, CV_32F);
    int n_pixels = im_float_1.rows * im_float_1.cols;
    // Compute mean and standard deviation of both images
    cv::Scalar im1_Mean, im1_Std, im2_Mean, im2_Std;
    meanStdDev(im_float_1, im1_Mean, im1_Std);
    meanStdDev(im_float_2, im2_Mean, im2_Std);
    assert(im_float_1.size() == im_float_2.size());
    // Compute covariance and correlation coefficient
    double covar = (im_float_1 - im1_Mean).dot(im_float_2 - im2_Mean) / n_pixels;
    //cout << "correl function conversion \n";
    double correl = covar / (im1_Std[0] * im2_Std[0]);
    return correl;
}

int main()
{
    Mat src_a, src_b;
    src_a = imread("../data/ref_sq.png", 0);
    src_b = imread("../data/drawn_sq.png", 0);
    assert( !(src_a.empty() || src_b.empty() ) );
    namedWindow("ref", WINDOW_AUTOSIZE);
    namedWindow("drawn", WINDOW_AUTOSIZE);

    Mat matA, matB;
    Rect boundRectA, boundRectB;
    cannyThreshold(&src_a, &matA);
    cannyThreshold(&src_b, &matB);

    //draw rectangles around the gesture (seperate it from black bkg)
    boundRectA = boundingRect(matA);
    boundRectB = boundingRect(matB);
    rectangle(matA, boundRectA, Scalar(255, 0, 0));
    rectangle(matB, boundRectB, Scalar(255, 0, 0));
    Mat cropA, cropB;

    //resize the gesture images to same size 
    cropA = matA(boundRectA);
    cropB = matB(boundRectB);
    resize(cropA, matA, Size(300, 300));
    resize(cropB, matB, Size(300, 300));
    imshow("ref", matA);
    imshow("drawn", matB);
    double d = correlation(matA, matB);
    float variance = matchShapes(matA, matB, CV_TM_CCORR, 0.0);
    cout << "Variance = " << variance << "\n";
    cout << "Correlation is : " << d << "\n";
    waitKey(0);
    return 0;
}

所以现在我想知道,是否还有其他方法来匹配图像我应该尝试?任何帮助表示赞赏。 drawn square by user

reference square gesture enter image description here

0 个答案:

没有答案