forked from ryoppippi/Gasyori100knock
-
Notifications
You must be signed in to change notification settings - Fork 0
/
answer_57.py
60 lines (45 loc) · 1.38 KB
/
answer_57.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
import cv2
import numpy as np
import matplotlib.pyplot as plt
# template matching
def Template_matching(img, template):
# get original image shape
H, W, C = img.shape
# subtract mean BGR
_img = img - np.mean(img, axis=(0, 1))
# get template image shape
Ht, Wt, Ct = template.shape
# subtract mean BGR
_template = template - np.mean(img, axis=(0, 1))
# Templete matching
# prepare x, y index
i, j = -1, -1
# prepare evaluate value
v = -1
for y in range(H - Ht):
for x in range(W - Wt):
# get ZNCC value
# get numerator of ZNCC
_v = np.sum(_img[y : y + Ht, x : x + Wt] * _template)
# devided numerator
_v /= (np.sqrt(np.sum(_img[y : y + Ht, x : x + Wt] ** 2)) * np.sqrt(np.sum(template ** 2)))
# if ZNCC is max
if _v > v:
v = _v
i, j = x, y
out = img.copy()
# draw rectangle
cv2.rectangle(out, pt1=(i, j), pt2=(i+Wt, j+Ht), color=(0,0,255), thickness=1)
out = out.astype(np.uint8)
return out
# Read image
img = cv2.imread("imori.jpg").astype(np.float32)
# Read templete image
template = cv2.imread("imori_part.jpg").astype(np.float32)
# Template matching
out = Template_matching(img, template)
# Save result
cv2.imwrite("out.jpg", out)
cv2.imshow("result", out)
cv2.waitKey(0)
cv2.destroyAllWindows()