-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path51 Template Matching using opencv.py
42 lines (33 loc) · 1.18 KB
/
51 Template Matching using opencv.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
"""
@author: Osama Shakeel
---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("D:\\music\\avengers.jpg")
grey_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
template = cv2.imread("D:\\music\\temp.jpg", 0)
w, h = template.shape[::-1]
#this function accept prameters (img,template,method)
res = cv2.matchTemplate(grey_img, template, cv2.TM_CCORR_NORMED)
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()