Skip to content

Commit 9a0333a

Browse files
Merge pull request akshitagupta15june#3 from anukeshi/master
adding template_matching program
2 parents dfb5d42 + 113edda commit 9a0333a

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

template_matching.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import cv2
2+
import numpy as np
3+
4+
cap = cv2.VideoCapture(0)
5+
template = cv2.imread("IMAGE_NAME.JPG", cv2.IMREAD_GRAYSCALE)
6+
w, h = template.shape[::-1]
7+
8+
while True:
9+
_, frame = cap.read()
10+
gray_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
11+
12+
res = cv2.matchTemplate(gray_frame, template, cv2.TM_CCOEFF_NORMED )
13+
loc = np.where(res >= 0.7)
14+
15+
for pt in zip(*loc[::-1]):
16+
cv2.rectangle(frame, pt, (pt[0]+w, pt[1]+h), (0,255,0), 3)
17+
18+
cv2.imshow("Frame", frame)
19+
20+
key = cv2.waitKey(1)
21+
22+
if key == 27:
23+
break
24+
25+
cap.release()
26+
cv2.destroyAllWindows()

0 commit comments

Comments
 (0)