How to read a binary file with below description?

3 次查看(过去 30 天)
Each of our tumor segmentation files is a binary file, consisting of the following format:
1. six uint16 values for the inclusive coordinates of the lesion’s cuboid , relative to the image:
y_start y_end
x_start x_end
z_start z_end
2. the N int8 on/off voxels (0 or 1) for the above specified cube, where
N = (y_end-y_start +1) * (x_end - x_start + 1) * (z_end - z_start + 1).
A voxel value of 1 denotes that it is part of the lesion, while a value of zero denotes it is not.
update: I attached the files so if some one wants to test
  1 个评论
dpb
dpb 2020-1-17
What are the characteristics of the file system/data storage writing the data as to endianess, etc.?
Best would be to attach a file for folks to test against; bester yet to also have the right answer to go along with it.

请先登录,再进行评论。

采纳的回答

James Tursa
James Tursa 2020-1-17
编辑:James Tursa 2020-1-17
E.g.,
fname = the name of your binary file
fp = fopen(fname,'rb');
k = fread(fp,6,'int16');
y_start = k(1);
y_end = k(2);
x_start = k(3);
x_end = k(4);
z_start = k(5);
z_end = k(6);
N = (y_end - y_start +1) * (x_end - x_start + 1) * (z_end - z_start + 1);
voxels = fread(fp,N,'*int8');
fclose(fp);
It is not entirely clear what order the y_start, y_end, etc values are in the file from your description, so you may have to adjust that part of the code.
  5 个评论
James Tursa
James Tursa 2020-1-18
编辑:James Tursa 2020-1-18
Here are the first six int16 values read from this file:
346
120
46
372
142
54
There doesn't seem to be consistency with your stated ordering. Can you recognize from this what values should be which dimension values for that particular file? I would note that if the last two values are z_end and z_start in that order, then their difference is 88.
But if the first three numbers are "start" values and the last three are corresponding "end" values, at least the numbering is increasing and consistent.
Can you clarify this?
salman rezaie
salman rezaie 2020-1-18
编辑:salman rezaie 2020-1-18
yes , I checked and its seems this is the only correct way, the three first values are start value and three last numbers are end values, thanks.

请先登录,再进行评论。

更多回答(2 个)

dpb
dpb 2020-1-17
Well, one can take a stab at it assuming is compatible w/ most common desktop architecture at the moment and see if it works or not--
fid=fopen('youfile.bin');
x1=fread(fid,1,'int16');
x2=fread(fid,1,'int16');
y1=fread(fid,1,'int16');
y2=fread(fid,1,'int16');
z1=fread(fid,1,'int16');
z2=fread(fid,1,'int16');
N=(x2-x1+1)*(y2-y1+1)*(z2-z1+1);
v=fread(fid,N);
fid=fclose(fid);
What's unspecified is the orientation in which the voxels are written...the above will return a 1D vector; it will be necessary to reshape() appropriately based on the output scheme used.
As noted above, a small sample data file would be about only way to really test it...a link to the devices documentation could/would also probably supply the required information. But, there's nothing like the test.
  1 个评论
salman rezaie
salman rezaie 2020-1-18
as I mentioned above the "v" length is not equal to the N and also the z2 exceeds the actual data length in z direction
it should be reshaped to the Nx*Ny*Nz , but it cant because "v" is smaller than N
I attached the data if you could test.thanks

请先登录,再进行评论。


Walter Roberson
Walter Roberson 2020-1-17
编辑:Walter Roberson 2020-1-17
filename = 'appropriatefile.les';
[fid, msg] = fopen(filename);
if fid < 0
error('Filed to open file "%s" because "%s"', filename, msg);
end
y_start = fread(fid, 1, '*uint16');
y_end = fread(fid, 1, '*uint16');
x_start = fread(fid, 1, '*uint16');
x_end = fread(fid, 1, '*uint16');
z_start = fread(fid, 1, '*uint16');
z_end = fread(fid, 1, '*uint16');
Ny = y_end-y_start + 1;
Nx = x_end - x_start + 1;
Nz = z_end - z_start + 1;
N = double(Ny) * double(Nx) * double(Nz);
voxel_mask = fread(fid, [Ny, Nx, Nz], '*int8'); %or maybe [Nx, Ny, Nz] or some other order
fclose(fid);

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by