-
Notifications
You must be signed in to change notification settings - Fork 11
/
ActiveContour.py
290 lines (199 loc) · 7.85 KB
/
ActiveContour.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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
# coding: utf-8
# In[5]:
import numpy as np
import matplotlib.pyplot as plt
from skimage.color import rgb2gray
from skimage.filters import gaussian
import scipy
import cv2
from scipy import ndimage
import Image_preperation as prep
import FileManager as fm
def calc_internal(p1,p2):
if (np.array_equal(p1,p2)):
return 100
return np.sum( (p2 - p1)**2 )
def calc_internal_mean(p1,p2, mean):
dist = scipy.spatial.distance.euclidean(p1,p2)
diff = (mean - dist)**2
return diff
def calc_mean(points):
size = len(points)
p1 = points[-1]
p2 = points[0]
mean_sum = scipy.spatial.distance.euclidean(p1,p2)
for i in range(size-1):
p1 = points[i]
p2 = points[i+1]
mean_sum += scipy.spatial.distance.euclidean(p1,p2)
return mean_sum / size
# def calc_external_img(img):
# img = rgb2gray(img)
# sobelx64f = cv2.Sobel(img,cv2.CV_64F,1,0,ksize=5)
# abs_sobel64f = np.absolute(sobelx64f)
# sobelx = np.uint8(abs_sobel64f)
# sobely64f = cv2.Sobel(img,cv2.CV_64F,0,1,ksize=5)
# abs_sobel64f = np.absolute(sobely64f)
# sobely = np.uint8(abs_sobel64f)
# return -(sobelx + sobely)
def calc_external_img2(img):
img = np.array(img, dtype=np.uint16)
kx = np.array([[-1,0,1],[-2,0,2],[-1,0,1]])
Gx = cv2.filter2D(img,-1,kx)
ky = np.array([[-1,-2,-1],[0,0,0],[1,2,1]])
Gy = cv2.filter2D(img,-1,ky)
G = np.sqrt(Gx**2 + Gy**2)
return -G
def calc_external(p, external_img):
p = p.astype(int)
return external_img[p[0],p[1]]
def calc_energy(p1, p2, external_img, alpha):
internal = calc_internal(p1,p2)
external = calc_external(p1, external_img)
return internal + alpha * external
def calc_internal2(p1,p2,p3, alpha, beta):
distance = alpha * (p3 - p2)**2
curvature = beta * (p3 - 2*p2 + p1)**2
return np.sum( distance + curvature)
def calc_energy2(p1, p2, p3, external_img, alpha, beta, gamma):
internal = calc_internal2(p1,p2, p3,alpha, beta)
external = calc_external(p3, external_img)
return internal + gamma * external
def calc_energy3(p1, p2, mean, external_img, alpha):
internal = calc_internal_mean(p1,p2, mean)
external = calc_external(p2, external_img)
return internal + alpha * external
def get_point_state(point, number, pixel_width):
n=0
for i in range(-pixel_width , pixel_width+1):
for j in range(-pixel_width , pixel_width+1):
if n == number:
return np.array([point[0]+i , point[1]+j])
n +=1
return
def unpack(number, back_pointers, points, pixel_width):
size = len(points)
new_points = np.empty((size,2))
new_points[-1] = get_point_state(points[-1],number, pixel_width)
pointer = back_pointers[-1,number]
for i in range(size-2, -1, -1):
new_points[i] = get_point_state(points[i],pointer, pixel_width)
pointer = back_pointers[i,pointer]
return new_points
#https://courses.engr.illinois.edu/cs447/fa2017/Slides/Lecture07.pdf
def viterbi(points, img, pixel_width, alpha):
size = len(points)
num_states = (2*pixel_width +1)**2
center = int((num_states-1)/2)
trellis = np.empty((size, num_states), dtype=np.float16)
back_pointers = np.empty((size, num_states), dtype=int)
external_img = calc_external_img2(img)
#init
trellis[0,:] = np.zeros((num_states))
back_pointers[0,:] = np.full((num_states),center)
mean = calc_mean(points)
#recursion
for i in range(1, size):
for t in range(num_states):
trellis[i,t] = np.inf
for d in range(num_states):
p1 = get_point_state(points[i-1], d, pixel_width)
p2 = get_point_state(points[i],t, pixel_width)
energy_trans = calc_energy(p1, p2, external_img, alpha)
# energy_trans = calc_energy3(p1, p2, mean, external_img, alpha)
tmp = trellis[i-1,d] + energy_trans
if(tmp < trellis[i,t]):
trellis[i,t] = tmp
back_pointers[i,t] = d
#find best
t_best, vit_min = 0, np.inf
for t in range(num_states):
if(trellis[size-1, t] < vit_min):
t_best = t
vit_min = trellis[size-1, t]
return unpack(t_best, back_pointers, points, pixel_width)
def viterbi2(points, img, pixel_width, alpha, beta, gamma):
size = len(points)
num_states = (2*pixel_width +1)**2
center = int((num_states-1)/2)
trellis = np.empty((size, num_states), dtype=np.float16)
back_pointers = np.empty((size, num_states), dtype=int)
external_img = calc_external_img2(img)
#init
trellis[0:2,:] = np.zeros((2,num_states))
back_pointers[0:2,:] = np.full((2,num_states),center)
#recursion
for i in range(2, size):
for t in range(num_states):
trellis[i,t] = np.inf
for d1 in range(num_states):
for d2 in range(num_states):
p1 = get_point_state(points[i-2], d1, pixel_width)
p2 = get_point_state(points[i-1], d2, pixel_width)
p3 = get_point_state(points[i],t, pixel_width)
energy_trans = calc_energy2(p1, p2,p3, external_img, alpha, beta, gamma)
tmp = trellis[i-1,d2] + energy_trans
if(tmp < trellis[i,t]):
trellis[i,t] = tmp
back_pointers[i,t] = d2
#find best
t_best, vit_min = 0, np.inf
for t in range(num_states):
if(trellis[size-1, t] < vit_min):
t_best = t
vit_min = trellis[size-1, t]
return unpack(t_best, back_pointers, points, pixel_width)
def active_contour(points, img, max_loop, pixel_width, alpha):
old_points = points
for i in range(max_loop):
new_points = viterbi(old_points, img, pixel_width, alpha)
if np.array_equal(new_points, old_points):
print(i)
break
#old_points = new_points
head, tail = np.split(new_points, [6])
old_points = np.append(tail, head).reshape(new_points.shape)
return new_points
def active_contour2(points, img, max_loop, pixel_width, alpha, beta, gamma):
old_points = points
for i in range(max_loop):
new_points = viterbi(old_points, img, pixel_width, alpha, beta, gamma)
if np.array_equal(new_points, old_points):
print(i)
break
#old_points = new_points
head, tail = np.split(new_points, [1])
old_points = np.append(tail, head).reshape(new_points.shape)
return new_points
def resolution_downscale(img, resize):
x, y = img.shape
xn = int(x/resize)
yn = int(y/resize)
return cv2.resize(img, (yn ,xn))
def previous_test():
dir_radiographs = "_Data\Radiographs\*.tif"
radiographs = fm.load_files(dir_radiographs)
radiograph = radiographs[0]
init = np.load("initial_position.npy")
down_sample = 5
tooth = init[0,4,:,:]/0.3
#tooth = tooth/down_sample
radiograph_pre = pre_processing(radiograph)
img = resolution_downscale(radiograph_pre,down_sample)
fig, ax = plt.subplots(figsize=(15, 15))
plt.imshow(radiograph)
plt.plot(tooth[:,0], tooth[:,1], 'ro', markersize=1)
plt.show()
def test_module():
piece = fm.load_img_piece()
tooth = fm.load_tooth_of_piece(0)
ext = calc_external_img2(piece)
fm.show_with_points(ext, tooth)
img, stooth = fm.resolution_scale(piece, tooth, 1/6)
ext = calc_external_img2(img)
fm.show_with_points(ext, stooth)
new_tooth = active_contour(stooth, img, 1, 3, 1)
fm.show_with_points(ext, new_tooth)
# In[6]:
if __name__ == "__main__":
test_module()