Ask Your Question
0

Feature Matching with FLANN - Exception on "nonfree_init.cpp"

asked 2012-12-02 15:59:41 -0600

Cristina gravatar image

updated 2012-12-02 16:04:36 -0600

HI! I'm new to OpenCV. I use opencv 2.4.3 with Visual Studio 2010 Express on Windows 7. To better understand OpenCV, I created a project on matching using SURF and FLANN. I just copied / pasted the code from the opencv tutorial.

I report my code:

#include <stdio.h>
#include <iostream>
#include "C:/Users/Cristina/Desktop/OpenCV/opencv/modules/core/include/opencv2/core/core.hpp"
#include "C:/Users/Cristina/Desktop/OpenCV/opencv/modules/features2d/include/opencv2/features2d/features2d.hpp"
#include "C:/Users/Cristina/Desktop/OpenCV/opencv/modules/highgui/include/opencv2/highgui/highgui.hpp"
#include "C:/Users/Cristina/Desktop/OpenCV/opencv/modules/nonfree/include/opencv2/nonfree/features2d.hpp"
#include "C:/Users/Cristina/Desktop/OpenCV/opencv/modules/nonfree/include/opencv2/nonfree/nonfree.hpp"
#include "C:/Users/Cristina/Desktop/OpenCV/opencv/modules/nonfree/src/surf.cpp"
#include "C:/Users/Cristina/Desktop/OpenCV/opencv/modules/nonfree/src/sift.cpp"
#include "C:/Users/Cristina/Desktop/OpenCV/opencv/modules/nonfree/src/nonfree_init.cpp"
#include "C:/Users/Cristina/Desktop/OpenCV/opencv/modules/calib3d/include/opencv2/calib3d/calib3d.hpp"
#include "C:/Users/Cristina/Desktop/OpenCV/opencv/modules/imgproc/include/opencv2/imgproc/imgproc.hpp"

using namespace std;
using namespace cv;

int main() 
{
  Mat img_1 = cvLoadImage( "box.png", CV_LOAD_IMAGE_GRAYSCALE );
  Mat img_2 = cvLoadImage( "box_in_scene.png", CV_LOAD_IMAGE_GRAYSCALE );

  namedWindow("Good Matches", CV_WINDOW_AUTOSIZE);
  imshow("Good Matches",img_1);
  namedWindow("Good Matches1", CV_WINDOW_AUTOSIZE);
  imshow("Good Matches1",img_2);

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

  //-- Step 1: Detect the keypoints using SURF Detector

 cv::initModule_nonfree();
    int minHessian = 400;

  SurfFeatureDetector detector( minHessian );
  vector<KeyPoint> keypoints_1, keypoints_2;
        printf("arriva qui 1\n");

  detector.detect( img_1, keypoints_1);
        printf("arriva qui 2\n");
  detector.detect( img_2, keypoints_2 );

  //-- Step 2: Calculate descriptors (feature vectors)
  SurfDescriptorExtractor extractor;

  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 )
  //-- 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 < 2*min_dist )
    { 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
  namedWindow("Good Matches2",CV_WINDOW_AUTOSIZE);
  imshow( "Good Matches2", 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;
}

When I run my project, I get this exception about (I suppose) "nonfree_init.cpp" file: image description

(it seems to me that just does not come in the ... (more)

edit retag flag offensive close merge delete

2 answers

Sort by ยป oldest newest most voted
0

answered 2012-12-06 06:28:42 -0600

Cristina gravatar image

no idea why this exception? I also linked the nonfree (static) library (Project --> property --> Linker --> input --> additional directives). May be the cause?

edit flag offensive delete link more
0

answered 2012-12-04 03:26:53 -0600

Geppertm gravatar image

updated 2012-12-04 03:27:42 -0600

Also had a problem using the FLANN Matcher. Just changed

FlannBasedMatcher matcher

to

BFMatcher matcher(NORM_L2,false);

edit flag offensive delete link more

Comments

I changed it as you suggested, but the problem remains .. It doesn't come in the main function, because doesn't print my test string (printf("uff\n")).

Cristina gravatar imageCristina ( 2012-12-04 06:34:28 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2012-12-02 15:59:41 -0600

Seen: 1,565 times

Last updated: Dec 06 '12