Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

@file SURF_FlannMatcher and OpenCV 3.0.0 do not link

Hi I am relatively new to OpenCV. I program C++ in Vis Studio 2013 and use Cuda 6.5. I found FlannMatcher algorithm as presented on OpenCV as a good tool for parts of what I need to do. It took me 3 days to find out how to correct the program found on opencv.org so I could compile it under 3.0.0. Thanks to all of you who contribute with good answers to many peoples questions. Why isn't this good example updated? Now I can compile it, but the linker do not work. One 'specialist' advised to recompile opencv using the additions from opencv_contrib-master. I started this process going through the tutorial on opencv.org. I have come to the part where I shall install Numpy under Python. According to opencv.org I will need this to be able to read the documentation. It crashes all the time and I receive many different error messages that I find many others do as well.

Is there a simpler way to recompile opencv so the new Flannmatcher can link? or do anyone have the binaries needed so I do not need to recompile myself? I would appreciate all the help I can get here!

The changes in the program is that extractor and detector have become pointers and minHessian is double.

Ptr<SURF> detector = SURF::create(minHessian);
    std::vector<KeyPoint> keypts;
    Mat desc;
    //detector->detectAndCompute(img_1, noArray(), keypts, desc);

    std::vector<KeyPoint> keypoints_1, keypoints_2;
    detector->detectAndCompute(img_1, noArray(), keypoints_1, desc);
    detector->detectAndCompute(img_2, noArray(), keypoints_2, desc);

//SurfDescriptorExtractor extractor;
Ptr<SurfFeatureDetector>  extractor = SurfFeatureDetector::create(minHessian);
Mat descriptors_1, descriptors_2;
extractor->compute(img_1, keypoints_1, descriptors_1);
extractor->compute(img_2, keypoints_2, descriptors_2);

Listing of the new code

 // ConsoleApplication1.cpp : Defines the entry point for the console application.
//

/**
* @file SURF_FlannMatcher
* @brief SURF detector + descriptor + FLANN Matcher
* @author A. Huaman
*/
#define _CRT_SECURE_NO_DEPRECATE
#pragma warning (disable : 4996)
#include "stdafx.h"
#include <stdio.h>
#include <iostream>
#include <stdio.h>
#include <iostream>
#include "opencv2/core.hpp"
#include "opencv2/features2d.hpp"
//#include "D:\OpenCV\sources\modules\features2d\include\opencv2/features2d.hpp"
#include "opencv2/imgcodecs.hpp"
#include "opencv2/highgui.hpp"
#include "D:\OpenCV\opencv_contrib-master\modules\xfeatures2d\include\opencv2/xfeatures2d.hpp"
#include "D:\OpenCV\opencv_contrib-master\modules\xfeatures2d\include\opencv2\xfeatures2d\nonfree.hpp"

using namespace std;
using namespace cv;
using namespace cv::xfeatures2d;

void readme();

/* * @function main * @brief Main function */ int main(int argc, char* argv) { if (argc != 3) { readme(); return -1; }

Mat img_1 = imread(argv[1], IMREAD_GRAYSCALE);
Mat img_2 = imread(argv[2], IMREAD_GRAYSCALE);

if (!img_1.data || !img_2.data)
{
    std::cout << " --(!) Error reading images " << std::endl; return -1;
}

using namespace cv::xfeatures2d;

//Ptr<SIFT> sift = SIFT::create(...);
//-- Step 1: Detect the keypoints using SURF Detector
//int minHessian = 400;
double minHessian = 400.0;

//SurfFeatureDetector detector(minHessian);
//Ptr<SurfFeatureDetector> detector = SurfFeatureDetector::create(minHessian, 4, 3, 0, 0);

Ptr<SURF> detector = SURF::create(minHessian);
std::vector<KeyPoint> keypts;
Mat desc;
//detector->detectAndCompute(img_1, noArray(), keypts, desc);

std::vector<KeyPoint> keypoints_1, keypoints_2;
detector->detectAndCompute(img_1, noArray(), keypoints_1, desc);
detector->detectAndCompute(img_2, noArray(), keypoints_2, desc);


//detector->detect(img_1, keypoints_1);
//detector->detect(img_2, keypoints_2);

//-- Step 2: Calculate descriptors (feature vectors)
//SurfDescriptorExtractor extractor;
Ptr<SurfFeatureDetector>  extractor = SurfFeatureDetector::create(minHessian);

Mat descriptors_1, descriptors_2;

extractor->compute(img_1, keypoints_1, descriptors_1);
extractor->compute(img_2, keypoints_2, descriptors_2);

//-- Step 3: Matching descriptor vectors using FLANN matcher
FlannBasedMatcher matcher;
std::vector< DMatch > matches;
matcher.match(descriptors_1, descriptors_2, matches);

double max_dist = 0; double min_dist = 100;

//-- Quick calculation of max and min distances between keypoints
for (int i = 0; i < descriptors_1.rows; i++)
{
    double dist = matches[i].distance;
    if (dist < min_dist) min_dist = dist;
    if (dist > max_dist) max_dist = dist;
}

printf("-- Max dist : %f \n", max_dist);
printf("-- Min dist : %f \n", min_dist);

//-- Draw only "good" matches (i.e. whose distance is less than 2*min_dist,
//-- or a small arbitary value ( 0.02 ) in the event that min_dist is very
//-- small)
//-- PS.- radiusMatch can also be used here.
std::vector< DMatch > good_matches;

for (int i = 0; i < descriptors_1.rows; i++)
{
    if (matches[i].distance <= max(2 * min_dist, 0.02))
    {
        good_matches.push_back(matches[i]);
    }
}

//-- Draw only "good" matches
Mat img_matches;
drawMatches(img_1, keypoints_1, img_2, keypoints_2,
    good_matches, img_matches, Scalar::all(-1), Scalar::all(-1),
    vector<char>(), DrawMatchesFlags::NOT_DRAW_SINGLE_POINTS);

//-- Show detected matches
imshow("Good Matches", img_matches);

for (int i = 0; i < (int)good_matches.size(); i++)
{
    printf("-- Good Match [%d] Keypoint 1: %d  -- Keypoint 2: %d  \n", i, good_matches[i].queryIdx, good_matches[i].trainIdx);
}

waitKey(0);

return 0;

}

/** * @function readme */ void readme() { std::cout << " Usage: ./SURF_FlannMatcher <img1> <img2>" << std::endl; }