-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathharrison_corner_detector.py
239 lines (205 loc) · 8.48 KB
/
harrison_corner_detector.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
import cv2
import numpy as np
from plyfile import PlyData,PlyElement
import matplotlib.pyplot as plt
from matplotlib.backends.backend_agg import FigureCanvasAgg
from cross_junctions import cross_junctions
#================================================================================================================
# PLY DATA PROCESS
#================================================================================================================
#start with data again
plydata = PlyData.read('new_cell2.ply')
#seperate the vertexs and faces from the ply file
vertex = plydata.elements[0]
face = plydata.elements[1]
(x,y,z) = (vertex[t] for t in('x','y','z'))
(R,G,B) = (vertex[t] for t in('red','green','blue'))
index = []
for i in range(vertex.count):
if z[i] < -0.6 or z[i] > -0.4:
index.append(i)
new_x = np.delete(x,index)
new_y = np.delete(y,index)
new_z = np.delete(z,index)
new_R = np.delete(R,index)
new_G = np.delete(G,index)
new_B = np.delete(B,index)
scale = 900
new_X = np.round((new_x - min(new_x))*scale).astype(int)
new_Y = np.round((new_y - min(new_y))*scale).astype(int)
rangeX =int(round((max(new_x) - min(new_x))*scale))+1
rangeY =int(round((max(new_y) - min(new_y))*scale))+1
new_img = 255 * np.ones((rangeY,rangeX,3),dtype = np.uint8)
count = 0
print('the range of Y is:',rangeY,'the range of X is: ',rangeX)
for i in range(len(new_X)):
if new_Y[i]>int(rangeY/5) and new_Y[i]<int(rangeY*4/5):
if new_X[i]>int(rangeX/5) and new_X[i]<int(rangeX*4/5):
new_img[new_Y[i]][new_X[i]][0] = new_R[i]
new_img[new_Y[i]][new_X[i]][1] = new_G[i]
new_img[new_Y[i]][new_X[i]][2] = new_B[i]
count+=1
#plt.imshow(new_img,origin = 'lower');plt.show()
figure1 = plt.figure(figsize=(41,30))
from matplotlib.backends.backend_agg import FigureCanvasAgg
canvas = FigureCanvasAgg(figure1)
ColoUr = np.dstack((new_R,new_G,new_B))
ColoUr.resize((len(new_R),3))
ax = figure1.add_subplot(111)
ax.scatter(new_x,new_y,color = ColoUr/255,edgecolors =None)
ax.axis('off')
canvas.draw()
buf = canvas.buffer_rgba()
X= np.asarray(buf)
plt.imshow(X);plt.show()
#================================================================================================================
# IMAGE LOAD AND FEATURE DETECT
#================================================================================================================
gray = cv2.cvtColor(new_img, cv2.COLOR_RGB2GRAY)#cv2.cvtColor(new_img, cv2.COLOR_RGB2GRAY)
#for i in range(len(new_X)):
#$ if gray[new_Y[i]][new_X[i]] > 70:
# gray[new_Y[i]][new_X[i]] = int(255)
#plt.imshow(gray);plt.show()
blur = cv2.medianBlur(gray, 3)
# blur2 = cv2.medianBlur(X,3)
# gray2 = cv2.cvtColor(blur2,cv2.COLOR_RGB2GRAY)
#sharpen_kernel = np.array([[-1,-1,-1], [-1,8,-1], [-1,-1,-1]])#this detect edge
sharpen_kernel2 = np.array([[-1,-1,-1], [-1,8,-1], [-1,-1,-1]])
sharpen2 = cv2.filter2D(blur,-1,sharpen_kernel2)
#need to see the openning image and close image to see difference
#Tophat is the difference between input image and openning of the image
#sharpen_kernel = np.array([[0,-1,0], [-1,6,-1], [0,-1,0]])#sharpen
#sharpen = cv2.filter2D(sharpen2, -1, sharpen_kernel)
kernel = cv2.getStructuringElement(cv2.MORPH_CROSS, (3,3))
close = cv2.morphologyEx(sharpen2, cv2.MORPH_GRADIENT, kernel, iterations=3)
#thresh = cv2.adaptiveThreshold(sharpen, 50,cv2.ADAPTIVE_T
#
#
# , cv2.THRESH_BINARY, 3, 2)
cnts = cv2.findContours(close, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
min_area = 5
max_area = 10000
image_number = 0
for c in cnts:
area = cv2.contourArea(c)
if area > min_area and area < max_area:
x,y,w,h = cv2.boundingRect(c)
#ROI = new_img[y:y+h, x:x+h]
#cv2.imwrite('ROI_{}.png'.format(image_number), ROI)
cv2.rectangle(new_img, (x, y), (x + w, y + h), (50,255,50), 1)
image_number += 1
# plt.imshow(X);plt.show()
# plt.imshow(close);plt.show()
# #plt.imshow(thresh,origin = 'lower');plt.show()
# plt.imshow(sharpen);plt.show()
# plt.imshow(gray2);plt.show()
# plt.imshow(blur2);plt.show()
plt.imshow(new_img,origin = 'lower');plt.show()
plt.imshow(close,origin = 'lower');plt.show()
#plt.imshow(sharpen,origin = 'lower');plt.show()
plt.imshow(sharpen2,origin = 'lower');plt.show()
plt.imshow(blur,origin = 'lower');plt.show()
plt.imshow(gray,origin = 'lower');plt.show()
#boundaries
boundaries = np.zeros((2,4))
boundaries[1][0] = boundaries[1][1]= int(np.round(rangeY*(1/6)))
boundaries[1][3] = boundaries[1][2]= int(np.round(rangeY*(5/6)))
boundaries[0][0] = boundaries[0][3]= int(np.round(rangeX*(1/4)))
boundaries[0][1] = boundaries[0][2]= int(np.round(rangeX*(3/4)))
#Ipts = cross_junctions(new_img, boundaries)
"""
import cv2
import numpy as np
from plyfile import PlyData,PlyElement
import matplotlib.pyplot as plt
plydata = PlyData.read('anotherface5.ply')
#seperate the vertexs and faces from the ply file
vertex = plydata.elements[0]
face = plydata.elements[1]
(x,y,z) = (vertex[t] for t in('x','y','z'))
(R,G,B) = (vertex[t] for t in('red','green','blue'))
index = []
for i in range(vertex.count):
if z[i] < -1:
#R[i] = G[i] = B[i] = 255
index.append(i)
new_x = np.delete(x,index)
new_y = np.delete(y,index)
new_z = np.delete(z,index)
new_R = np.delete(R,index)
new_G = np.delete(G,index)
new_B = np.delete(B,index)
new_X = np.round((new_x - min(new_x))*600).astype(int)
new_Y = np.round((new_y - min(new_y))*600).astype(int)
rangeX =int(round((max(new_x) - min(new_x))*600))+1
rangeY =int(round((max(new_y) - min(new_y))*600))+1
new_img = 255 * np.ones((rangeY,rangeX,3),dtype = np.uint8)
count = 0
for i in range(len(new_X)):
new_img[new_Y[i]][new_X[i]][0] = new_R[i]
new_img[new_Y[i]][new_X[i]][1] = new_G[i]
new_img[new_Y[i]][new_X[i]][2] = new_B[i]
count+=1
plt.imshow(new_img,origin = 'lower');plt.show()
"""
def findindex(X_array,Y_array,x,y):
indexY = np.where(Y_array == y)[0]
indexX = np.where(X_array == x)[0]
result = np.intersect1d(indexY,indexX)
return result[0]
"""
#this is for extract matrix from the plot
figure1 = plt.figure(figsize=(30,20))
from matplotlib.backends.backend_agg import FigureCanvasAgg
canvas = FigureCanvasAgg(figure1)
ColoUr = np.dstack((new_R,new_G,new_B))
ColoUr.resize((len(new_R),3))
ax = figure1.add_subplot(111)
ax.scatter(new_x,new_y,color = ColoUr/255,edgecolors =None)
ax.axis('off')
canvas.draw()
buf = canvas.buffer_rgba()
X= np.asarray(buf)
plt.imshow(X)
plt.show()
"""
#finding the box
"""
gray = cv2.cvtColor(new_img, cv2.COLOR_BGR2GRAY)
blur = cv2.medianBlur(gray, 3)
sharpen_kernel = np.array([[-1,-1,-1], [-1,9,-1], [-1,-1,-1]])
sharpen = cv2.filter2D(gray, -1, sharpen_kernel)
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (5,5))
close = cv2.morphologyEx(sharpen, cv2.MORPH_TOPHAT, kernel, iterations=1)
cnts = cv2.findContours(close, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
min_area = 500
max_area = 7000
image_number = 0
for c in cnts:
area = cv2.contourArea(c)
if area > min_area and area < max_area:
x,y,w,h = cv2.boundingRect(c)
ROI = new_img[y:y+h, x:x+h]
cv2.imwrite('ROI_{}.png'.format(image_number), ROI)
cv2.rectangle(new_img, (x, y), (x + w, y + h), (36,255,12), 2)
image_number += 1
rho = 1 # distance resolution in pixels of the Hough grid
theta = np.pi / 180 # angular resolution in radians of the Hough grid
threshold = 15 # minimum number of votes (intersections in Hough grid cell)
min_line_length = 10 # minimum number of pixels making up a line
max_line_gap = 2 # maximum gap in pixels between connectable line segments
lines = cv2.HoughLinesP(close, rho, theta, threshold, np.array([]),
min_line_length, max_line_gap)
for line in lines:
for x1,y1,x2,y2 in line:
cv2.line(new_img,(x1,y1),(x2,y2),(255,0,0),1)
plt.imshow(new_img,origin = 'lower');plt.show()
#boundaries
boundaries = np.zeros((2,4))
boundaries[1][0] = boundaries[1][1]= np.round(rangeY*(1/6)).astype(int)
boundaries[1][3] = boundaries[1][2]= np.round(rangeY*(5/6)).astype(int)
boundaries[0][0] = boundaries[0][3]= np.round(rangeX*(1/4)).astype(int)
boundaries[0][1] = boundaries[0][2]= np.round(rangeX*(3/4)).astype(int)
"""