2

I am trying to find a template in an image and I am using OpenCV for that. The problem is that the TM_SQDIFF function is not exactly what I need and it fails in some cases, for example:

Template:

Template

Image:

Image

Result using SQDIFF:

enter image description here

I want the template to match the bottom right image, because it has more elements that are equal to the template than the result that SQDIFF shows.

What I need seems to be a simpler than the SQDIFF, but I couldn't find a way to do it. Is it possible to do that using OpenCV or another tool in Python?

Ideally I also want to be able to use a mask, because some of the templates are not rectangular.

klaus
  • 1,187
  • 2
  • 9
  • 19
  • No OpenCV does not allows a custom comparator for template matching, it has its own set of predefined methods: `['cv2.TM_CCOEFF', 'cv2.TM_CCOEFF_NORMED', 'cv2.TM_CCORR', 'cv2.TM_CCORR_NORMED', 'cv2.TM_SQDIFF', 'cv2.TM_SQDIFF_NORMED']`. You should give a try to each one of them and see if anyone works(I have lesser hopes BTW). The next thing you can do is, study the template matching code in C++ and write your own implementation with this custom comparator function. – ZdaR Jul 18 '18 at 04:32
  • I know that template matching does not allow custom functions, but I was hoping that some other function or library would solve my problem. I know that if the images are binary, a simple convolution by the template would solve it. – klaus Jul 18 '18 at 05:28
  • Can you attach sample input and template image as well? [This](https://docs.opencv.org/2.4/doc/tutorials/imgproc/imgtrans/filter_2d/filter_2d.html) is the closest thing I can think as of now – ZdaR Jul 18 '18 at 05:30
  • I have edited the question with some example images – klaus Jul 19 '18 at 04:20

0 Answers0