下面是從descriptor_extractor_matcher.cpp樣品可用一個片段從OpenCV的:
if(!isWarpPerspective && ransacReprojThreshold >= 0)
{
cout << "< Computing homography (RANSAC)..." << endl;
vector<Point2f> points1; KeyPoint::convert(keypoints1, points1, queryIdxs);
vector<Point2f> points2; KeyPoint::convert(keypoints2, points2, trainIdxs);
H12 = findHomography(Mat(points1), Mat(points2), CV_RANSAC, ransacReprojThreshold);
cout << ">" << endl;
}
Mat drawImg;
if(!H12.empty()) // filter outliers
{
vector<char> matchesMask(filteredMatches.size(), 0);
vector<Point2f> points1; KeyPoint::convert(keypoints1, points1, queryIdxs);
vector<Point2f> points2; KeyPoint::convert(keypoints2, points2, trainIdxs);
Mat points1t; perspectiveTransform(Mat(points1), points1t, H12);
double maxInlierDist = ransacReprojThreshold < 0 ? 3 : ransacReprojThreshold;
for(size_t i1 = 0; i1 < points1.size(); i1++)
{
if(norm(points2[i1] - points1t.at<Point2f>((int)i1,0)) <= maxInlierDist) // inlier
matchesMask[i1] = 1;
}
// draw inliers
drawMatches(img1, keypoints1, img2, keypoints2, filteredMatches, drawImg, CV_RGB(0, 255, 0), CV_RGB(0, 0, 255), matchesMask
#if DRAW_RICH_KEYPOINTS_MODE
, DrawMatchesFlags::DRAW_RICH_KEYPOINTS
#endif
);
#if DRAW_OUTLIERS_MODE
// draw outliers
for(size_t i1 = 0; i1 < matchesMask.size(); i1++)
matchesMask[i1] = !matchesMask[i1];
drawMatches(img1, keypoints1, img2, keypoints2, filteredMatches, drawImg, CV_RGB(0, 0, 255), CV_RGB(255, 0, 0), matchesMask,
DrawMatchesFlags::DRAW_OVER_OUTIMG | DrawMatchesFlags::NOT_DRAW_SINGLE_POINTS);
#endif
}
else
drawMatches(img1, keypoints1, img2, keypoints2, filteredMatches, drawImg);
關鍵線,用於濾波在這裏進行的:
if(norm(points2[i1] - points1t.at<Point2f>((int)i1,0)) <= maxInlierDist) // inlier
matchesMask[i1] = 1;
其是測量之間的L2範數距離點(如果沒有指定,則爲3個像素,或用戶定義的像素重投影錯誤數)。
希望有幫助!
謝謝你的回答......我自認爲是在C語言編寫的,這片段是C++不能直接在find_obj.cpp使用此代碼。然而,我已經在SURFFlannMatcher.cpp演示中嘗試過了,它並沒有給出好的結果。例如,如果我得到同一個對象,同一場景的兩張圖片,即幾乎相同的圖片但不同的分辨率,findHomography函數將只能找到異常值並且沒有內部值...這很奇怪。 – user1148222 2012-01-19 15:21:26