-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathimage_matching.py
151 lines (106 loc) · 4.31 KB
/
image_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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#############################################################################
# Image matching for 3D printing to detect failures
#
# Description: Images are loaded from disk, applied thresholding and
# computed hu moments to calculate the difference between them
#
# References:
# - Computer Vision Course 2020, Laboratory 5, from Technical University
# Iasi, Faculty of Automatic Control and Computer Engineering
#############################################################################
import cv2
import os
import glob
import numpy as np
from scipy.spatial import distance
from matplotlib import pyplot as plt
IMG_SLICER = 0
IMG_PRINTED = 1
def load_images(path):
data_path = os.path.join(path,'*g')
files = glob.glob(data_path)
data = []
for f in files:
images = dict()
images["name"] = os.path.basename(f)
img = cv2.imread(f)
images["image"] = img
data.append(images)
return data
def load_image(path):
image = dict()
image["name"] = os.path.basename(path)
img = cv2.imread(path)
image["image"] = img
return image
def draw_image(image, name):
cv2.namedWindow(name, cv2.WINDOW_NORMAL)
cv2.resizeWindow(name, 600, 600)
#cv2.imshow(name, image)
cv2.imwrite("images/export/" + name, image)
cv2.waitKey(0)
cv2.destroyAllWindows()
def filter_slicer_image(image):
hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
# Range for lower red
lower_red = np.array([0, 120, 70])
upper_red = np.array([10, 255, 255])
mask1 = cv2.inRange(hsv, lower_red, upper_red)
# Range for upper red
lower_red = np.array([170, 120, 70])
upper_red = np.array([180, 255, 255])
mask2 = cv2.inRange(hsv, lower_red, upper_red)
mask = mask1 + mask2
return mask
def filter_printer_image(image):
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
filtered_image = cv2.medianBlur(gray, 11)
_, im = cv2.threshold(filtered_image, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
kernel = np.ones((11, 11), np.uint8)
print(kernel)
opening = cv2.morphologyEx(im, cv2.MORPH_OPEN, kernel)
return opening
def calculate_hu_moments(image):
moments = cv2.moments(image)
hu_moments = cv2.HuMoments(moments)
return hu_moments
def compare_moments(moment_1, moment_2):
difference = distance.euclidean(moment_1, moment_2)
return difference
def successful_test():
# Read test and database images
slicer_images = load_images("images/phil_slicer/")
printer_images = load_images("images/phil_3d_printed/")
for sl_img, pr_img in zip(slicer_images, printer_images):
filtered_image = filter_slicer_image(sl_img["image"])
sl_hu_moments = calculate_hu_moments(filtered_image)
draw_image(filtered_image, sl_img["name"])
filtered_image = filter_printer_image(pr_img["image"])
pr_hu_moments = calculate_hu_moments(filtered_image)
draw_image(filtered_image, pr_img["name"])
diff = compare_moments(sl_hu_moments, pr_hu_moments)
print("Similarity(" + sl_img["name"] + ", " + pr_img["name"] + " = " + str(diff))
def fail_test():
# Read test and database images
slicer_image = load_image("images/phil_slicer/phil_layer2_slicer.png")
successful_image = load_image("images/phil_3d_printed/phil_layer2_printed.jpg")
failed_images = load_images("images/phil_3d_printed/fault/")
filtered_image = filter_slicer_image(slicer_image["image"])
sl_hu_moments = calculate_hu_moments(filtered_image)
draw_image(filtered_image, slicer_image["name"])
filtered_image = filter_printer_image(successful_image["image"])
su_hu_moments = calculate_hu_moments(filtered_image)
draw_image(filtered_image, successful_image["name"])
diff = compare_moments(sl_hu_moments, su_hu_moments)
print("Similarity(" + slicer_image["name"] + ", " + successful_image["name"] + " = " + str(diff))
for fl_img in failed_images:
filtered_image = filter_printer_image(fl_img["image"])
pr_hu_moments = calculate_hu_moments(filtered_image)
draw_image(filtered_image, fl_img["name"])
diff = compare_moments(sl_hu_moments, pr_hu_moments)
print("Similarity(" + slicer_image["name"] + ", " + fl_img["name"] + " = " + str(diff))
def main():
successful_test()
fail_test()
if __name__== "__main__":
main()