[英]Drawing matches from Nearest Neighbour Distance Ratio
在openCV中,我已在我的應用程序中改編了此教程代碼
我一直在嘗試使用“最近的鄰居距離比率”來修剪匹配項,以便僅將匹配項高於某個閾值。
double ratio = 0.9;
std::vector< vector<DMatch > > nnMatches;
std::vector< DMatch > good_NNmatches;
matcher.knnMatch(descriptors_scene, descriptors_object, nnMatches, 2 );
for(int k = 0; k < nnMatches.size(); k++)
{
if(nnMatches[k][0].distance / nnMatches[k][1].distance > ratio)
{
good_NNmatches.push_back(nnMatches[k][0]);
}
}
然后,我嘗試使用本教程中演示的相同方法在good_NNmatches中繪制匹配項,但出現以下錯誤:
OpenCV Error: Assertion failed (i1 >= 0 && i1 < static_cast<int>(keypoints1.size())) in drawMatches, file /Users/cgray/Downloads/opencv-2.4.6/modules/features2d/src/draw.cpp, line 207
libc++abi.dylib: terminating with uncaught exception of type cv::Exception: /Users/cgray/Downloads/opencv-2.4.6/modules/features2d/src/draw.cpp:207: error: (-215) i1 >= 0 && i1 < static_cast<int>(keypoints1.size()) in function drawMatches
嘗試使用good_nnMatches而不是本教程中描述的good_matches調用drawMatches時。
drawMatches( roiImg, keypoints_object, compare, keypoints_scene,
good_NNmatches, img_matches, Scalar::all(-1), Scalar::all(-1),
vector<char>(), DrawMatchesFlags::NOT_DRAW_SINGLE_POINTS );
我只是有同樣的問題。 就我而言
cv::Mat output;
drawMatches( smallimg, keyptsSm, bigimg, keyptsBg, matchesBig2Small, output );
匹配matchesBig2Small
的匹配matchesBig2Small
是從bigimg
到smallimg
std::vector<cv::DMatch> matchesBig2Small;
//match from bigimg to smallimg
因此,您得到的錯誤實際上是在尋找smallimg
中的關鍵點索引,該索引實際上指向第二張圖像,該索引較大,因此您超出了smallimg
的大小。 因此,可以drawMatches
的調用drawMatches
為:
drawMatches( bigimg, keyptsBg, smallimg, keyptsSm, matchesBig2Small, output );
或者改變你的計算比賽是從路smallimg
到bigimg
,所以不是matchesBig2Small
你必須matchesSmall2Big
,然后呼叫將是:
std::vector<cv::DMatch> matchesSmall2Big;
//match from smallimg to bigimg
drawMatches( smallimg, keyptsSm, bigimg, keyptsBg, matchesSmall2Big, output );
在經過一番嘗試和錯誤之后,我看來現在已經解決了這個問題。
我更改了drawMatches方法,以將“ roiImg”和關鍵點與“ compare”和關鍵點交換。
drawMatches(compare, keypoints_scene, roiImg, keypoints_object,
good_NNmatches, img_matches, Scalar::all(-1), Scalar::all(-1),
vector<char>(), DrawMatchesFlags::NOT_DRAW_SINGLE_POINTS );
這停止了斷言錯誤。 但是由於要在檢索關鍵點時交換價值,我還必須進行一些更改。 trainIdx和queryIdx也已交換,以說明以前所做的更改。
obj.push_back( keypoints_object[ good_NNmatches[i].trainIdx ].pt );
scene.push_back( keypoints_scene[ good_NNmatches[i].queryIdx ].pt );
我已經做到了,沒有任何問題,希望這段代碼對您有所幫助
double NNDR;
for(int i=0;i<(int)ORB_ORB_matches.size(); i++){
NNDR= ( ORB_ORB_matches[i][0].distance/ORB_ORB_matches[i][1].distance );
if(NNDR <= 0.9){
ORB_single_matches.push_back (ORB_ORB_matches[i][0]);
}
}
drawMatches( image_1, keypoints_1, image_2, keypoints_2, ORB_single_matches, img_matches,Scalar::all(-1),Scalar::all(-1),vector<char>(),`enter code here` DrawMatchesFlags::DRAW_RICH_KEYPOINTS|DrawMatchesFlags::NOT_DRAW_SINGLE_POINTS);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.