matchTemplate method in opencv?
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 .