-
Notifications
You must be signed in to change notification settings - Fork 0
/
templateMatching.py
92 lines (65 loc) · 2.65 KB
/
templateMatching.py
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# Template Matching
# source : https://forum.yazbel.com/t/real-time-template-matching/1076
import cv2
def detect(frame, temp, w, h):
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
result = cv2.matchTemplate(frame, temp, cv2.TM_CCORR_NORMED)
min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(result)
top_left = max_loc
bottom_right = (top_left[0] + w, top_left[1] + h)
return (max_val, top_left, bottom_right)
if __name__ == '__main__':
image = cv2.imread('try.jpg') # aranılan resim
template = cv2.imread('tr.png', 0) # aranan resim
tW, tH = template.shape[::-1]
result = detect(image, template, tW, tH); print(result)
cv2.rectangle(image, *result[1:], (0, 255, 0), 2)
cv2.imshow('match', image)
if cv2.waitKey(0) & 0xFF == ord('q'):
cv2.destroyAllWindows()
"""
# Gerçek Zamanlı Template Matching
# templates diye klasör açıp onun içine aranacak şeyleri koy
#!/usr/bin/env python3
#-*- coding: utf-8 -*-
#
#
__author__ = '@laszlokuehl'
import os
import cv2
class TemplateMatch:
def __init__(self, templates):
self.cam = cv2.VideoCapture(0)
for temp in templates.keys():
templates[temp] = (templates[temp], *templates[temp].shape[::-1])
self.templates = templates
def detect(self, frame, temp, w, h):
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
result = cv2.matchTemplate(frame, temp, cv2.TM_CCORR_NORMED)
min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(result)
top_left = max_loc
bottom_right = (top_left[0] + w, top_left[1] + h)
return (max_val, top_left, bottom_right)
def main(self):
while True:
_, image = self.cam.read()
for name, temp in self.templates.items():
result = self.detect(image, *temp)
if result[0] >= 0.98:
label = '{}: {:.2f}%'.format(name, result[0] * 100)
cv2.putText(image, label, (result[1][0], result[1][1] - 10), cv2.FONT_HERSHEY_COMPLEX, 0.5, (32, 32, 32), 1)
cv2.rectangle(image, *result[1:], (32, 32, 32), 2)
else:
print('{}: {:.2f}%'.format(name, result[0] * 100))
cv2.imshow('temp match', image)
if (cv2.waitKey(5) & 0xFF) == 27:
break
cv2.destroyAllWindows()
if __name__ == '__main__':
app = TemplateMatch(
dict(
[(img.split('.')[0], cv2.imread('templates/' + img, 0)) for img in os.listdir('templates') if 'png' in img]
)
)
app.main()
"""