How to read a binary file with below description?

2 views (last 30 days)
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 Comment
dpb
dpb on 17 Jan 2020
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.

Sign in to comment.

Accepted Answer

James Tursa
James Tursa on 17 Jan 2020
Edited: James Tursa on 17 Jan 2020
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 Comments
James Tursa
James Tursa on 18 Jan 2020
Edited: James Tursa on 18 Jan 2020
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 on 18 Jan 2020
Edited: salman rezaie on 18 Jan 2020
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.

Sign in to comment.

More Answers (2)

dpb
dpb on 17 Jan 2020
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 Comment
salman rezaie
salman rezaie on 18 Jan 2020
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

Sign in to comment.


Walter Roberson
Walter Roberson on 17 Jan 2020
Edited: Walter Roberson on 17 Jan 2020
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);

Community Treasure Hunt

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

Start Hunting!