template matching several bounding boxes outputted need only one 1

template matching several bounding boxes outputted need only one

img_rgb = cv2.imread('obsvu.jpg')
img_rgb = cv2.medianBlur(img_rgb, 7)

img_gray = cv2.cvtColor(img_rgb, cv2.COLOR_BGR2GRAY)

template_image = cv2.imread('template.jpg',0)
template_image = cv2.medianBlur(template_image, 5)
width, height = template_image.shape[::-1]

res = cv2.matchTemplate(img_gray, template_image, cv2.TM_CCOEFF_NORMED)

threshold =  0.6

locations = np.where(res >= threshold)

new_locations = group_locations(np.array(locations).T, 50).T

for position_tuple in zip(*new_locations.astype(int)[::-1]):
        cv2.rectangle(img_rgb, position_tuple, (position_tuple[0] + width, position_tuple[1] + height), (0,255,0), 5)

Here is what the above code is Doing:
1. Read the image and template
2. Convert the image to grayscale
3. Apply a median blur to the image and template
4. Find the width and height of the template
5. Apply template matching
6. Set a threshold value for how similar the template and image should be
7. Find the locations of the template in the image
8. Group the locations that are close together
9. Draw a rectangle around the grouped locations

Similar Posts