Brute Force 360 Degree Template matching OpenCV Python

Viewed 573

I'm trying to find all the arrows on the original image using the below template image and draw a rectangle around them. I do not want to use Sift/Surf/homography/etc. Only template matching. I only want to use 1 template and not generate 360 individual 1 degree rotation templates as reference.

Template: enter image description here

Original Image:enter image description here

This is my code so far

import cv2
import numpy as np
import imutils

template = cv2.imread("C:\\Users\\Desktop\\All\\images\\template.png")
template = cv2.cvtColor(template, cv2.COLOR_BGR2GRAY)
_ ,template = cv2.threshold(template,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)

image = cv2.imread("C:\\Users\\Desktop\\All\\images\\original image.png")
image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
_ ,image = cv2.threshold(image,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)

MATCH_THRESH = 4000000

for degrees in range(0, 360, 1):
    rotate = imutils.rotate_bound(template,degrees)
    w, h = rotate.shape[::-1]
    res = cv2.matchTemplate(image, rotate, cv2.TM_SQDIFF)
    loc = np.where(res < MATCH_THRESH)
    for pt in zip(*loc[::-1]):
        rect = cv2.rectangle(image, pt, (pt[0] + w, pt[1] + h), (0,255,0), 4)
        cv2.imshow("matches",image)
        cv2.imshow("rectangle",rect)
        cv2.waitKey(500)
        cv2.destroyAllWindows()
        print('Match for deg{}, pt({}, {}), sqdiff {}'.
            format(degrees,pt[0],pt[1],res[pt[1],pt[0]]))

I take both the .png's and convert them to gray and then to black and white using Otsu, which should help with the template matching.

Next I rotate my template image in 1 degree steps through 360 degrees starting at 0. "degrees" saves what degree i'm currently at and affects the rotation of my template using this function, rotate = imutils.rotate_bound(template,degrees).

Then I run cv2.matchTemplate for each degree and save the location of points higher than a certain threshold and draw a rectangle around the found match based on the rotated templates size.

This is where i'm running into an issue. I cant seem to get it to display the rectangles.I know its finding the points because its stating so. I've tried every combination of cv2.imshow. Do you guys see something I don't? Thank you.

1 Answers

You are not able to see the coloured rectangle because the picture you are trying to display it in is converted to grayscale. I suggest you store a copy before you convert to grayscale and use it to display the rectangles in.

Related