Homography for set of trainimages

asked 2013-12-05 10:53:56 -0500

viratan gravatar image

updated 2013-12-05 11:14:17 -0500

berak gravatar image

How can I use findhomography()and perspectiveTransform() methods in the case where you have keypoints obtained from the trainset of images.

following is my code.

cv::BruteForceMatcher<cv::HammingLUT > descriptormatcher; 
descriptormatcher.add(addtraindesc); 
descriptormatcher.train();  
descriptormatcher.match(descriptor1,matches1);
 //calculate min and max distance between keypoints
 double max_dist =  0;
 double min_dist = 100;
 for (int i_desc=0; i_desc< descriptor1.rows;i_desc++)
 {
     double dist=matches1[i_desc].distance;
     if( dist < min_dist ) min_dist = dist;
     if( dist > max_dist ) max_dist = dist;
 }
 //Get only good matches
 vector<DMatch> goodmatches;
 for (int i_desc=0; i_desc< descriptor1.rows;i_desc++)
    {
      double good_dist=3*min_dist ;
     if( matches1[i_desc].distance < good_dist )
      { goodmatches.push_back( matches1[i_desc]); }
    }
// calculate object and scene points
 std::vector<Point2f> obj;
 std::vector<Point2f> scene;
 vector<vector<KeyPoint> > newtrainkeypoints=gettrainkeypoints();
for(int i_gm=0; i_gm<goodmatches.size(); i_gm++)
 {
  DMatch imatch = goodmatches[i_gm];
  obj.push_back(newtrainkeypoints[imatch.imgIdx][imatch.trainIdx].pt);
  scene.push_back(v1[imatch.queryIdx].pt);
 }
Mat H = findHomography( obj, scene, CV_RANSAC );
std::vector<Point2f> obj_corners(4);
 // how to calculate the obj_corners for array of images from training set
std::vector<Point2f> scene_corners(4);
     perspectiveTransform( obj_corners, scene_corners, H);
   line( mRgb1, scene_corners[0], scene_corners[1], Scalar(0, 255, 0), 4 );
   line( mRgb1, scene_corners[1], scene_corners[2], Scalar( 0, 255, 0), 4 );
   line( mRgb1, scene_corners[2], scene_corners[3], Scalar( 0, 255, 0), 4 );
   line( mRgb1, scene_corners[3], scene_corners[0], Scalar( 0, 255, 0), 4 );

One quick dirty solution would be to put this entire part of code in a forloop for all the images in training set and perform homography if the number of good matches is greater than 4 .but that would be very ineffective in terms of performance since i plan to use this code for the JNI part of android project im working on.

Can anyone please guide me on how do the perspective transform and draw a rectangle around the detected object or suggest any better methods of doing it.(so far in my search through internet,i could only find 1 to 1 image matching and then draw homography for the detected object)

edit retag flag offensive close merge delete