Ask Your Question
0

matchTemplate method in opencv?

asked 2016-01-27 03:40:39 -0600

I am using OpenCV matchTemplate() method to find the box present in the main image that i am having , Basically i have an image that have boxes of different colors and i am trying to locate the box of a particular color eg. Blue color box .

The output that i am getting after the code is that all boxes are getting marked after the python script is executed . So is there any other alternative procedure using that i can distinguish between my color boxes or i can do some manipulations in matchTemplate() method .

import cv2 import numpy as np from matplotlib import pyplot as plt

 img_rgb = cv2.imread('main_image.png')
 img_gray = cv2.cvtColor(img_rgb, cv2.COLOR_BGR2GRAY)
 template = cv2.imread('blue_box.png',0)
 w, h = template.shape[::-1]

 res = cv2.matchTemplate(img_gray,template,cv2.TM_CCOEFF_NORMED)
 threshold = 0.8
 loc = np.where( res >= threshold)
 for pt in zip(*loc[::-1]):
     cv2.rectangle(img_rgb, pt, (pt[0] + w, pt[1] + h), (0,0,255), 2)

 cv2.imwrite('res.png',img_rgb)

I have used the code provided in the opencv site : http://docs.opencv.org/master/d4/dc6/...

Any suggestions as how can i solve this problem using matchTemplate() method or any other method that could help me accomplish this task.

One answer to my problem (I could be wrong) is that i am using gray scale image so it is not able to recognize colors but when i try giving color images to the matchTemplate() method , It is giving me errors .

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
1

answered 2016-01-27 04:24:30 -0600

pklab gravatar image

updated 2016-01-28 06:13:56 -0600

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
edit flag offensive delete link more

Comments

@pklab actually the boxes are colored with multiple colors , eg . a box could be colored with red and green . So i have to pass a template so that the program do it by themselves . The template depends on the user input and hence i do know in advance as what color the user will be selecting.

kishankumar gravatar imagekishankumar ( 2016-01-27 04:51:04 -0600 )edit

If you provide some images it would be easier

pklab gravatar imagepklab ( 2016-01-27 06:08:13 -0600 )edit

@pklab the images are proprietary and hence i can't post it . I will explain you the problem again . My first problem is that i am not able to pass color images to the matchTemplate method as it is giving me an error , stating that i have an MAT: error in the matchTemplate method . I read somewhere that the method accepts only Grayscale images and hence the images are converted to grayscale first and then given as input . So can you help me as how can i pass color images to the method . It would be a great help.

kishankumar gravatar imagekishankumar ( 2016-01-27 20:49:35 -0600 )edit

@kishankumar see edit in my answer

pklab gravatar imagepklab ( 2016-01-28 06:14:57 -0600 )edit

Easier to create an ROI from TouchEvent, scan for colors?

jmbapps gravatar imagejmbapps ( 2016-01-28 09:16:18 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2016-01-27 03:40:39 -0600

Seen: 3,375 times

Last updated: Jan 28 '16