0

How can I match a masked image using matchTemplate() in OpenCV with C++?

This is my masked template image:

enter image description here

And this is my source image:

enter image description here

Jaime Ivan Cervantes
  • 3,579
  • 1
  • 40
  • 38

2 Answers2

2

matchTemplate's fifth argument is a mask array which you can use for this purpose. In your case, you'll want a binary mask. That is, a Mat with:

  • depth CV_8U, and
  • dimensions equal to your template image, and
  • the pixels you want to ignore from your template set to 0, and
  • the pixels you want to be used set to a non-zero value (typically 1)
Mark Amery
  • 143,130
  • 81
  • 406
  • 459
0

Look at opencv example. You gotta call the matchTemplate function:

matchTemplate( img, templ, result, match_method );
Safir
  • 902
  • 7
  • 9
  • @Sadir: I looked at that example and I used matchTemplate() before, but I would like to know the best way to match a template image that is masked. – Jaime Ivan Cervantes Apr 16 '13 at 06:31
  • I don't understand what you mean by > "I would like to know the best way to match a template image that is masked". – Safir Apr 16 '13 at 06:33
  • If you look at the template image in my question you will see that it has black pixels. In a masked image, the black pixels will be "transparent", and only the pixels with values > 0 will be taken into consideration when matching. – Jaime Ivan Cervantes Apr 16 '13 at 06:39
  • `matchTemplate` does something like convolution, it slides the windows over the image and applies the convolution. So, I don't think it's possible to specify the template image as "mask" the way you want it. – Safir Apr 16 '13 at 06:57