OpenCV的findHomography产生废话结果 [英] OpenCV's findHomography produces nonsense results

查看:261
本文介绍了OpenCV的findHomography产生废话结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个程序,用于跟踪我遵循的来自OpenCV(2.43)的ORB功能 本教程,并使用了建议

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.

我的程序找到关键点并正确匹配它们,但是当我尝试使用findHomography + perspectiveTransform查找图像的新角时,通常会返回一些废话类型值(尽管有时它会返回正确的单应性).

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 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.要跟踪的源图像是一个区域,该区域从视频输入中复制,然后从该输入中跟踪到新图片,这有关系吗?

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?

推荐答案

函数 perspectiveTransform 在假定您的相应点集不容易出错的情况下估计单应性.但是,在现实世界的数据中,您无法假设这一点.解决方案是使用稳健的估计函数(如RANSAC)来解决单应性问题,作为方程的超定系统.

The function perspectiveTransform estimates the homography under the assumption that your corresponding points set are not error prone. However, in real world data you cannot assume that. The solution is to use a robust estimation function such as the RANSAC to solve the homography problem as an overdetermine system of equations.

您可以改用 findHomography 函数,该函数返回单应性.该功能的输入是一组点.该组至少需要4点,但较大的组更好.单应性仅是一种估计,但对错误更鲁棒.通过使用 CV_RANSAC 标志,它可以在内部删除异常值(错误点对应).

You can use the findHomography function instead which returns a homography. The input of this function is a set of points. This set needs at least 4 point but a larger set is better. The homography is only an estimate but which is more robust against errors. By using the CV_RANSAC flag it is able to remove outliers (wrong point correspondences) internaly.

这篇关于OpenCV的findHomography产生废话结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