-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRPread.m
256 lines (223 loc) · 9.73 KB
/
RPread.m
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% loads the ultrasound RF data saved from the Sonix software
%%
%% Inputs:
%% filename - The path of the data to open
%%
%% Return:
%% Im - The image data returned into a 3D array (h, w, numframes)
%% header - The file header information
%%
%% Corina Leung, [email protected]
%% Modified by Ali Baghani, Nov 2012 to support recent changes to the user
%% interface [email protected]
%% Ultrasonix Medical Corporation Nov 2007
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [Im,header] = RPread(varargin)
% to determine whether a later version of Sonix is being used
if nargin > 1
filename = varargin{1};
version = varargin{2}; % Valid versions are: 5 for Sonix versions prior to 6.0
% 6 for newer Sonix versions 6.0.2, 6.0.3, 6.0.4, 6.0.6, etc
else
filename = varargin{1};
version = '6.0.3';
end
fid= fopen(filename, 'r');
fileExt = filename(end-3:end);
if( fid == -1)
error('Cannot open file');
end
% read the header info
hinfo = fread(fid, 19, 'int32');
% load the header information into a structure and save under a separate file
header = struct('filetype', 0, 'nframes', 0, 'w', 0, 'h', 0, 'ss', 0, 'ul', [0,0], 'ur', [0,0], 'br', [0,0], 'bl', [0,0], 'probe',0, 'txf', 0, 'sf', 0, 'dr', 0, 'ld', 0, 'extra', 0);
header.filetype = hinfo(1);
header.nframes = hinfo(2);
header.w = hinfo(3);
header.h = hinfo(4);
header.ss = hinfo(5);
header.ul = [hinfo(6), hinfo(7)];
header.ur = [hinfo(8), hinfo(9)];
header.br = [hinfo(10), hinfo(11)];
header.bl = [hinfo(12), hinfo(13)];
header.probe = hinfo(14);
header.txf = hinfo(15);
header.sf = hinfo(16);
header.dr = hinfo(17);
header.ld = hinfo(18);
header.extra = hinfo(19);
% -------------- memory initialization for speeding up -------------------
switch header.filetype
case {2, 4, 8, 16, 64, 256, 1024, 4096, 8192, 16384, 32768}
Im = zeros(header.h, header.w, header.nframes);
case {32}
Im = zeros(header.h, header.nframes);
case {128}
Im = zeros(header.h, 1, header.nframes);
case {512}
Im = zeros(header.h, header.w * header.extra, header.nframes);
case {2048}
Im = zeros(header.h, header.w * 2, header.nframes);
case {524288}
Im = zeros(header.w, header.nframes);
otherwise
Im = [];
end
h = waitbar(0, 'Generando las lineas RF');
% load the data and save into individual .mat files
for frame_count = 1:header.nframes
if(header.filetype == 2) %.bpr
if (regexp(version, '5.*') == 1) tag = fread(fid,1,'int32'); end % Each frame has 4 byte header for frame number in older versions
[v,count] = fread(fid,header.w*header.h,'uchar=>uchar');
Im(:,:,frame_count) = reshape(v,header.h,header.w);
elseif(header.filetype == 4) %postscan B .b8
if (regexp(version, '5.*') == 1) tag = fread(fid,1,'int32'); end
[v,count] = fread(fid,header.w*header.h,'uint8');
temp = int16(reshape(v,header.w,header.h));
Im(:,:,frame_count) = imrotate(temp, -90);
elseif(header.filetype == 8) %postscan B .b32
if (regexp(version, '5.*') == 1) tag = fread(fid,1,'int32'); end
[v,count] = fread(fid,header.w*header.h,'uint32');
temp = reshape(v,header.w,header.h);
Im(:,:,frame_count) = imrotate(temp, -90);
elseif(header.filetype == 16) %rf
if (regexp(version, '5.*') == 1) tag = fread(fid,1,'int32'); end
[v,count] = fread(fid,header.w*header.h,'int16');
Im(:,:,frame_count) = int16(reshape(v,header.h,header.w));
elseif(header.filetype == 32) %.mpr
if (regexp(version, '5.*') == 1) tag = fread(fid,1,'int32'); end
[v,count] = fread(fid,header.h,'int16');
Im(:,frame_count) = v;
elseif(header.filetype == 64) %.m
[v,count] = fread(fid,'uint8');
temp = reshape(v,header.w,header.h);
Im(:,:,frame_count) = imrotate(temp,-90);
elseif(header.filetype == 128) %.drf
if (regexp(version, '5.*') == 1) tag = fread(fid,1,'int32'); end
[v,count] = fread(fid,header.h,'int16');
Im(:,:,frame_count) = int16(reshape(v,header.h,1));
elseif(header.filetype == 512) %crf
if (regexp(version, '5.*') == 1) tag = fread(fid,1,'int32'); end
[v,count] = fread(fid,header.extra*header.w*header.h,'int16');
Im(:,:,frame_count) = reshape(v,header.h,header.w*header.extra);
% to obtain data per packet size use:
% Im(:,:,:,frame_count) = reshape(v,header.h,header.w,header.extra);
elseif(header.filetype == 256) %.pw
[v,count] = fread(fid,'uint8');
temp = reshape(v,header.w,header.h);
Im(:,:,frame_count) = imrotate(temp,-90);
% elseif(header.filetype == 1024) %.col %The old file format for SONIX version 3.0X
% [v,count] = fread(fid,header.w*header.h,'int');
% temp = reshape(v,header.w,header.h);
% temp2 = imrotate(temp, -90);
% Im(:,:,frame_count) = mirror(temp2,header.w);
%
% elseif((header.filetype == 2048) & (fileExt == '.sig')) %color .sig %The old file format for SONIX version 3.0X
% %Each frame has 4 byte header for frame number
% tag = fread(fid,1,'int32');
% [v,count] = fread(fid,header.w*header.h,'uchar=>uchar');
% temp = reshape(v,header.w,header.h);
% temp2 = imrotate(temp, -90);
% Im(:,:,frame_count) = mirror(temp2,header.w);
elseif(header.filetype == 1024) %.col
[v,count] = fread(fid,header.w*header.h,'int');
temp = reshape(v,header.w,header.h);
temp2 = imrotate(temp, -90);
Im(:,:,frame_count) = mirror(temp2,header.w);
elseif((header.filetype == 2048)) %color .cvv (the new format as of SONIX version 3.1X)
% velocity data
[v,count] = fread(fid,header.w*header.h,'uint8');
temp = reshape(v,header.w,header.h);
temp2 = imrotate(temp, -90);
tempIm1 = mirror(temp2,header.w);
% sigma
[v,count] =fread(fid, header.w*header.h,'uint8');
temp = reshape(v,header.w, header.h);
temp2 = imrotate(temp, -90);
tempIm2 = mirror(temp2,header.w);
Im(:,:,frame_count) = [tempIm1 tempIm2];
elseif(header.filetype == 4096) %color vel
if (regexp(version, '5.*') == 1) tag = fread(fid,1,'int32'); end
[v,count] = fread(fid,header.w*header.h,'uchar=>uchar');
temp = reshape(v,header.w,header.h);
temp2 = imrotate(temp, -90);
Im(:,:,frame_count) = mirror(temp2,header.w);
elseif(header.filetype == 8192) %.el
[v,count] = fread(fid,header.w*header.h,'int32');
temp = reshape(v,header.w,header.h);
temp2 = imrotate(temp, -90);
Im(:,:,frame_count) = mirror(temp2,header.w);
elseif(header.filetype == 16384) %.elo
[v,count] = fread(fid,header.w*header.h,'uchar=>uchar');
temp = int16(reshape(v,header.w,header.h));
temp2 = imrotate(temp, -90);
Im(:,:,frame_count) = mirror(temp2,header.w);
elseif(header.filetype == 32768) %.epr
[v,count] = fread(fid,header.w*header.h,'uchar=>uchar');
Im(:,:,frame_count) = int16(reshape(v,header.h,header.w));
elseif(header.filetype == 65536) %.ecg
[v,count] = fread(fid,header.w*header.h,'uchar=>uchar');
Im = v;
elseif or(header.filetype == 131072, header.filetype == 262144) %.gps
gps_posx(:, frame_count) = fread(fid, 1, 'double'); %8 bytes for double
gps_posy(:, frame_count) = fread(fid, 1, 'double');
gps_posz(:, frame_count) = fread(fid, 1, 'double');
if strcmp(version, '6.0.3')
gps_a(:, frame_count) = fread(fid, 1, 'double');
gps_e(:, frame_count) = fread(fid, 1, 'double');
gps_r(:, frame_count) = fread(fid, 1, 'double');
end
gps_s(:,:, frame_count) = fread(fid, 9, 'double'); %position matrix
if strcmp(version, '6.0.3')
gps_q(:, frame_count) = fread(fid, 4, 'double');
end
gps_time(:, frame_count) = fread(fid, 1, 'double');
gps_quality(:, frame_count) = fread(fid, 1, 'ushort'); % 2 bytes for unsigned short
Zeros(:, frame_count) = fread(fid, 3, 'uint16'); % 6 bytes of padded zeroes
elseif (header.filetype == 524288) % time stamps
LineStartInClkCycles = fread(fid, header.w, 'uint32');
Im(:, frame_count) = LineStartInClkCycles;
else
disp('Data not supported');
end
if (ishandle(h))
waitbar(frame_count/header.nframes, h);
else
h = waitbar(frame_count/header.nframes, 'Generando las senales RF.');
end
end
if or(header.filetype == 131072, header.filetype == 262144) %.gps
Im.gps_posx = gps_posx;
Im.gps_posy = gps_posy;
Im.gps_posz = gps_posz;
Im.gps_s = gps_s;
Im.gps_time = gps_time;
Im.gps_quality = gps_quality;
Im.gps_Zeros = Zeros;
if strcmp(version, '6.0.3')
Im.gps_a = gps_a;
Im.gps_e = gps_e;
Im.gps_r = gps_r;
Im.gps_q = gps_q;
end
end
pause(0.1);
if (ishandle(h))
delete(h);
end
fclose(fid);
% % for RF data, plot both the RF center line and image
% if(header.filetype == 16 || header.filetype == 512)
% RPviewrf(Im, header, 24);
%
% elseif(header.filetype == 128)
% PDRFLine = Im(:,:,1);
% figure, plot(PDRFLine, 'b');
% title('PW RF');
%
% elseif(header.filetype == 256)
% figure, imagesc(Im(:,:,1));
% colormap(gray);
% title('PW spectrum');
% end