Ask Your Question

kishankumar's profile - activity

2016-01-27 20:49:35 -0600 commented answer matchTemplate method in opencv?

@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.

2016-01-27 04:51:04 -0600 commented answer matchTemplate method in opencv?

@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.

2016-01-27 03:40:39 -0600 asked a question 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 .

2016-01-27 03:06:46 -0600 commented answer theory of template matching algorithm

@Mathieu Barnachon , I am trying to use a color image and a color template but i am getting an error , Most of the places where i have seen people using matchTemplate method ,they have done using grayscale image. Can you please explain if possible with an example or so as how i can use color images in template matching . It would be a great help to me.