-
Notifications
You must be signed in to change notification settings - Fork 0
/
active_contour.py
282 lines (210 loc) · 7.43 KB
/
active_contour.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
import numpy as np
import cv2
import matplotlib.pyplot as plt
from skimage import filters
import os
import moviepy.video.io.ImageSequenceClip
import sys
from skimage.filters import threshold_yen
# -------------------------
def plots(x, y, img, n):
image3 = np.copy(img)
color = (0, 255, 0)
for i in range(0, n):
if(i != 99):
start_point = (int(y[i]), int(x[i]))
end_point = (int(y[i+1]), int(x[i+1]))
if(i == 99):
start_point = (int(y[99]), int(x[99]))
end_point = (int(y[0]), int(x[0]))
image3 = cv2.line(image3, start_point, end_point, color, 3)
color = (255, 0, 0)
for i in range(0, n):
center_coordinates = (int(y[i]), int(x[i]))
image3 = cv2.circle(
image3, center_coordinates, 2, color, 2)
image3 = cv2.cvtColor(image3, cv2.COLOR_RGB2BGR)
return image3
# ----------------------------
def decoder(num):
x_m = 0
y_m = 0
if(num == 0):
x_m = -1
y_m = -1
if(num == 1):
x_m = -1
y_m = 0
if(num == 2):
x_m = -1
y_m = 1
if(num == 3):
x_m = 0
y_m = -1
if(num == 4):
x_m = 0
y_m = 0
if(num == 5):
x_m = 0
y_m = 1
if(num == 6):
x_m = 1
y_m = -1
if(num == 7):
x_m = 1
y_m = 0
if(num == 8):
x_m = 1
y_m = 1
return x_m, y_m
# ----------------------------
def gradian(matrix):
grad = cv2.Canny(matrix, 100, 200)
grad = filters.gaussian(grad, 9)
thresh_min = threshold_yen(grad)
condition = (grad > thresh_min)
grad *= condition
grad /= np.max(grad)
return -grad
# ------------------
def find_best_iteration(gradian_min, x_cor, y_cor, img: np.ndarray, cost_matrix):
index_x_matrix = np.zeros([100, 9])
index_y_matrix = np.zeros([100, 9])
number_matrix = np.ones([100, 9])*4
cost_matrix_copy = np.copy(cost_matrix[0, :])
cost_matrix[0, :] = 0
x_roll = np.roll(x_cor, 1)
y_roll = np.roll(y_cor, 1)
d = np.mean(np.sqrt(((x_cor-x_roll)**2+(y_cor-y_roll)**2)))
x_center = np.mean(x_cor)
y_center = np.mean(y_cor)
l = np.mean(np.sqrt(((x_cor-x_center)**2+(y_cor-y_center)**2)))
number_of_centers = 100
for g in range(1, number_of_centers):
x_instant = x_cor[g]
y_instant = y_cor[g]
for gg in range(0, 9):
x_m1, y_m1 = decoder(gg)
for ggg in range(0, 9):
x_m2, y_m2 = decoder(ggg)
ex_temp = \
(gradian_min[int(x_instant+x_m1), int(y_instant+y_m1)])
inter_temp1 = (
((x_instant+x_m1-(x_cor[g-1]+x_m2))**2+(y_instant+y_m1-(y_cor[g-1]+y_m2))**2)-d)**2
inter_temp2 = (
((x_instant+x_m1-(x_center))**2+(y_instant+y_m1-(y_center))**2)-1/2*l)**2
inter_temp2 *= 2/((np.exp(20*(-ex_temp)))+1)
total = 10**(-5)*inter_temp1+10**(-6)*inter_temp2 + \
10*ex_temp+cost_matrix[g-1, ggg]
if total < cost_matrix[g, gg]:
cost_matrix[g, gg] = total
number_matrix[g, gg] = ggg
index_x_matrix[g, gg] = x_m2
index_y_matrix[g, gg] = y_m2
# make it closed loop
x_instant = x_cor[0]
y_instant = y_cor[0]
cost_matrix[0, :] = cost_matrix_copy
c = 0
for gg in range(0, 9):
x_m1, y_m1 = decoder(gg)
for ggg in range(0, 9):
x_m2, y_m2 = decoder(ggg)
ex_temp = \
(gradian_min[int(x_instant+x_m1), int(y_instant+y_m1)])
inter_temp1 = (
((x_instant+x_m1-(x_cor[99]+x_m2))**2+(y_instant+y_m1-(y_cor[99]+y_m2))**2)-d)**2
inter_temp2 = (
((x_instant+x_m1-(x_center))**2+(y_instant+y_m1-(y_center))**2)-1/2*l)**2
inter_temp2 *= 2/((np.exp(20*(-ex_temp)))+1)
total = 10**(-5)*inter_temp1+10**(-6)*inter_temp2 + \
10*ex_temp+cost_matrix[99, ggg]
if total <= cost_matrix[0, gg]:
c += 1
cost_matrix[0, gg] = total
index_x_matrix[0, gg] = x_m2
index_y_matrix[0, gg] = y_m2
number_matrix[0, gg] = ggg
if(c >= 1):
indice = np.where(cost_matrix[0, :] == min(cost_matrix[0, :]))[0][0]
elif(c == 0):
indice = 4
x_m, y_m = decoder(indice)
number = number_matrix[0, indice]
x_cor[0] = x_m+x_cor[0]
y_cor[0] = y_m+y_cor[0]
for m in range(99, 0, -1):
if(m == 99):
x_m, y_m = decoder(number)
x_cor[m] = x_m+x_cor[m]
y_cor[m] = y_m+y_cor[m]
number = number_matrix[int(m), int(number)]
else:
x_m, y_m = decoder(number)
x_cor[int(m)] = x_m+x_cor[int(m)]
y_cor[int(m)] = y_m+y_cor[int(m)]
number = number_matrix[int(m), int(number)]
return x_cor, y_cor, cost_matrix
# -------------------------------
# read image
image = cv2.imread('tasbih.jpg')
image = np.array(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
img = np.copy(image)
plt.imshow(img)
# getting boarders of contour (should be distribute uniformly)
plt.title('choose 6-15 points "all around " the tasbih .\n please choose it all around the object(not just one side)!\nafter choose press ENTER')
points = plt.ginput(0, 0)
plt.close()
points = np.uint(np.array((points)))
x_cor = np.array(points[:, 1])
y_cor = np.array(points[:, 0])
# -------------
x_center = np.mean(x_cor) # center of circle (x)
y_center = np.mean(y_cor) # center of circle (y)
# ------------
y_max = max(abs(y_cor-y_center)) # radius in y axis
x_max = max(abs(x_cor-x_center)) # radius in x axis
deg = np.linspace(0, 2 * np.pi, 100) # to select vertices unifromly
x_new_cent = x_center + x_max * np.sin(deg)
y_new_cent = y_center + y_max * np.cos(deg)
first_image = plots(x_new_cent, y_new_cent, img, 100)
first_image = cv2.cvtColor(first_image, cv2.COLOR_BGR2RGB)
plt.imshow(first_image)
plt.title('close the window to continue the proccess')
plt.show()
gradian_minus = gradian(img)
# Create directory for frames to save
# -------------------------
dirName = 'pic'
try:
# Create target Directory
os.mkdir(dirName)
print("Directory ", dirName, " Created ")
except FileExistsError:
print("Directory ", dirName, " already exists")
file = sys.argv[0]
dirname = os.path.dirname(file)
dirname = dirname + '/' + 'pic'
path = dirname
# ---------------------------------
for f in range(0, 400):
if(f == 0):
cost = np.ones([100, 9])*np.inf
x, y, cost = find_best_iteration(
gradian_minus, x_new_cent, y_new_cent, image, cost)
frame = plots(x, y, img, 100)
cv2.imwrite(os.path.join(path, f'{f}.png'), frame)
image_folder = dirname
fps = 3
image_files = []
# make gif
for i in range(0, 400, 5):
image_files.append(image_folder + '/' + str(i) + ".png")
clip = moviepy.video.io.ImageSequenceClip.ImageSequenceClip(
image_files, fps=fps)
clip.write_videofile('contour.mp4')
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
plt.imsave('res11.jpg', frame)
plt.imshow(frame)
plt.title('final_image')
plt.show()