I am using OpenCV2.2 in Windows 7. What I am trying to do is to detect a defined object in another image using this code:
// Read the two image files
Mat image1 = imread( argv[1], CV_LOAD_IMAGE_GRAYSCALE );
Mat image2 = imread( argv[2], CV_LOAD_IMAGE_GRAYSCALE );
Mat finalImage = imread(argv[2]);
if( !image1.data || !image2.data ) {
std::cout << " --(!) Error reading images " << std::endl;
return -10;
}
//Construct the SURF Detector
int minHessian = 400;
SurfFeatureDetector detector( minHessian );
//Extract the keypoints for each image
std::vector<KeyPoint> keypoints1, keypoints2;
detector.detect(image1,keypoints1);
detector.detect(image2,keypoints2);
//Calculate descriptors (feature vectors)
SurfDescriptorExtractor extractor;
Mat descriptors1, descriptors2;
extractor.compute(image1,keypoints1,descriptors1);
extractor.compute(image2,keypoints2,descriptors2);
//Define the Matcher
FlannBasedMatcher matcher;
std::cout << "matcher constructed!" << std::endl;
std::vector<vector<DMatch >> matches;
matcher.knnMatch(descriptors1, descriptors2, matches, 2);
std::cout << "matches: " << matches.size() << std::endl;
std::vector<DMatch > good_matches;
//THIS LOOP IS SENSITIVE TO SEGFAULTS
for (int i = 0; i < min(descriptors2.rows-1,(int) matches.size()); i++)
{
if((matches[i][0].distance < 0.8*(matches[i][1].distance)) && ((int) matches[i].size()<=2 && (int) matches[i].size()>0))
{
good_matches.push_back(matches[i][0]);
}
}
std::cout << "good_matches: " << good_matches.size() << std::endl;
VS2010 builds and compiles the code without error. My problem is that in some (and not all) cases, when the execution goes to the line
matcher.knnMatch(descriptors1, descriptors2, matches, 2);
the cmd stops and returns a message like: "Assertion Failed (dataset.Type() == CvType(T)::type()) in unknown function etc. etc. ending to flann.hpp line 105"
This is taking place when there is no similarity between images (and not for all cases), although descriptors (needed for matching) are extracted correctly from both images. The code works fine if I use the BruteForce matcher.
I have also tried the code from OpenCV:
http://opencv.itseez.com/doc/tutorials/features2d/feature_homography/feature_homography.html
and had the same issue. The execution fails even if I use the simple matcher as in the Opencv sample.
std::vector< DMatch > matches;
matcher.match( descriptors_object, descriptors_scene, matches );
I searched for solution (a similar question found at OpenCV flann.h assertion Error but unfortunately no answer) but I didn't find anything useful. Is there anyone who know how to tackle this problem?