I am trying to detect some simple 'red patterns' in an image. Here is the algorithm I follow: 1) filter out all other colors rather than 'red' and creating a balck&white image. I used 'cvtColor' with appropriate masking then I applied 'GaussianBlur' to reduce the noise. So far everthing is fine.
2) I used function 'matchTemplate' as follows to detect the 'arrow' templete in image.
Problem: when 'arrow' template is in photo , it is detected correctly. But when it is not in photo, algorithm detect some other shapes which is mistake. Can somebody modify code, so that when arrow template is not in picture, nothing get detected. Here is my code:
template = cv2.imread(address,0)
w, h = template.shape[::-1]
res = cv2.matchTemplate(self.image['blured'], template, cv2.TM_CCOEFF_NORMED)
min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res)
top_left = max_loc
bottom_right = (top_left[0] + w, top_left[1] + h)
cv2.rectangle(self.image['blured'],top_left, bottom_right, 255, 2)
cv2.rectangle(self.image['normal'], top_left, bottom_right, 255,2)
My template image , which I cropped exactly from main photo:
Anybody can detect my mistake? I am new in image processing. Thanks in advance.
You have to look at your max_val
and put a threshold on it.
Let's say max_val
is x1
when the image contains the arrow and is x2
when the image does not contain the arrow, it should be that x1 > x2
. As a first tentative value you can choose threshold=(x1+x2)/2
and then if max_val > threshold
then the pattern is found in the image else the pattern is not found.
The reason is that matchTemplate
slides through
image
, compares the overlapped patches of sizew
\timesh
againsttempl
using the specified method and stores the comparison results inresult
.
and so your image res
will always have a maximum value disregarding the presence of the arrow in the image.