I am using OpenCV2.2 on Windows 7. What I'm trying to do is to detect a specific object in another image using this code:
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;
}
int minHessian = 400;
SurfFeatureDetector detector( minHessian );
std::vector<KeyPoint> keypoints1, keypoints2;
detector.detect(image1,keypoints1);
detector.detect(image2,keypoints2);
SurfDescriptorExtractor extractor;
Mat descriptors1, descriptors2;
extractor.compute(image1,keypoints1,descriptors1);
extractor.compute(image2,keypoints2,descriptors2);
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;
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 errors. My problem is that in some (and not all) cases where execution jumps to a line
matcher.knnMatch(descriptors1, descriptors2, matches, 2);
cmd stops and returns a message like: "Assertion Failed ( dataset.Type() == CvType(T)::type()) in an unknown function, etc. etc., ending before the line flann.hpp 105"
This happens when there is no similarity between the images (and not for all cases), although the descriptors (necessary for matching) are correctly extracted from both images. The code works fine if I use BruteForce.
I also tried the code from OpenCV:
http://opencv.itseez.com/doc/tutorials/features2d/feature_homography/feature_homography.html
. , , Opencv.
std::vector< DMatch > matches;
matcher.match( descriptors_object, descriptors_scene, matches );
( , OpenCV flann.h assertion Error, , , ), . -, , ?