Ask Your Question
0

Could Opencv2.4.2 registrate images?

asked 2012-07-16 05:59:57 -0500

this post is marked as community wiki

This post is a wiki. Anyone with karma >50 is welcome to improve it.

And would any friend give me an example about regiatration of images? To make the work,I have do some preparation, I write code as follow:

   void main( ) 
{
    // Load images
Mat imgA = imread("E:\\picture\\Bimg1.jpg", CV_LOAD_IMAGE_GRAYSCALE);
Mat imgB = imread("E:\\picture\\Bimg4.jpg", CV_LOAD_IMAGE_GRAYSCALE );
std::vector<KeyPoint> keypointsA, keypointsB;
Mat descriptorsA, descriptorsB;
std::vector<DMatch> matches;

// DETECTION
SurfFeatureDetector detector(3000);
    //DESCRIPTOR    
FREAK extractor(true, true, 22, 4, std::vector<int>());
     BruteForceMatcher<Hamming> matcher;
detector.detect( imgA, keypointsA );
detector.detect( imgB, keypointsB );
extractor.compute( imgA, keypointsA, descriptorsA );
extractor.compute( imgB, keypointsB, descriptorsB );
     matcher.match(descriptorsA, descriptorsB, matches);

}

You know that follow the code above we could get corespondent points,the problem is that I couldn"t make registration of images.How to do the further work? I want to make fusion of images based on homography matrix .

edit retag flag offensive close merge delete

Comments

What do you mean? You want to know how to extract the good matches and draw boundary around them? In simpler words, determine, whether the object was found in the scene or not?

XCoder gravatar imageXCoder ( 2012-07-16 06:04:15 -0500 )edit

1 answer

Sort by » oldest newest most voted
2

answered 2012-07-16 07:14:04 -0500

this post is marked as community wiki

This post is a wiki. Anyone with karma >50 is welcome to improve it.

Well I'm not sure I understood exactly what you're up to. But this is how I find the homography of detected keypoints. It's more or less based on this OpenCV tutorial: http://docs.opencv.org/doc/tutorials/features2d/featurehomography/featurehomography.html#feature-homography

Only in my code I'm using Flann matcher instead of brute force, but you can use brute force as well if you wish, then, if you are using opencv I think 2.4 and up you have to change "BruteForceMatcher" to "BFMatcher" in your case it might be "BFMatcher matcher(HAMMING)" I'm not sure about the hamming part I know exactly that this works BFMatcher matcher(NORM_L1);

//-- Step 3: Matching descriptor vectors using FLANN matcher
  FlannBasedMatcher matcher;  
  std::vector< vector<DMatch>  > matches;
  matcher.knnMatch( descriptors_object, descriptors_scene, matches, 2 );    
  vector< DMatch > good_matches;
  good_matches.reserve(matches.size());  

  for (size_t i = 0; i < matches.size(); ++i)
  { 
      if (matches[i].size() < 2)
                  continue;

      const DMatch &m1 = matches[i][0];
      const DMatch &m2 = matches[i][1];

      if(m1.distance <= nndrRatio * m2.distance)        
      good_matches.push_back(m1);     
  }

  //If there are at least 7 good matches, then object has been found
  if( (good_matches.size() >=7))
  { 
    cout << "OBJECT FOUND!" << endl;

    std::vector<Point2f> obj;
    std::vector<Point2f> scene;

    for( unsigned int i = 0; i < good_matches.size(); i++ )
    {
        //-- Get the keypoints from the good matches
        obj.push_back( keypointsO[ good_matches[i].queryIdx ].pt );
        scene.push_back( keypointsS[ good_matches[i].trainIdx ].pt );
    }

    Mat H = findHomography( obj, scene, CV_RANSAC );



    //-- Get the corners from the image_1 ( the object to be "detected" )
    std::vector<Point2f> obj_corners(4);
    obj_corners[0] = cvPoint(0,0); obj_corners[1] = cvPoint( objectP.cols, 0 );
    obj_corners[2] = cvPoint( objectP.cols, objectP.rows ); obj_corners[3] = cvPoint( 0, objectP.rows );
    std::vector<Point2f> scene_corners(4);

    perspectiveTransform( obj_corners, scene_corners, H);


    //-- Draw lines between the corners (the mapped object in the scene - image_2 ) 
    line( outImg, scene_corners[0] , scene_corners[1], color, 2 ); //TOP line
    line( outImg, scene_corners[1] , scene_corners[2], color, 2 );
    line( outImg, scene_corners[2] , scene_corners[3], color, 2 );
    line( outImg, scene_corners[3] , scene_corners[0] , color, 2 ); 
    objectFound=true;
  }
else
{
  cout << "OBJECT NOT FOUND" << endl;
}
edit flag offensive delete link more
Login/Signup to Answer

Question Tools

1 follower

Stats

Asked: 2012-07-16 05:59:57 -0500

Seen: 834 times

Last updated: Jul 16 '12