5

I am making a program that tracks features with ORB from OpenCV (2.43) I followed this tutorial and used advice from here.

My goal is to track the object in video feed (face) and draw a rectangle around it.

My program finds keypoints and matches them correctly, but when I try to use findHomography + perspectiveTransform to find new corners for the image usually returns some nonsense type values (though sometimes it returns correct homography).

Here is an example picture: example

Here is the corresponding problematic part:

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( img_object.cols, 0 );
obj_corners[2] = cvPoint( img_object.cols, img_object.rows ); obj_corners[3] = cvPoint( 0, img_object.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( img_matches, scene_corners[0] + Point2f( img_object.cols, 0), scene_corners[1] + Point2f( img_object.cols, 0), Scalar(0, 255, 0), 4 );
line( img_matches, scene_corners[1] + Point2f( img_object.cols, 0), scene_corners[2] + Point2f( img_object.cols, 0), Scalar( 0, 255, 0), 4 );
line( img_matches, scene_corners[2] + Point2f( img_object.cols, 0), scene_corners[3] + Point2f( img_object.cols, 0), Scalar( 0, 255, 0), 4 );
line( img_matches, scene_corners[3] + Point2f( img_object.cols, 0), scene_corners[0] + Point2f( img_object.cols, 0), Scalar( 0, 255, 0), 4 );

Rest of the code is practically the same as in the links I provided. The lines drawn seem completley random, my goal is only to get minimal rectangle of the source object in new scene, so if there is alternative to using homography that works too.

P.S. Source image to track is a region that is copied from video input and then tracked in new pictures from that input, does it matter?

4

1 回答 1

0

该函数perspectiveTransform在您的对应点集不易出错的假设下估计单应性。但是,在现实世界的数据中,您不能假设。解决方案是使用稳健的估计函数(例如 RANSAC)来解决作为超定方程组的单应性问题。

您可以改用findHomography返回单应性的函数。该函数的输入是一组点。这组至少需要 4 分,但更大的一组更好。单应性只是一个估计,但对错误更稳健。通过使用CV_RANSAC标志,它能够在内部删除异常值(错误的点对应)。

于 2016-06-01T20:03:14.863 回答