-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmulti_template_matching.py
More file actions
60 lines (44 loc) · 1.92 KB
/
multi_template_matching.py
File metadata and controls
60 lines (44 loc) · 1.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# import the necessary packages
import cv2
import numpy as np
# set the template matching and
# non-maximum suppression thresholds
thresh = 0.98
nms_thresh = 0.6
# load the main image and the template image
image = cv2.imread("examples/2.jpg")
template = cv2.imread("examples/template2.jpg")
# make a copy of the image
image_copy = image.copy()
# convert the images to grayscale
image_gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
template_gray = cv2.cvtColor(template, cv2.COLOR_BGR2GRAY)
# get the width and height of the template image
template_h, template_w = template.shape[:-1]
# perform template matching using the normalized cross-correlation method
result = cv2.matchTemplate(image_gray, template_gray, cv2.TM_CCOEFF_NORMED)
# get the coordinates of the matches that are above the threshold
y_coords, x_coords = np.where(result >= thresh)
print("Number of matches found:", len(x_coords))
# loop over the coordinates and draw a rectangle around the matches
for x, y in zip(x_coords, y_coords):
cv2.rectangle(image_copy, (x, y), (x + template_w,
y + template_h), (0, 255, 0), 2)
# show the images
cv2.imshow("Template", template)
cv2.imshow("Multi-Template Matching", image_copy)
######################################################################
# Apply Non-Maximum Suppression
######################################################################
# create a list of bounding boxes
boxes = np.array([[x, y, x + template_w, y + template_h]
for (x, y) in zip(x_coords, y_coords)])
# apply non-maximum suppression to the bounding boxes
indices = cv2.dnn.NMSBoxes(
boxes, result[y_coords, x_coords], thresh, nms_thresh)
print("Number of matches found after NMS:", len(indices))
for i in indices:
(x, y, w, h) = boxes[i][0], boxes[i][1], boxes[i][2], boxes[i][3]
cv2.rectangle(image, (x, y), (w, h), (0, 255, 0), 2)
cv2.imshow("Multi-Template Matching After NMS", image)
cv2.waitKey(0)