Search code examples
c++opencvfeature-extractionfeature-detection

Want to detect features and match the feature in 2 different frames


I'm currently using OpenCV 3.4.0, c++ in QT creator. I've tried sample code in this page https://docs.opencv.org/2.4/doc/tutorials/features2d/feature_description/feature_description.html

int minHessian = 400;
cv::xfeatures2d::SurfFeatureDetector detector(minHessian);
vector<KeyPoint> keypoints1, keypoints2;
detector.detect(img1, keypoints1);
detector.detect(img2, keypoints2);

// computing descriptors
cv::xfeatures2d::SurfDescriptorExtractor extractor;
Mat descriptors1, descriptors2;
extractor.compute(img1, keypoints1, descriptors1);
extractor.compute(img2, keypoints2, descriptors2);

// matching descriptors
BFMatcher matcher(NORM_L2);
vector<DMatch> matches;
matcher.match(descriptors1, descriptors2, matches);

// drawing the results
namedWindow("matches", 1);
Mat img_matches;
drawMatches(img1, keypoints1, img2, keypoints2, matches, img_matches);
imshow("matches", img_matches);
waitKey(0);

but the code kept returning error

no matching function for call to 'cv::xfeatures2d::SURF::SURF(int&)(2nd line)

cannot declare variable 'detector' to be of abstract type 'cv::xfeatures2d::SURF'(2nd line)

cannot declare variable 'extractor' to be of astract type 'cv::xfeatures2d::SURF'(7th line)

I have imported all the necessary modules I think including xfeatures2d

What is the problem?

And are there any other sample codes that I can try?


Solution

  • Your coding reflects an older version of OpenCV which is not compatible with OpenCV 3.4.0. You can try like so. It is better to convert the template image to a grayscale image for better matching:

    cv::Ptr<Feature2D> detector = cv::xfeatures2d::SurfFeatureDetector::create();
    detector->detect(img1, keypoints1);
    
    cv::Ptr<DescriptorExtractor> extractor = cv::xfeatures2d::SurfFeatureDetector::create();
    extractor->compute(img1, keypoints1, descriptors1);