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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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
%% MATLAB Script for displaying 2D and 3D plots of reconstructed B-Mode data in
%  files named "*3d_recon.mat"

% See "multiframe_bmode_plot.m" for displaying real-time B-Mode data in files 
% named "*rt_recon.mat"

% See "image_recon_3d.m" and "image_recon_rt.m" for reconstructing B-Mode data from
% raw RF data in files names "*_raw.zip"

clear;
% clc;
% close;

%% Image params
log_compression = true;
dB_range = 30;
orthogonal = false;
slice_dir = 'yz';
video_out = false;

%% Load saved B-Mode complex data for plotting in MATLAB/
%    exporting into image data for Origin

%Data file path
filepath = './datasets_neck/';

%File name
filename = 'neck_yz_3d_recon';

%Load Complex data
bmode_data_struct = load(strcat(filepath, filename, '.mat'));

%Check how many fields in struct for 2-D/3-D
num_dim = length(fieldnames(bmode_data_struct))-1;

%Assign variables
bmode_data = bmode_data_struct.bmode_data;

zI = bmode_data_struct.zI;
if (num_dim==2 & slice_dir=='xz')
    xI = bmode_data_struct.xI;
elseif (num_dim==2 && slice_dir=='yz')
    xI = bmode_data_struct.yI;
elseif(num_dim == 3)
    xI = bmode_data_struct.xI;
    yI = bmode_data_struct.yI;
    grid_res = size(bmode_data, 1);
    grid_res_orth = size(bmode_data, 3);
end

%% Imaging

if(num_dim == 2)
%% Plot 2-D images
    bmode_data = abs(bmode_data);

    if(log_compression)
        %Log compression
        b_mode_temp = 20*log10(bmode_data/max(max(bmode_data)));
        b_mode = (b_mode_temp+dB_range).*(b_mode_temp>-dB_range);
        b_mode = uint8(255*b_mode/dB_range);
        
        %Plot Image
        figure();
        
        if(slice_dir == "xz")
            hP = pcolor(xI*1e2,zI*1e2,b_mode);
        elseif(slice_dir == "yz")
            hP = pcolor(yI*1e2,zI*1e2,b_mode);
        end
        set(gca, 'XAxisLocation', 'bottom', 'XTickMode', 'auto');
        shading interp, axis ij tight
        c = colorbar;
        clim([0 255]);
        c.YTick = [0 255];
        c.YTickLabel = {'0 dB',strcat(num2str(dB_range), ' dB')};
        ylabel('[cm]')
        xlabel('[cm]')
        set(gca,'XColor','none','box','off')
        colormap hot
        % title(test_name, 'Interpreter', 'none');
        % view(2)
        % hP.DataTipTemplate.DataTipRows(3).Value   = 'CData';
    else
        %Normalization
        b_mode_temp = bmode_data/max(max(bmode_data));
        
        %Plot Image
        figure();
        
        if(slice_dir == "xz")
            hP = pcolor(xI*1e2,zI*1e2,b_mode_temp);
        elseif(slice_dir == "yz")
            hP = pcolor(yI*1e2,zI*1e2,b_mode_temp);
        end
        shading interp, axis equal ij tight
        c = colorbar;
        xlabel('[cm]')
        clim([0 1]);
        c.YTick = [0 1];
        c.YTickLabel = {'0','1'};
        ylabel('[cm]')
        set(gca,'XColor','none','box','off')
        colormap hot
        % title(test_name, 'Interpreter', 'none');
        % view(2)
        % hP.DataTipTemplate.DataTipRows(3).Value   = 'CData';
    end   

