pythonimageopencvcolorsmatchtemplate

What method is used in opencv matchtemplate source code to process color images?


The documentation of opencv matchtemplate says that

In case of a color image, template summation in the numerator and each sum in the denominator is done over all of the channels and separate mean values are used for each channel. That is, the function can take a color template and a color image. The result will still be a single-channel image, which is easier to analyze.

I can't figure out what it means. For a color template and a color image, is the single-channel image (the result) the average of the results of all the channels?

templmatch.cpp source code: github


Solution

  • Looking at the source code of the convolve_32F function used in matchTemplate, it seems that template matching on color images actually transforms the color image and the color template into gray images with three times as many columns before applying the convolution between the image and the template as grayscale images.

    To illustrate how the conversion to a gray image is done, consider the following 2x2 image with 4 color pixels (written with BGR values):

    (1, 2, 3) (4, 5, 6)
    (7, 8, 9) (10,11,12)
    

    It becomes the following 2x6 gray image:

    (1)  (2)  (3)  (4)  (5)  (6)
    (7)  (8)  (9)  (10) (11) (12)
    

    They perform the convolution same as if it were gray images and then extract the result by taking one value out of three in the result image (equivalent to extracting the first channel of a color image).