shimat / opencvsharp

OpenCV wrapper for .NET

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

MatchTemplate results are inconsistent or incorrect

Nadafy opened this issue · comments

I am experiencing an inconsistency in the results of the MatchTemplate method between OpenCVSharp and EmguCV. When using the same template image and search image, the EmguCV version of the code consistently finds a best match with a similarity score of approximately 0.99, while the OpenCVSharp version of same code does not find any matches at all. Also if you save the match result as an image for debugging, it looks completely irrelevant!

Steps to reproduce:

  • Cut a template image from an image to be searched.
  • Run MatchTemplate and just check the result.

I am on windows,
Visual Studio 2022,
OpenCvSharp4.Windows 4.8.0.20230708 from NuGet

Sample Code:

Mat sourceMat= new Mat(path1, ImreadModes.Grayscale);
Mat templateMat= new Mat(path2, ImreadModes.Grayscale);

Mat matchResult = new Mat();
Cv2.MatchTemplate(sourceMat, templateMat, matchResult, TemplateMatchModes.CCoeffNormed);
//For debugging purpose
Cv2.Normalize(matchResult, matchResult, 0, 255, NormTypes.MinMax);
matchResult.SaveImage("MatchMapForDebugging.png");

//double minval, maxval, threshold = 0.8;
//Point minloc, maxloc;
//Cv2.MinMaxLoc(res, out minval, out maxval, out minloc, out maxloc);