elseif(num_dim == 3)
%% Plot 3-D images
    
    %Magnitude of complex number
    bmode_data = abs(bmode_data);
    
    if(log_compression)
        %Log compression
        b_mode_temp = 20*log10(bmode_data/max(bmode_data,[],'all'));
        b_mode = (b_mode_temp+dB_range).*(b_mode_temp>-dB_range);
        b_mode = uint8(255*b_mode/dB_range);

        %Plot real-tim image
        figure();
    
        if(~orthogonal)
            % for i=1:grid_res_orth
            for i=27
                % b_mode_temp = 20*log10(bmode_data(:, :, i)/max(bmode_data(:, :, i), [],'all'));
                % b_mode(:, :, i) = (b_mode_temp+dB_range).*(b_mode_temp>-dB_range);
                % b_mode(:, :, i) = uint8(255*b_mode(:, :, i)/dB_range);
                % figure();
                if(strcmp(slice_dir,'xz'))
                    hP = pcolor(xI(:,:,i)*1e2,zI(:,:,i)*1e2,b_mode(:,:,i));
                elseif(strcmp(slice_dir,'yz'))
                    hP = pcolor(yI(:,:,i)*1e2,zI(:,:,i)*1e2,b_mode(:,:,i));
                end
                shading interp, axis equal ij tight
                c = colorbar;
                clim([0 255]);
                c.YTick = [0 255];
                c.YTickLabel = {'0 dB', strcat(num2str(dB_range),' dB')};
                % c.YTickLabel = {'0 dB', strcat(num2str(dB_range),' dB')};
                ylabel('Z (depth) [cm]')
                yticks(1:1:4)
                xticks(-2:1:2)

                % ylim([0 4])
                if(strcmp(slice_dir,'xz'))
                    xlabel('X [cm]')
                elseif(strcmp(slice_dir,'yz'))
                    xlabel('Y [cm]')
                end

                % set(gca,'XColor','none','box','off')
                colormap hot
                if(strcmp(slice_dir,'xz'))
                    % title(strcat('Y =', {' '}, num2str(round(100*yI(64, 64, i), 2)), ' cm'), 'Interpreter', 'none');
                    title(strcat('Slice #', num2str(i)));
                else
                    title(strcat('X =', {' '}, num2str(round(100*xI(64, 64, i), 2)), ' cm'), 'Interpreter', 'none');
                    % title('Center Slice – YZ');
                end
                drawnow limitrate;
                F = getframe(gcf) ;
                img{i} = frame2im(F);
            end
        else
            for i=1:grid_res
            % for i = 51
                % b_mode_temp = 20*log10(bmode_data(i, :, :)/max(bmode_data(i, :, :), [],'all'));
                % b_mode(i, :, :) = (b_mode_temp+dB_range).*(b_mode_temp>-dB_range);
                % b_mode(i, :, :) = uint8(255*b_mode(i, :, :)/dB_range);
                if(strcmp(slice_dir,'xz') && orthogonal)
                    hP = pcolor(squeeze(yI(i,:,:))*1e2,squeeze(zI(i,:,:))*1e2,squeeze(b_mode(i,:,:)));
                    % hP = pcolor(squeeze(b_mode(i,:,:)));
                elseif(strcmp(slice_dir,'yz') && orthogonal)
                    hP = pcolor(squeeze(xI(i,:,:))*1e2,squeeze(zI(i,:,:))*1e2,squeeze(b_mode(i,:,:)));
                end
                shading interp, axis equal ij tight
                c = colorbar;
                clim([0 255]);
                c.YTick = [0 255];
                c.YTickLabel = {'0 dB', strcat(num2str(dB_range),' dB')};
                ylabel('Z (depth) [cm]')
                yticks([1 2 3 4])

                if(strcmp(slice_dir,'xz'))
                    xlabel('Y [cm]')
                elseif(strcmp(slice_dir,'yz'))
                    xlabel('X [cm]')
                end

                % set(gca,'XColor','none','box','off')
                colormap hot
                if(strcmp(slice_dir,'xz'))
                    % title('Center Slice – YZ')
                    % title(strcat('X =', {' '}, num2str(round(100*xI(i, 64, 26), 2)), ' cm'), 'Interpreter', 'none');
                else
                    % title('YZ – Orthogonal Reconstruction at Center')
                    title(strcat('Y =', {' '}, num2str(round(100*yI(i, 64, 26), 2)), ' cm'), 'Interpreter', 'none');
                end
                drawnow limitrate;
                F = getframe(gcf) ;
                img{i} = frame2im(F);
            end
        end
    else
        %Normalization
        b_mode = bmode_data/max(bmode_data,[],'all');
        
        %Plot real-tim image
        figure();
        if(~orthogonal)
            for i=1:grid_res_orth
            % for i=84
                % b_mode_temp = 20*log10(bmode_data(:, :, i)/max(bmode_data(:, :, i), [],'all'));
                % b_mode(:, :, i) = (b_mode_temp+dB_range).*(b_mode_temp>-dB_range);
                % b_mode(:, :, i) = uint8(255*b_mode(:, :, i)/dB_range);
                if(strcmp(slice_dir,'xz'))
                    hP = pcolor(xI(:,:,i)*1e2,zI(:,:,i)*1e2,b_mode(:,:,i));
                elseif(strcmp(slice_dir,'yz'))
                    hP = pcolor(yI(:,:,i)*1e2,zI(:,:,i)*1e2,b_mode(:,:,i));
                end
                shading interp, axis equal ij tight
                c = colorbar;
                clim([0 1]);
                c.YTick = [0 1];
                c.YTickLabel = {'0','1'};
                ylabel('Z (depth) [cm]')
                xlabel('X [cm]')
                set(gca,'XColor','none','box','off')
                colormap hot
                title(strcat('Slice: ',num2str(i)), 'Interpreter', 'none');
                drawnow limitrate;
                F = getframe(gcf) ;
                img{i} = frame2im(F);
            end
        else
            for i=1:grid_res
                b_mode_temp = 20*log10(bmode_data(:, :, i)/max(bmode_data(:, :, i), [],'all'));
                b_mode(:, :, i) = (b_mode_temp+dB_range).*(b_mode_temp>-dB_range);
                b_mode(:, :, i) = uint8(255*b_mode(:, :, i)/dB_range);
                if(strcmp(slice_dir,'xz') && orthogonal)
                    hP = pcolor(squeeze(yI(i,:,:))*1e2,squeeze(zI(i,:,:))*1e2,squeeze(b_mode(i,:,:)));
                elseif(strcmp(slice_dir,'yz') && orthogonal)
                    hP = pcolor(squeeze(xI(i,:,:))*1e2,squeeze(zI(i,:,:))*1e2,squeeze(b_mode(i,:,:)));
                end
                shading interp, axis equal ij tight
                c = colorbar;
                clim([0 1]);
                c.YTick = [0 1];
                c.YTickLabel = {'0','1'};
                ylabel('Z (depth) [cm]')
                xlabel('Y [cm]')
                set(gca,'XColor','none','box','off')
                colormap hot
                title(strcat('Slice: ',num2str(i), ' Angle = ', num2str((i-grid_res/2-0.5)/(grid_res/2-0.5)*30), ' degrees'), 'Interpreter', 'none');
                drawnow limitrate;
                F = getframe(gcf) ;
                img{i} = frame2im(F);
            end
        end
    end
    
    %Find frame with minimum elements (/3 for uint8)
    elementCounts = cellfun(@numel, img)/3;
    [~, min_index] = min(elementCounts);
    
    %Frame parameters
    frameheight = size(img{min_index},1);
    framewidth = size(img{min_index},2);
    
    %% Write Video for RT multiframe

    if(video_out)
        % create the video writer with 1 fps
        writerObj = VideoWriter(strcat(filepath, filename, '.mp4'), 'MPEG-4');
        % set the seconds per image
        writerObj.FrameRate = 1;
        % open the video writer
        open(writerObj);
        % write the frames to the video
        if(~orthogonal)
            for i=1:grid_res_orth
                % convert the image to a frame
                img{i} = imresize(img{i},[frameheight framewidth]);
                writeVideo(writerObj, img{i});
            end
        else
            for i=1:grid_res
                % convert the image to a frame
                img{i} = imresize(img{i},[frameheight framewidth]);
                writeVideo(writerObj, img{i});
            end
        end
        % close the writer object
        close(writerObj);
    end

    % view(2)
    % hP.DataTipTemplate.DataTipRows(3).Value   = 'CData';

