Why OpenCV matchTemplate API not detect all occurrence of image?
I am not able detect all occurrence of template image from source image. Please look attache below file and code for the same.
source Image: C:\fakepath\source.JPG
Template Image: C:\fakepath\target.jpg
In the source image there are 3 occurrence of template but matchTemplate API give only two( first and third). The second occurrence not found with 0.98 threshold. I can found the second occurrence with 0.8 threshold.
Can you help me what the wrong with below code?
How detect with all 3 occurrence with 0.98 threshold?
cv::Mat ref = cv::imread("source.JPG");
cv::Mat tpl = cv::imread("template.jpg")
cv::Mat gref, gtpl;
cv::cvtColor(ref, gref, CV_BGR2GRAY);
cv::cvtColor(tpl, gtpl, CV_BGR2GRAY);
cv::Mat res(ref.rows - tpl.rows + 1, ref.cols - tpl.cols + 1, CV_32FC1);
cv::matchTemplate(gref, gtpl, res, CV_TM_CCOEFF_NORMED);
double THRESHOLD = 0.98;
while (true)
{
double minval, maxval;
cv::Point minloc, maxloc;
cv::minMaxLoc(res, &minval, &maxval, &minloc, &maxloc);
if (maxval >= THRESHOLD)
{
cv::rectangle(ref,maxloc,cv::Point(maxloc.x + tpl.cols, maxloc.y + tpl.rows),CV_RGB(0, 255, 0), 2);
cv::floodFill(res, maxloc, cv::Scalar(0), 0, cv::Scalar(.1), cv::Scalar(1.));
}
else
break;
}
cv::imshow("result", ref);
cv::waitKey();
- Thanks
Case 1- I am using method CV_TM_CCORR_NORMED, which is working fine with most of the examples. But not working in few. using match-percentage/threshold 99%. see below screen, finding folder img, but it also gives false images.
source img - C:\fakepath\Capture.PNG target img - C:\fakepath\target.png output - C:\fakepath\folderSearch.JPG
Case 2-
Not finding the target img if source img is 75% of original img. using CV_TM_CCORR_NORMED, img open mode is IMREAD_COLOR. code using same that i posted, threshold is 95%.
source img - C:\fakepath\inflow75.PNG target img - C:\fakepath\target.png
output - 20180321.2011 - C:\fakepath\BergersAlgo.PNG
I tried the recent code posted here. Target was not found, I attached the screen.