-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfastFeatureDetector.py
254 lines (196 loc) · 9.5 KB
/
fastFeatureDetector.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
'''
Defect Detection Using OpenCV
Find KeyPoint of the Images and find close 3 points in some distance and draw circle which have center point as one of them
Normal Image : Find Keypoint with smaller threshold value
Defect Image : Find Keypoint with Bigger threshold value
if there are new keypoint at defect images, which is not close to normal image's,
then there are defect.
'''
import cv2
import matplotlib.pyplot as plt
import numpy as np
import math
# Call Normal Image and Pre-processing
normalImage = cv2.imread('./img/normal.jpg')
normalImage = cv2.resize(normalImage, dsize=(640,480), interpolation = cv2.INTER_AREA)
normalImage = cv2.cvtColor(normalImage, cv2.COLOR_BGR2GRAY)
# Call Defect Image and Pre-processing
defectImage = cv2.imread('./img/defect1.jpg')
defectImage = cv2.resize(defectImage, dsize=(640,480), interpolation = cv2.INTER_AREA)
defectImage = cv2.cvtColor(defectImage,cv2.COLOR_BGR2GRAY)
# Find Key point of Normal Image and show it with smaller threshold Value
fastF_normalImage =cv2.FastFeatureDetector.create(threshold=80)
kp_normalImage = fastF_normalImage.detect(normalImage)
dst_normalImage = cv2.drawKeypoints(normalImage, kp_normalImage, None, color=(0,0,255))
print('len(kp_normalImage)=', len(kp_normalImage))
cv2.imshow('dst_normalImage', dst_normalImage)
# Find Key point of Defect Image and show it with bigger threshold Value
fastF_defectImage =cv2.FastFeatureDetector.create(threshold=90)
kp_defectImage = fastF_defectImage.detect(defectImage)
dst_defectImage = cv2.drawKeypoints(defectImage, kp_defectImage, None, color=(0,0,255))
print('len(kp_defectImage)=', len(kp_defectImage))
cv2.imshow('dst_defectImage', dst_defectImage)
# Set 2 Lists to save pixel values for keypoint
list_kp_normalImage = []
list_kp_defectImage = []
# Save pixel values for keypoint
for keypoint in kp_normalImage:
list_kp_normalImage.append(keypoint.pt)
for keypoint in kp_defectImage:
list_kp_defectImage.append(keypoint.pt)
# Initialize distances Value
distanceUnder1 = 0
distanceUnder2 = 0
distanceUnder3 = 0
distanceOver = 0
# Initialize Threshold Distance Values
thresholdDistance1 = 8
thresholdDistance2 = 30
thresholdDistance3 = 50
# Make 2 Lists For save close 3 points
triangleForNormalImage = []
triangleForDefectImage = []
# Find 3 close points and save them to triangleForNormalImage
for i in range(len(kp_normalImage)-2):
for j in range(i+1, len(kp_normalImage)-1):
for m in range(j+1, len(kp_normalImage)):
x1_distance = list_kp_normalImage[i][0] - list_kp_normalImage[j][0]
y1_distance = list_kp_normalImage[i][1] - list_kp_normalImage[j][1]
first_distance = math.sqrt(x1_distance ** 2 + y1_distance ** 2)
x2_distance = list_kp_normalImage[j][0] - list_kp_normalImage[m][0]
y2_distance = list_kp_normalImage[j][1] - list_kp_normalImage[m][1]
second_distance = math.sqrt(x2_distance **2 + y2_distance ** 2)
total_distance = first_distance + second_distance
if(total_distance < thresholdDistance1) :
distanceUnder1 += 1
triangleForNormalImage.append(list_kp_normalImage[j])
elif(total_distance < thresholdDistance2) :
distanceUnder2 += 1
elif(total_distance < thresholdDistance3) :
distanceUnder3 += 1
else :
distanceOver += 1
# Show How many points are close
print('Sum of distance between 3 points are under ', thresholdDistance1 ,' : ', distanceUnder1)
print('Sum of distance between 3 points are under ', thresholdDistance2 ,' : ', distanceUnder2)
print('Sum of distance between 3 points are under ', thresholdDistance3 ,' : ', distanceUnder3)
print('Over ', thresholdDistance3 ,' : ', distanceOver)
# Remove Same Points in triangleForNormalImage
triangleForNormalImage = list(set(triangleForNormalImage))
print(triangleForNormalImage)
print('# of the components for under ', thresholdDistance1,' : ', len(triangleForNormalImage))
# Ready for pop
lengthOfTriangle = len(triangleForNormalImage)
popList = []
# Find close values and save the index to popList
for i in range(lengthOfTriangle-1):
for j in range(i+1, lengthOfTriangle):
distanceXToCheckHowClose = triangleForNormalImage[i][0] - triangleForNormalImage[j][0]
distanceYToCheckHowClose = triangleForNormalImage[i][1] - triangleForNormalImage[j][1]
distance = math.sqrt( distanceXToCheckHowClose ** 2 + distanceYToCheckHowClose ** 2 )
if distance < 10 :
popList.append(j)
popList = list(set(popList))
popList.sort()
print('Pop List : ', popList)
popNum = 0
for i in range(len(popList)):
triangleForNormalImage.pop(popList[i]-popNum)
print('After remove, component of triangle : ', triangleForNormalImage)
print('remove order : ', popList[i]-popNum)
popNum += 1
for i in range(len(triangleForNormalImage)):
cv2.circle(normalImage, (int(float(triangleForNormalImage[i][0])), int(float(triangleForNormalImage[i][1]))), 10, (0,0,255), 3)
# Draw Lines For compare distance
cv2.line(normalImage, (10,10), (40,10), (0,255,0), 5)
print('After remove close pixels : ', triangleForNormalImage)
cv2.imwrite('./img/withCircle_Normal_1.png', normalImage)
cv2.imshow('normalImage', normalImage)
# Initialize distances Value Again
distanceUnder1 = 0
distanceUnder2 = 0
distanceUnder3 = 0
distanceOver = 0
# Find 3 close points and save them to triangleForDefectImage
for i in range(len(kp_defectImage)-2):
for j in range(i+1, len(kp_defectImage)-1):
for m in range(j+1, len(kp_defectImage)):
x1_distance = list_kp_defectImage[i][0] - list_kp_defectImage[j][0]
y1_distance = list_kp_defectImage[i][1] - list_kp_defectImage[j][1]
first_distance = math.sqrt(x1_distance ** 2 + y1_distance ** 2)
x2_distance = list_kp_defectImage[j][0] - list_kp_defectImage[m][0]
y2_distance = list_kp_defectImage[j][1] - list_kp_defectImage[m][1]
second_distance = math.sqrt(x2_distance **2 + y2_distance ** 2)
total_distance = first_distance + second_distance
if(total_distance < thresholdDistance1) :
distanceUnder1 += 1
triangleForDefectImage.append(list_kp_defectImage[j])
elif(total_distance < thresholdDistance2) :
distanceUnder2 += 1
elif(total_distance < thresholdDistance3) :
distanceUnder3 += 1
else :
distanceOver += 1
# Show How many points are close
print('Sum of distance between 3 points are under ', thresholdDistance1 ,' : ', distanceUnder1)
print('Sum of distance between 3 points are under ', thresholdDistance2 ,' : ', distanceUnder2)
print('Sum of distance between 3 points are under ', thresholdDistance3 ,' : ', distanceUnder3)
print('Over ', thresholdDistance3 ,' : ', distanceOver)
# Remove Same Points in triangleForDefectImage
triangleForDefectImage = list(set(triangleForDefectImage))
print(triangleForDefectImage)
print('# of the components for under ', thresholdDistance1,' : ', len(triangleForDefectImage))
# Ready for pop
lengthOfTriangle = len(triangleForDefectImage)
popList = []
# Find close values and save the index to popList
for i in range(lengthOfTriangle-1):
for j in range(i+1, lengthOfTriangle):
distanceXToCheckHowClose = triangleForDefectImage[i][0] - triangleForDefectImage[j][0]
distanceYToCheckHowClose = triangleForDefectImage[i][1] - triangleForDefectImage[j][1]
distance = math.sqrt( distanceXToCheckHowClose ** 2 + distanceYToCheckHowClose ** 2 )
if distance < 10 :
popList.append(j)
popList = list(set(popList))
popList.sort()
print('Pop List : ', popList)
popNum = 0
for i in range(len(popList)):
triangleForDefectImage.pop(popList[i]-popNum)
print('After remove, component of triangle : ', triangleForDefectImage)
print('remove order : ', popList[i]-popNum)
popNum += 1
# backup Defect Image
defectImageForIndicateDefect = defectImage
# Draw Keypoints for defectImage
for i in range(len(triangleForDefectImage)):
cv2.circle(defectImage, (int(float(triangleForDefectImage[i][0])), int(float(triangleForDefectImage[i][1]))), 10, (0,0,255), 3)
print('After remove close pixels : ', triangleForDefectImage)
# Draw Lines For compare distance
cv2.line(defectImage, (10,10), (40,10), (0,255,0), 5)
cv2.imwrite('./img/withCircle_Defect_1.png', defectImage)
cv2.imshow('defectImage', defectImage)
# Record Defect Part
DefectPart = []
for i in range(len(triangleForDefectImage)):
DefectPart.append(1)
for i in range(len(triangleForDefectImage)):
for j in range(len(triangleForNormalImage)):
distanceXBetweenNormalAndDefect = triangleForDefectImage[i][0] - triangleForNormalImage[j][0]
distanceYBetweenNormalAndDefect = triangleForDefectImage[i][1] - triangleForNormalImage[j][1]
distanceBetweenNormalAndDefect = math.sqrt( distanceXBetweenNormalAndDefect **2 + distanceYBetweenNormalAndDefect ** 2)
if(distanceBetweenNormalAndDefect < 30) :
DefectPart[i] = 0
defect = 0
defectIndex = []
for i in range(len(triangleForDefectImage)):
if(DefectPart[i] == 1):
defect = 1
defectIndex.append(i)
if defect == 1 :
print('Defect is Detected!!')
for i in range(len(defectIndex)):
print('Defect Point : ', triangleForDefectImage[defectIndex[i]])
cv2.circle(defectImageForIndicateDefect, (int(float(triangleForDefectImage[i][0])), int(float(triangleForDefectImage[i][1]))), 20, (0,255,255), 3)
cv2.imshow('defectImage With Circle', defectImageForIndicateDefect)
cv2.waitKey(0)