New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Strange results of matchTemplate using TM_CCORR_NORMED with mask #15768
Comments
Floating-point calculations are not very accurate in general. If you have strong requirement of |
There are values like 17.003805 or 3.4028235e+38. |
Could this be related? #15214 |
I think #15214 is a possible solution. |
I found some clues about the problem. It seems like some sort of overflow or underflow problem. I implemented a TM_CCORR_NORMED algorithm in pytorch. It still gives |
I still have same problem with version 4.5.2 that nan and inf are occuring with TM_CCORR_NORMED and mask provided. |
Have same issue when using template matching on version |
If anyone's looking for a one line fix, you can just set them to zero. This is definitely a hack though.
|
images.zip // source is source.png, tmpl is template.png
cv::Mat mask(tmpl.rows, tmpl.cols, CV_8UC1, cv::Scalar(1));
cv::Mat opencv_res_map;
cv::matchTemplate(source, tmpl, opencv_res_map, cv::TM_CCOEFF_NORMED, mask);
opencv_res_map.convertTo(opencv_res_map, CV_64FC1);
// check
for (int idy = 0; idy < opencv_res_map.rows; idy++)
{
for (int idx = 0; idx < opencv_res_map.cols; idx++)
{
const double& d1 = opencv_res_map.at<double>(idy, idx);
if (d1 < -1.-1e-9 || d1 > 1.+1e-9)
{
// inf or -inf
}
}
} And I use opencv4.5.5 on windows11, msvc 19. If this problem is caused by overflow, should we check whether other results value is correct? |
System information (version)
Detailed description
Results of
cv.matchTemplate(image, needle, cv.TM_CCORR_NORMED, mask=mask)
sometimes contains many values larger than 1 at stange position.
It happens only for TM_CCORR_NORMED with mask.
Also, it seems if the
image
has odd width the problem will not be triggered.Steps to reproduce
This only happens with some special shapes of
image
, it seems odd width never trigger the bug.opencvtest.zip
out.txt
shows the result produced bypython cvtest.py
on my machine, including output ofcv.getBuildInformation()
.I'm not sure what causes this problem.
Opencv 3.4.4 on windows and 3.1.0 on mac has the same problem, but the err_count varies.
Another build of 4.1.1 on linux doesn't has this problem.
The text was updated successfully, but these errors were encountered: