-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsulci_reduction_efficient.m
More file actions
237 lines (178 loc) · 5.96 KB
/
sulci_reduction_efficient.m
File metadata and controls
237 lines (178 loc) · 5.96 KB
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
%% read each subject OFC image as a 3D block in a 4D matrix holding all subjects
% identify our sulci of interst, convert to point coords
% reverse x coords of $ hemisphere, convert point coord lists to point clouds
filelist = dir("/Users/willsnyder/Downloads/images/itksnap*.nii");
prefix = "/Users/willsnyder/Downloads/images/";
clouds.all = cell(1, 2*length(filelist));
for i = 1:length(filelist)
disp(i)
fname = filelist(i).name;
%folder = fname(1:6);
fname = char(prefix + fname);
V = spm_vol(fname);
[P, XYZ] = spm_read_vols(V);
left = P(1:(end/2), :, :);
right = P((end/2+1:end), : , :);
[xl, yl, zl ] = ind2sub(size(left), find(left == 3));
[xr, yr, zr ] = ind2sub(size(right),find(right == 3));
left_coords = [xl yl zl];
right_coords = [-xr yr zr];
ptcloudl = pointCloud(left_coords);
ptcloudr = pointCloud(right_coords);
%disp((2*(i-1))+1)
%disp(2*i)
clouds.all{(2*(i-1))+1} = {ptcloudl};
clouds.all{2*i} = {ptcloudr};
end
%% use parfor loop to, pairwise, compute RMSE, to be stored in pre-allocated matrix
tic;
distance_mat = zeros(length(clouds.all),length(clouds.all));
for i = 1:length(clouds.all)
disp(i)
cloudi = clouds.all(i);
cloudi = cloudi{1,1};
if class(cloudi) ~= "pointCloud"
cloudi = cloudi{1,1};
end
for j = 1:length(clouds.all)
cloudj = clouds.all(j);
cloudj = cloudj{1,1};
if class(cloudj) ~= "pointCloud"
cloudj = cloudj{1,1};
end
if i <= j
[tform, movingreg, rmse] = pcregistericp(cloudi, cloudj);
distance_mat(i,j) = rmse;
distance_mat(j,i) = rmse;
end
if i==7 && j ==8
cloudy = movingreg;
cloudz = cloudj;
end
end
end
toc;
ptconcat = pcmerge(cloudy, cloudz,1);
figure; pcshow(ptconcat); figure; pcshow(cloudy); figure; pcshow(cloudz)
%% Perform isomap learning on RMSE matrix, retain primary axis
vals = zeros(1,16);
for k = 5:20
[mappedX, mapping] = isomap(distance_mat, 1, k);
if length(mappedX) == size(distance_mat,1)
disp("Mapping Successful")
else
disp("Error in nearest neighbor calculations. Please select a different k")
end
vals(k) = mapping.val;
end
plot(vals)
[~,ind] = min(vals);
[mappedX, mapping] = isomap(distance_mat, 1, ind);
if length(mappedX) == size(distance_mat,1)
disp("Mapping Successful")
else
disp("Error in nearest neighbor calculations. Please select a different k")
end
%% other isomap approach
residuals = zeros(1,20);
for k = 5:20
[Y, R, E] = Isomap(distance_mat, 'k', k);
residuals(k) = R(1);
end
plot(residuals)
[~,k] = min(residuals)
%%
[Y, R, E] = Isomap(distance_mat, 'k', 10);
list = Y.coords{1};
%% align all sulci to template, "neutral" sulcus
clouds.template = cell(1,2*length(filelist));
[~, neutral_loc ] = min(sum(distance_mat));
for i = 1:length(clouds.all)
disp(i)
cloudi = clouds.all(i);
cloudi = cloudi{1,1};
if class(cloudi) ~= "pointCloud"
cloudi = cloudi{1,1};
end
%%change to not be hardcoded
cloudj = clouds.all(neutral_loc);
%cloudj = clouds.all(17);
cloudj = cloudj{1,1};
if class(cloudj) ~= "pointCloud"
cloudj = cloudj{1,1};
end
[tform, movingreg, rmse] = pcregistericp(cloudi, cloudj);
clouds.template{i} = {movingreg};
end
%% look at distribution along primary axis, set number of evenly spaced bins
%-100 to 150 is the range here -- length = 250, make 15 bins
%hist(mappedX)
list = list';
imgs = zeros(size(P,1) + 20 ,size(P,2) + 20,size(P,3),9);
img_count = 1;
%for i = -100:25:150
for i = -1.5:.5:2.5
disp(i)
dist = power((power(abs(list-i), 2) /1000),-1);
%dist = power((power(abs(mappedX-i), 2) /1000),-1);
for j = 1:length(clouds.all)
cloudj = clouds.template(j);
cloudj = cloudj{1,1};
if class(cloudj) ~= "pointCloud"
cloudj = cloudj{1,1};
end
xyz = round(cloudj.Location);
for s = 1:size(xyz,1)
x = xyz(s,1);
y = xyz(s,2);
z = xyz(s,3);
imgs(x,y,z,img_count) = imgs(x,y,z,img_count) + 1*dist(j);
end
end
img_count = img_count + 1;
end
%% compute weighted average image of each bin
% the average isomap point in each bin will be the center, and weight of
% each image will be inversely proportional to the squared distance from
% each center. Each image must first be registered to the center before
% this is computed
% nearest neighbors approach
imgs = zeros(size(P,1) + 20 ,size(P,2) + 20,size(P,3),9);
img_count = 1;
for i = -1.5:.5:2.5
dist = power((power(abs(list-i), 2) /1000),-1);
[~ , locs] = maxk(dist,20);
for j = 1:length(locs)
cloudj = clouds.template(locs(j));
cloudj = cloudj{1,1};
if class(cloudj) ~= "pointCloud"
cloudj = cloudj{1,1};
end
xyz = round(cloudj.Location);
for s = 1:size(xyz,1)
x = xyz(s,1);
y = xyz(s,2);
z = xyz(s,3);
imgs(x,y,z,img_count) = imgs(x,y,z,img_count) + 1;
end
end
img_count = img_count + 1;
end
%% add the highest threshold that does not allow for holes, save averaged images, convert to mesh
imgs_gauss_thresh = imgs;
for i = 1:size(imgs,4)
disp(i)
imgs_gauss_thresh(:,:,:,i) = imgaussfilt3(imgs_gauss_thresh(:,:,:,i),2);
%imgs_gauss_thresh(:,:,:,i) = imgaussfilt3(imgs_gauss_thresh(:,:,:,i));
%icatb_write_nifti_data(char("moving_avg_sulc" + num2str(i) + ".nii"),V, imgs_gauss_thresh(:,:,:,i))
end
%% testing
for j = flip(.1:.1:7)
disp("j")
disp(j)
im_thresh = imgs_gauss(:,:,:,i) > j;
invert = imcomplement(im_thresh);
conn = bwconncomp(invert);
disp("conn comps")
disp(conn.NumObjects)
end