-
Notifications
You must be signed in to change notification settings - Fork 0
/
template matching.py
74 lines (57 loc) · 2.16 KB
/
template matching.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
#Template Matching --
"""
It is a method for searching and finding the location of a template image
in a larger image.OpenCV comes with a function cv2.matchTemplate() for this
purpose. It simply slides the template image over the input image
(as in 2D convolution) and compares the template and patch of input image
under the template image.
"""
import cv2
import numpy as np
img = cv2.imread(r"C:\Opencv_output\avengers.jpg")
grey_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
template = cv2.imread(r"C:\Opencv_output\avengers - Copy.jpg", 0)
w, h = template.shape[::-1]
#this function accept prameters (img,template,method)
res = cv2.matchTemplate(grey_img, template, cv2.TM_CCORR_NORMED)#to highlight that point
print("res==",res)
threshold = 0.999
loc = np.where(res >= threshold) #find brightest point
print("bright pixels==",loc)
count = 0
for i in zip(*loc[::-1]):
print("i===",i)
cv2.rectangle(img, i, (i[0] + w, i[1] + h), (0, 0, 255), 2)
count+=1
print("number of iterations==",count)
img = cv2.resize(img,(800,600))
res = cv2.resize(res,(800,600))
cv2.imshow("img", img)
cv2.imshow("match temp==",res)
cv2.waitKey(0)
cv2.destroyAllWindows()
# #Check All Method
# img = cv2.imread('H:\\Data\\avengers1.jpg',0)
# img2 = img.copy()
# template = cv2.imread('H:\\Data\\temp.jpg',0)
# w, h = template.shape[::-1]
# # All the 6 methods for comparison in a list
# methods = ['cv2.TM_CCOEFF', 'cv2.TM_CCOEFF_NORMED', 'cv2.TM_CCORR',
# 'cv2.TM_CCORR_NORMED', 'cv2.TM_SQDIFF',
# 'cv2.TM_SQDIFF_NORMED']
# for i in methods:
# print("i==",i)
# img = img2.copy()
# method = eval(i)
# res = cv2.matchTemplate(img,template,method)
# threshold = cv2.minMaxLoc(res) #minMax Loc return 4 values
# #getting cordinates from threshold
# x1 = threshold[3]
# cv2.rectangle(img, x1,(x1[0]+w,x1[1]+h) , (255, 0, 255), 2)
# #result display
# img = cv2.resize(img,(400,600))
# cv2.imshow(i+"img",img)
# res = cv2.resize(res,(400,600))
# cv2.imshow(i+"res",res)
# cv2.waitKey(0)
# cv2.destroyAllWindows()