end

%% 3-D scatter plot from scattered data

plot_thresh_lower = 90;
plot_thresh_upper = 180;

index_3D = b_mode>plot_thresh_lower & b_mode<plot_thresh_upper & zI>0.005;
x_3D = 100*xI(index_3D);
y_3D = 100*yI(index_3D);
z_3D = 100*zI(index_3D);

bmode_3D = b_mode(index_3D);

figure();
h = scatter3(x_3D, y_3D, z_3D, 20, y_3D,  'o', 'filled');
set(h, 'MarkerFaceAlpha', 0.5)
% set(h, 'MarkerFaceColor', "#444444")
constantplane([0 1 0], 0, "FaceColor", "k", "FaceAlpha", 0.1);

c = colorbar('eastoutside');
clim([-0.8 0.8]);
c.YTick = [-0.8 0 0.8];
c.Label.String = 'Y coordinate [cm]';
% c.Label.Rotation = 270;
cpos = c.Position;
cpos(3) = 0.6*cpos(3);
cpos(4) = 0.6*cpos(4);
cpos(2) = 0.8-cpos(4);
c.Position = cpos;

% colormap sky
set(gca, 'Zdir', 'reverse');
% axis equal 
xlabel('X [cm]')
xlim([-0.8 0.8])
ylabel('Y [cm]')
ylim([-0.8 0.8])
yticks(-0.5:0.5:0.5)
zlabel('Z [cm]')
zlim([0.8 2.1])
zticks(1:0.5:2.5)

%% Interpolate volume data for oblique slices

bmode_3D_func = scatteredInterpolant(xI(:), yI(:), zI(:), double(b_mode(:)), 'linear', 'none');

%% Query Interpolant

x_axis = (-1:0.02:1)*1e-2;
y_axis = (-2:0.02:2)*1e-2;
z_axis = (0:0.02:4.5)*1e-2;

[xq, yq, zq] = meshgrid(x_axis, y_axis, z_axis);

bmode_q = bmode_3D_func(xq, yq, zq);

%% Plot XY slices from volume data

