shimat / opencvsharp

OpenCV wrapper for .NET
Apache License 2.0
5.22k stars 1.13k forks source link

Max Score is infinity when using Mask for Template Matching #1669

Closed ShiddhamSharma closed 1 month ago

ShiddhamSharma commented 1 month ago

Summary of your issue

I am adding a mask when I am performing template matching but the score is going to infinity and the location is also incorrect. I have tried a similar approach in Python and it is working but not in C#. In this, the mask has white lines where template matching will be done. I have also tried keeping the complete mask as white but I am still facing the same issue.

Environment

Language: C# OpenCVSharp Version: 4.9 .NET Core Version: 8.0

Example code:

My mask are white contour lines and I generate it like this:

Mat mask = new Mat(temp.Height, temp.Width, MatType.CV_8UC1, Scalar.All(0));
Cv2.DrawContours(mask, contours, -1, Scalar.White, 1);
Cv2.MatchTemplate(image, template, result, TemplateMatchModes.CCorrNormed, mask);
Cv2.MinMaxLoc(result, out _, out double maxVal, out _, out OpenCvSharp.Point maxLoc);

Output:

maxVal is = infinity and Location of found template is wrong

ShiddhamSharma commented 1 month ago

I used Mode as CCorr and it worked fine by finding the correct location. Why is the denominator zero then when I am trying to do normalization?

ShiddhamSharma commented 1 month ago

I checked the formula for CCORR and understood why this problem might be there. My image is also thresholded and has much larger areas with black values. Because of this, the denominator for the summation of intensity at given locations in the image with the mask might come out as zero. Though the numerator must also be zero, it somehow gives such cases as infinity. For this, I went element by element and converted the cases where score is greater than 1 to -1. After this it is working fine. Let me know if there is any other short way?