在this answer中我了解到之前的所有事情" // 5.使用RANSAC"验证匹配。
在我的代码中,我使用该代码除了ransacTest的部分。我面临的问题是,我得到的太多了#34;匹配"这是错误的和/或有时我的代码找到的对象周围的矩形太扭曲了。
//Template image's corners
obj_corners[0] = cvPoint( 0, 0);
obj_corners[1] = cvPoint( best_img.cols, 0 );
obj_corners[2] = cvPoint( best_img.cols, best_img.rows );
obj_corners[3] = cvPoint( 0, best_img.rows );
obj.clear();
scene.clear();
for ( int i = 0; i < best_matches.size(); i++ )
{
//Get the keypoints from the good matches
obj.push_back( best_img_keypoints[ best_matches[i].queryIdx ].pt ); // Template image
scene.push_back( frame_keypoints[ best_matches[i].trainIdx ].pt ); // Frame
}
// -----Find homography----- //
std::vector<uchar> outlier_mask; //I don't use this line
cv::Mat H = findHomography( obj, scene, CV_RANSAC, reprojThres, outlier_mask);
cv::perspectiveTransform(obj_corners, scene_corners, H);
a)如果我使用基本矩阵,我可以使用findHomography和perspectiveTransform吗?
b)以上几行有什么问题吗?
答案 0 :(得分:0)
我和你有同样的问题。我通过将symMatches视为good_matches(best_matches)和
之后解决了这个问题if (good_matches.size() > 3){
//-- Localize the object
std::vector<Point2f> obj;
std::vector<Point2f> scene;
for (int i = 0; i < good_matches.size(); i++)
{
//-- Get the keypoints from the good matches
obj.push_back(keypoints_object[good_matches[i].queryIdx].pt);
scene.push_back(keypoints_scene[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(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);
它也适合你。