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\sugarcrm_sour orignal.JPG
Template Image: C:\fakepath\sugarcrm_edit.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