flexus / data /multiframe_bmode_plot.m
introvoyz045's picture
Migrated from GitHub
96c0ff0 verified
Raw History Blame Contribute Delete
5.24 kB
%% MATLAB Script for displaying real-time B-Mode data in files named "*rt_recon.mat"
% See "bmode_image_plot.m" for displaying 2D and 3D B-Mode data in files
% named "*3d_recon.mat"
% See "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 = 40;
orthogonal = false;
slice_dir = 'yz';
video_out = false;
frame_rate = 5.394;
averaging = 0;
%% Load saved B-Mode complex data for plotting in MATLAB/
% exporting into image data for Origin
%Data file path
filepath = './datasets_lung/';
%File name
filename = 'lung_dataset1_rt_recon';
%Load Complex data
bmode_data_struct = load(strcat(filepath, filename, '.mat'));
%Assign variables
bmode_data = bmode_data_struct.bmode_data;
zI = bmode_data_struct.zI;
if (slice_dir=='xz')
xI = bmode_data_struct.xI;
elseif (slice_dir=='yz')
yI = bmode_data_struct.yI;
end
if(averaging)
bmode_data = sum(abs(bmode_data), 3);
end
N_frame = size(bmode_data, 3);
%% Imaging
if(N_frame==1)
%% 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 equal
c = colorbar;
clim([0 255]);
c.YTick = [0 255];
c.YTickLabel = {'0 dB',strcat(num2str(dB_range), ' dB')};
xlabel('X [cm]')
ylabel('Z (depth) [cm]')
% set(gca,'XColor','none','box','off')
title("Lung - XZ Slice");
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
else
%% Multiframe
%Magnitude of complex number
bmode_data = abs(bmode_data);
% bmode_data_diff = bmode_data(:, :, 2:end) - bmode_data(:, :, 1:end-1);
%
% bmode_data = abs(bmode_data_diff);
% bmode_avg = mean(bmode_data, 3);
% bmode_data = abs(bmode_data - bmode_avg);
%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);
% frame 27, 39, 51, 65, 75, 92
figure();
for i=1:N_frame
% for i=1:9
% subplot(1, 3, i);
% figure();
if(slice_dir == "xz")
hP = pcolor(xI*1e2,zI*1e2,b_mode(:,:,i));
elseif(slice_dir == "yz")
hP = pcolor(yI*1e2,zI*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')};
ylabel('Z (depth) [cm]')
if(slice_dir=='xz')
xlabel('X [cm]');
else
xlabel('Y [cm]');
end
% set(gca,'XColor','none','box','off')
colormap hot
% title("Frame: "+num2str(i), 'Interpreter', 'none');
% title("Lung – YZ section, Time = "+num2str(round((i-1)/frame_rate, 2))+" sec")
% title("Lung – XZ");
drawnow limitrate;
F = getframe(gcf);
img{i} = frame2im(F);
end
%% write video
if(video_out)
%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
% create the video writer with 1 fps
writerObj = VideoWriter(strcat(filepath, filename, '.mp4'), 'MPEG-4');
% set the seconds per image
writerObj.FrameRate = frame_rate;
% open the video writer
open(writerObj);
% write the frames to the video
for i=1:N_frame-1
% convert the image to a frame
img{i} = imresize(img{i},[frameheight framewidth]);
writeVideo(writerObj, img{i});
end
% close the writer object
close(writerObj);
end
end