figure();
tiledlayout(3, 3, "TileSpacing", "compact", "Padding", "compact")
for z = 65:2:82
    nexttile;
    pcolor(1e2*squeeze(yq(:, :, z)), 1e2*squeeze(xq(:, :, z)), squeeze(bmode_q(:, :, z)));
    axis equal tight ij
    shading interp;
    colorbar;
    colormap gray;
    title(strcat("Z = ", num2str(1e2*z_axis(z)), " cm"));
end


figure();
tiledlayout(3, 3, "TileSpacing", "compact", "Padding", "compact")
for z = 41:3:67
    nexttile;
    pcolor(1e2*squeeze(yq(:, :, z)), 1e2*squeeze(xq(:, :, z)), squeeze(bmode_q(:, :, z)));
    axis equal tight ij
    shading interp;
    colorbar;
    colormap gray;
    title(strcat("Z = ", num2str(1e2*z_axis(z)), " cm"));
end

%% Plot oblique slices in 3D space

slice_point = [0 0 189];
% slice_point = [51 101 0];
slice_normal = [0 0 1];

[B_slice, x_slice, y_slice, z_slice] = obliqueslice(bmode_q, slice_point, slice_normal);

x_slice = (x_slice./size(xq, 2) - 0.5)*2;
y_slice = (y_slice./size(yq, 1) - 0.5)*4;
z_slice = z_slice./size(zq, 3)*4.5;

% B_slice(y_slice<-1.6 | y_slice>1.4) = NaN;

figure();
surf(x_slice,y_slice, z_slice, B_slice, 'FaceAlpha', 0.9);
colormap(hot);
clim([20 204])
c = colorbar('eastoutside');
c.YTick = [20 204];
c.YTickLabel = {'0 dB', '30 dB'};
cpos = c.Position;
cpos(3) = 0.6*cpos(3);
cpos(4) = 0.6*cpos(4);
cpos(2) = 0.8-cpos(4);
c.Position = cpos;
shading interp
set(gca, 'Zdir', 'reverse');

axis equal tight
xlabel('X [cm]');
xlim([-1 1]);
xticks(-1:1);
ylabel('Y [cm]');
ylim([-2 2]);
yticks(-2:1:2);
zlabel('Z [cm]');
zlim([1 4.5]);
zticks(1:1:4);
view([0 90]);

%% Scatter plot with interpolated data

plot_thresh_lower = 18;
plot_thresh_upper = 20;

index_vessel = (bmode_q>plot_thresh_lower & bmode_q<plot_thresh_upper & zq>0.019 &  zq<0.042 & yq>-0.01 & yq<0.01);
x_vessel = 100*xq(index_vessel);
y_vessel = 100*yq(index_vessel);
z_vessel = 100*zq(index_vessel);

bmode_vessel = bmode_q(index_vessel);

% plot_thresh_lower = 80;
% plot_thresh_upper = 255;
% 
% index_3D = (bmode_q>plot_thresh_lower & bmode_q<plot_thresh_upper & zq>0.019 & yq>-0.015 & yq<0.008);
% x_3D = 100*xq(index_3D);
% y_3D = 100*yq(index_3D);
% z_3D = 100*zq(index_3D);

% bmode_3D = bmode_q(index_3D);

hold on
% h1 = scatter3(x_3D, y_3D, z_3D, 1, 'w', 'o', 'filled');
% set(h1, 'MarkerFaceAlpha', 0.2)

h2 = scatter3(x_vessel, y_vessel, z_vessel, 15, 'k', 'o', 'filled');
set(h2, 'MarkerFaceAlpha', 0.6)
set(h2, 'MarkerFaceColor', "#555555")

colormap gray

% c = colorbar('eastoutside');
% c.Limits = [-1 1];
% c.YTick = [-1 1];
% c.YTickLabel = {'-1', '1'};
% c.Label.String = 'X coordinate [cm]';
% clim([-1 2])
% cpos = c.Position;
% cpos(3) = 0.6*cpos(3);
% cpos(4) = 0.6*cpos(4);
% cpos(2) = 0.8-cpos(4);
% c.Position = cpos;

set(gca, 'Zdir', 'reverse');
ax = gca;
% ax.Color = 'k'; 
% ax.GridColor = 'w';
ax.GridLineWidth = 1.5;

grid on
axis equal
view([48 13]);

constantplane([38 3 0], 0, "FaceColor", "r", "FaceAlpha", 0.2);
constantplane([-3 38 0], 0, "FaceColor", "g", "FaceAlpha", 0.2);
constantplane([0 0 1], 2.6, "FaceColor", "b", "FaceAlpha", 0.2);
constantplane([0 0 1], 3.75, "FaceColor", "b", "FaceAlpha", 0.2);

xlabel('X [cm]');
xlim([-1 1]);
xticks(-1:1);
ylabel('Y [cm]');
ylim([-1.7 1.7]);
yticks(-1:1:1);
zlabel('Z [cm]');
zlim([1.5 4.5]);
zticks(1:1:4);