Search code examples
c++imageobjectopencvdetection

Why can't I seem to get any matches with knnMatch using OpenCV with C++?


First off I will say that so far I have based a large chunk of this using this very interesting post on the subject.

In the mentioned post, the example uses a webcam and a UI window for the purposes of seeing the output in real time. I am simply just trying to use similar code to compare two images, (as appose to one image and lots of frames), but have ran into some problems.

So I have two images (cv::Mat objects)

Mat object_1 = imread( "image1.jpg", CV_LOAD_IMAGE_GRAYSCALE );
Mat object_2 = imread( "image2.jpg", CV_LOAD_IMAGE_GRAYSCALE );

The following code isn't great, but this is the general idea:

int minHessian = 500;

SurfFeatureDetector detector( minHessian );
std::vector<KeyPoint> kp_object;

SurfDescriptorExtractor extractor;
Mat des_object;

extractor.compute( object_1, kp_object, des_object );

FlannBasedMatcher matcher;

std::vector<Point2f> obj_corners(4);

//Get the corners from the object
obj_corners[0] = cvPoint(0,0);
obj_corners[1] = cvPoint( object_1.cols, 0 );
obj_corners[2] = cvPoint( object_1.cols, object_1.rows );
obj_corners[3] = cvPoint( 0, object_1.rows );

Mat des_image, img_matches;
std::vector<KeyPoint> kp_image;
std::vector<vector<DMatch > > matches;
std::vector<DMatch > good_matches;
std::vector<Point2f> obj;
std::vector<Point2f> scene;
std::vector<Point2f> scene_corners(4);
Mat H;

detector.detect( object_2, kp_image );
extractor.compute( object_2, kp_image, des_image );

matcher.knnMatch(des_object, des_image, matches, 2);

for(int i = 0; i < min(des_image.rows-1,(int) matches.size()); i++) //THIS LOOP IS SENSITIVE TO SEGFAULTS
{
    if((matches[i][0].distance < 0.6*(matches[i][1].distance)) && ((int) matches[i].size()<=2 && (int) matches[i].size()>0))
    {
        good_matches.push_back(matches[i][0]);
    }
}

The issue here is that because matches.size() is equal to 0, it is not getting into the loop at all.

My question is, (even if both original images are the same) why are there no matches?


Solution

  • You need to detect keypoints in object_1 with detector.detect(object_1, kp_image );

    And after that you can call extractor.compute( object_1, kp_object, des_object ); as seen HERE