-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathJustCPDRegistration.m
138 lines (124 loc) · 4.2 KB
/
JustCPDRegistration.m
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
% Created and Developed by Amin Alam - 29th hun 2021
clc
clear
close all
% loading datas
SN = 18;
file_path = "./Project_Stuff/Datas/";
[V, V_label] = NiiLoader(SN,file_path);
tool = imtool3D((V));
setMask(tool,(V_label));
% saveas(gcf,"./report/images/Subject"+num2str(SN)+".png")
% showing point clouds
close all
figure
ptCloud = Pcloudmaker(V_label);
pcshow(ptCloud);
xlabel('X')
ylabel('Y')
zlabel('Z')
view(-46,60)
colormap jet
% saveas(gcf,"./report/images/MainPCSubject"+num2str(SN)+".png")
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Atlas preview
close all
file_path = "./Healthy_sample/";
VA = double(niftiread(file_path+"00"+".nii"));
VA_label = double(niftiread(file_path+"00_mask.nii"));
tool = imtool3D((VA));
setMask(tool,(VA_label));
ptCloud_A = Pcloudmaker(VA_label);
pcshow(ptCloud_A);
xlabel('X')
ylabel('Y')
zlabel('Z')
view(-46,60)
colormap jet
close all
ptCloud_P = Pcloudmaker(V_label);
ptCloud_A = Pcloudmaker(VA_label);
% downsampling
GridStep = 5;
moving = pcdownsample(ptCloud_P,'gridAverage',GridStep);
fixed = pcdownsample(ptCloud_A,'gridAverage',GridStep);
tform = pcregistercpd(moving, fixed);
movingReg = pctransform(moving, tform);
% seperating vertebras
Img = V_label;
VertebraNumbers = 15:1:30;
L = 0;
for i = VertebraNumbers
[x, y, z] = ind2sub(size(Img), find(Img == i));
xyzPoints = [x, y, z];
ptCloud = pointCloud(xyzPoints);
if ~isempty(ptCloud.Location)
SeperateVertebras.(sprintf("Vertebra_%i", i)).ptCloud = pcdownsample(ptCloud,'gridAverage',GridStep);
L = L + length(SeperateVertebras.(sprintf("Vertebra_%i", i)).ptCloud.Location);
SeperateVertebras.(sprintf("Vertebra_%i", i)).ptCloud_rotated = SeperateVertebras.(sprintf("Vertebra_%i", i)).ptCloud;
SeperateVertebras.(sprintf("Vertebra_%i", i)).number = i;
end
end
Img = VA_label;
VertebraNumbers = 15:1:30;
for i = VertebraNumbers
[x, y, z] = ind2sub(size(Img), find(Img == i));
xyzPoints = [x, y, z];
ptCloud = pointCloud(xyzPoints);
if ~isempty(ptCloud.Location)
SeperateVertebrasA.(sprintf("Vertebra_%i", i)).ptCloud_rotated = pcdownsample(ptCloud,'gridAverage',GridStep);;
SeperateVertebrasA.(sprintf("Vertebra_%i", i)).number = i;
end
end
registered_pointCloud = movingReg;
Locations = movingReg.Location;
Locations = interparc(L,Locations(:,1),Locations(:,2),Locations(:,3),'spline');
movingReg = pointCloud(Locations);
SeperateVertebrasF = Segmenter(movingReg.Location, SeperateVertebras, SeperateVertebrasA);
ptCloud_A_transformed = pcdownsample(ptCloud_A,'gridAverage',GridStep);
ptCloud = pcdownsample(ptCloud_P,'gridAverage',GridStep);
figure
pcshowpair(fixed, moving, 'MarkerSize',50)
xlabel('X')
ylabel('Y')
zlabel('Z')
legend({'Atlas','Subject'},'TextColor','w')
view(-46,-10)
% saveas(gcf,"./report/images/Subject"+num2str(SN)+"Original.png")
figure
pcshowpair(fixed, movingReg, 'MarkerSize',50)
xlabel('X')
ylabel('Y')
zlabel('Z')
legend({'Atlas','Subject'},'TextColor','w')
view(-46,-10)
% saveas(gcf,"./report/images/Subject"+num2str(SN)+"AfterJustCPD.png")
clc
fn1 = fieldnames(SeperateVertebras);
fn2 = fieldnames(SeperateVertebrasA);
for i = 15:1:30
name = "Vertebra_"+num2str(i);
if sum(ismember(fn1,name)) && sum(ismember(fn2,name))
moving = SeperateVertebrasF.(sprintf("Vertebra_%i", i)).pointCloud;
fixed = SeperateVertebrasA.(sprintf("Vertebra_%i", i)).ptCloud_rotated;
LocsA_R = fixed.Location;
LocsP_R = moving.Location;
cp = CommonPoints(LocsA_R, LocsP_R);
SeperateVertebrasF.(sprintf("Vertebra_%i", i)).CommonPointsWithAtlas = cp;
end
end
% Dice score
clc
DiceScore = DS(SeperateVertebrasF,SeperateVertebrasA)
% Hausdorff Distance
HausdorffScore = HD(SeperateVertebrasF,SeperateVertebrasA)
% Average Surface Distance
ASD_Score = ASD(SeperateVertebrasF.PCloud, ptCloud_A_transformed)
% intersection of vertebras
VertebraIntersectsVolume = VertebraIntersect_calc(SeperateVertebrasF,SeperateVertebrasA)
% jacobian of displacemnet field
try
DisplacemnetField = registered_pointCloud.Location - ptCloud.Location;
JacobianMatofDisplacemnetField = JacobianMatCalc(DisplacemnetField)
catch
disp('there is a prolem with your python entrepretor. check it using pyenv command')
end