%% 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_mode0.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_q0.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_q0.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);