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.