-
Notifications
You must be signed in to change notification settings - Fork 11
/
ActiveShapeModel.py
164 lines (109 loc) · 4.64 KB
/
ActiveShapeModel.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
# coding: utf-8
# In[46]:
import MatchingModelPoints as match
import FitFunction as fit
import FileManager as fm
import numpy as np
import matplotlib.pyplot as plt
import Image_preperation as prep
import PCA_analysis as PCA
import ActiveFitContour as af
import time
def active_shape_scale_n_times(img, tooth_points, pca_tooth, length, scale, n_times):
points = [0] * (n_times+1)
points[0] = tooth_points
scaled_img , scaled_tooth_points = scaling(img, tooth_points, 1/scale)
edge_img = prep.calc_external_img_active_contour(scaled_img)
new_points = active_shape(edge_img, scaled_tooth_points, pca_tooth, length)
for i in range(n_times):
new_points = active_shape(edge_img, new_points, pca_tooth, length)
points[i+1] = np.around(new_points*scale)
return points
def active_shape_scale(img, tooth_points, pca_tooth, length, scale):
scaled_img , scaled_tooth_points = scaling(img, tooth_points, 1/scale)
edge_img = prep.calc_external_img_active_contour(scaled_img)
new_points = active_shape(edge_img, scaled_tooth_points, pca_tooth, length)
new_img, new_scaled_points = scaling(scaled_img, new_points, scale)
return new_scaled_points
def active_shape(edge_img, tooth_points, pca_tooth, length, alfa, activeFitON, MatchingON=True):
if(activeFitON):
new_points = af.active_contour(tooth_points, edge_img, length, alfa)
else:
new_points, error = fit.fit_measure(tooth_points, length, edge_img)
if(MatchingON):
b, pose_param = match.match_model_points(new_points, pca_tooth)
x = match.generate_model_point(b, pca_tooth)
y = match.inv_transform(x.reshape(40,2),pose_param)
else:
y = new_points
return y
def active_shape_n_times(edge_img, tooth_points, pca_tooth, length,alfa, n_times):
points = [0] * (n_times+1)
points[0] = tooth_points
for i in range(n_times):
points[i+1] = active_shape(edge_img, points[i], pca_tooth, length, alfa)
return points
def preperation_all(radiograph, all_landmarks):
#median = prep.median_filter(radiograph)
# edge_img = prep.edge_detection_low(median)
edge_img = prep.calc_external_img_active_contour(radiograph)
pcas_tooth = PCA.PCA_analysis_all(all_landmarks, None)
return edge_img, pcas_tooth
def preperation(radiograph, tooth_variations):
#median = prep.median_filter(radiograph)
# edge_img = prep.edge_detection_low(median)
edge_img = prep.calc_external_img_active_contour(radiograph)
pca_tooth = PCA.PCA_analysis(tooth_variations, None)
return edge_img, pca_tooth
def scaling(img, points, scale):
new_img, new_points = fm.resolution_scale(img, points, scale)
return new_img, new_points
def show_evolution(img, points_list):
plt.figure()
fig, ax = plt.subplots(figsize=(15, 7))
n = len(points_list)
hn = int(n/2)
for i, landmark in enumerate(points_list):
plt.subplot(2, hn, i+1)
plt.imshow(img)
plt.xticks(())
plt.yticks(())
plt.plot(landmark[:,0], landmark[:,1], 'ro', markersize=1)
plt.show()
def testings():
fig, ax = plt.subplots(figsize=(15, 15))
plt.imshow(radiograph)
plt.plot(new_points[:,0], new_points[:,1], 'ro', markersize=1)
plt.show()
img = fm.load_img_piece()
tooth = fm.load_tooth_of_piece(4)
landmarks = fm.load_landmarks_std()
tooth_variations = landmarks[:,4]
edge_img, pca_tooth = preperation(img, tooth_variations)
fm.show_with_points(img, tooth)
#points_array = active_shape_scale_n_times(img, tooth, pca_tooth, 15, 4, 10)
points_array = active_shape_n_times(edge_img, tooth, pca_tooth, 10, 20 ,9)
show_evolution(img, points_array)
def show_with_points(img, points):
fig, ax = plt.subplots(figsize=(15, 15))
plt.imshow(img)
plt.plot(points[:,0], points[:,1], 'ro', markersize=1)
plt.show()
# In[2]:
if __name__ == "__main__":
teeth = np.load('initial_position.npy')
tooth = teeth[0,0]
tooth = tooth/0.3
radiographs = fm.load_radiographs()
radiograph = radiographs[0]
landmarks = fm.load_landmarks_std()
tooth_variations = landmarks[:,0]
edge_img, pca_tooth = preperation(radiograph, tooth_variations)
fig, ax = plt.subplots(figsize=(15, 15))
plt.imshow(radiograph)
plt.plot(tooth[:,0], tooth[:,1], 'ro', markersize=1)
plt.show()
points_list = active_shape_n_times(edge_img, tooth, pca_tooth, 10, 1,1)
#show_evolution(radiograph,points_list)
for points in points_list:
show_with_points(radiograph, points)