我有更大的在另一個圖像搜索一個小圖像代碼:OpenCV C++ - 查找圖像中包含的圖像?
int* MyLib::MatchingMethod(int, void*)
{
/// Source image to display
img.copyTo(img_display);
/// Create the result matrix
int result_cols = img.cols - templ.cols + 1;
int result_rows = img.rows - templ.rows + 1;
result.create(result_rows, result_cols, CV_32FC1);
match_method = 0;
/// Do the Matching and Normalize
matchTemplate(img, templ, result, match_method);
normalize(result, result, 0, 1, cv::NORM_MINMAX, -1, cv::Mat());
/// Localizing the best match with minMaxLoc
double minVal;
double maxVal;
cv::Point minLoc;
cv::Point maxLoc;
cv::Point matchLoc;
minMaxLoc(result, &minVal, &maxVal, &minLoc, &maxLoc, cv::Mat());
/// For SQDIFF and SQDIFF_NORMED, the best matches are lower values. For all the other methods, the higher the better
if (match_method == CV_TM_SQDIFF || match_method == CV_TM_SQDIFF_NORMED)
{
matchLoc = minLoc;
}
else
{
matchLoc = maxLoc;
}
if (showOpenCVWindow) {
/// Show me what you got
rectangle(img_display, matchLoc, cv::Point(matchLoc.x + templ.cols, matchLoc.y + templ.rows), cv::Scalar(255, 0, 0, 255), 2, 8, 0);
rectangle(result, matchLoc, cv::Point(matchLoc.x + templ.cols, matchLoc.y + templ.rows), cv::Scalar(255, 0, 0, 255), 2, 8, 0);
imshow(image_window, img_display);
imshow(result_window, result);
}
double myX = (matchLoc.x + (templ.cols)/2);
double myY = (matchLoc.y + (templ.rows)/2);
static int o[2];
o[0] = myX;
o[1] = myY;
return o;
}
但這種代碼可以錯誤地「發現」任何區域,即使更大的圖像不包含小圖像。
如何更改此代碼,強制它「精確」搜索小圖像。例如,如果較小的圖像不在較大的圖像上,則此代碼必須顯示任何信息消息「圖像未找到」。
更新1.看起來,像matchTemplate
不工作良好。例如,我有3個圖像 - 一個模板(http://s6.postimg.org/nj2ts3lf5/image.png),一個圖像(包含來自模板的圖像(http://s6.postimg.org/fp6tkg301/image.png))和一個圖像(不包含模板(http://s6.postimg.org/9x23zk3sh/image.png))。
對於第一圖像中,包含模板,MAXVAL = 0.99999994039535522並正確選擇的區域:http://s6.postimg.org/65x4qzfht/image.png
但對於圖像,即不包含模板,MAXVAL = 1.0000000000000000和它錯誤選擇區域,不包含模板圖像:http://s6.postimg.org/5132llt0x/screenshot_544.png
謝謝!
check [these](http://docs.opencv.org/2.4/doc/tutorials/features2d/table_of_content_features2d/table_of_content_features2d.html)out – slawekwin