Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

matchTemplate is quite robust to intensity variation in special case of TM_CCOEFF_NORMED so using a gray image you will match all boxes independently of its color.

You could check the color for the matched block after matchTemplate. But if your boxes are simple coloured squares you could use inRange over the Hue channel

matchTemplate is quite robust to intensity variation in special case of TM_CCOEFF_NORMED so using a gray image you will match all boxes independently of its color.

You could check the color for the matched block after matchTemplate. But if your boxes are simple coloured squares you could use inRange over the Hue channel

Edit matchTemplate accepts only 1 channel images. Instead of gray image you could try using the HUE channel:

img_rgb = cv2.imread('main_image.png')
cv.CvtColor(img_rgb, img_Hsv, BGR2HSV)
img_H, img_S, img_V = cv2.split(img_HSV)

template = cv2.imread('blue_box.png')
cv.CvtColor(template, template_Hsv, BGR2HSV)
template_H, template_S, template_V = cv2.split(template_HSV)

resH = cv2.matchTemplate(img_H,template_H,cv2.TM_CCOEFF_NORMED)
loc = np.where( resH >= threshold)
...

in addiction you could try use he 3 rgb channels than sum the score for the 3 channel

img_B, img_G, img_R = cv2.split(img_rgb)
template_B, template_G, template_R = cv2.split(template)
resB = cv2.matchTemplate(img_B,template_B,cv2.TM_CCOEFF_NORMED)
resG = cv2.matchTemplate(img_G,template_G,cv2.TM_CCOEFF_NORMED)
resR = cv2.matchTemplate(img_R,template_R,cv2.TM_CCOEFF_NORMED)

res = resB+resG+resR
loc = np.where( res >= 3*threshold)
... and so on