-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtemplate_matching.py
60 lines (45 loc) · 1.65 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
import numpy as np
import matplotlib.pyplot as plt
from PIL import Image
from skimage import data
from skimage.feature import match_template
def pil_loader(path):
# open path as file to avoid ResourceWarning (https://github.com/python-pillow/Pillow/issues/835)
with open(path, 'rb') as f:
img = Image.open(f)
return img.convert('RGB')
def rgb2gray(rgb):
r, g, b = rgb[:,:,0], rgb[:,:,1], rgb[:,:,2]
gray = 0.2989 * r + 0.5870 * g + 0.1140 * b
return gray
# normal_img_path = "./dataset/training/s1/normal.tif"
# defect_img_path = "./dataset/training/s1/defect.tif"
normal_img_path = "./dataset/training/s3/normal.tif"
defect_img_path = "./dataset/training/s3/defect.tif"
normal_img = pil_loader(normal_img_path)
defect_img = pil_loader(defect_img_path)
#patch = rgb2gray(np.array(normal_img)[0:660, 0:100, :])
patch = rgb2gray(np.array(normal_img)[10:710, 10:710, :])
defect_img = rgb2gray(np.array(defect_img)[:, :, :])
result = match_template(defect_img, patch)
ij = np.unravel_index(np.argmax(result), result.shape)
print(ij)
x, y = ij[::-1]
a1, ax1 = plt.subplots(1)
plt.imshow(normal_img, cmap=plt.cm.gray)
hcoin, wcoin = patch.shape
rect1 = plt.Rectangle((0, 0), wcoin, hcoin, edgecolor='r', facecolor='none')
ax1.add_patch(rect1)
plt.show()
a2, ax2 = plt.subplots(1)
plt.imshow(defect_img, cmap=plt.cm.gray)
# highlight matched region
hcoin, wcoin = patch.shape
rect2 = plt.Rectangle((x, y), wcoin, hcoin, edgecolor='r', facecolor='none')
ax2.add_patch(rect2)
plt.show()
plt.imshow(result)
# highlight matched region
plt.autoscale(False)
plt.plot(x, y, 'o', markeredgecolor='r', markerfacecolor='none', markersize=10)
plt.show()