File size: 5,237 Bytes
96c0ff0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
%% 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