Ask Your Question

findTemplate: why conversion to grayscale is needed

asked 2013-03-25 07:53:08 -0500

Eugene gravatar image

As far as I can see from the code of Image.findTemplate() (, both the image and the template image are converted to gray scale before matching:

cv.MatchTemplate( self._getGrayscaleBitmap(), template_image._getGrayscaleBitmap(), matches, method )

I wonder, why it is this way. If I understand the description of matchTemplate() in OpenCV docs correctly, matchTemplate() itself can operate on colour images.

If the conversion to grayscale is done on purpose, what is the recommended way to locate an image on a screenshot where several more images with similar shapes but different colours can be present?

By the way, if I disable the conversion, that is, change that line of code of findTemplate() to

cv.MatchTemplate( self.getBitmap(), template_image.getBitmap(), matches, method )

findTemplate() seems to work OK in my experiments although 'quality' value for the found matches is lower than if the conversion is enabled.

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2013-03-25 09:12:31 -0500

kscottz gravatar image

The short answer to this question is that template matching is usually done in grayscale as you are looking to match texture and gradient information more than you are looking to match color. For most applications you aren't going to have two things that look exactly the same but are different colors, and more often than not some of that color information will be preserved in the grayscale image. Since you don't need color information for most applications there really isn't a good reason to perform the template match over all three channels.

That said, I could see how this could be confusing, or cases where there would be applications for this. I went ahead and filed a ticket to get this in the next release of the library.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower


Asked: 2013-03-25 07:53:08 -0500

Seen: 775 times

Last updated: Mar 25 '13