How to read a binary file with below description?

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

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

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

this works but its seems a little odd for me N is 6401025 but voxels is 5589x1 int8
the voxels length should be equal to the N,is'nt it ?
and also I know that data length in z direction is 88 but z_end exceeds 88
I attached the files if you could look
I made an edit that you maybe missed. Check this line to make sure the asterisk (*) is not present:
k = fread(fp,6,'int16');
This forces k to be a double instead of an int16.
For the other values being off, there might be an ordering issue or an endianness issue.
Which particular file from your zip collection should we be looking at? And what are the known data dimensions of that file? In the one file I have looked at so far the data dimensions do not seem consistent with the ordering you specified.
changed the file, now the zip only contains the file that I tested against .
no there wasnt any asterisk befor int16
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?
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)

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

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.

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);

Categories

Find more on Graph and Network Algorithms in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!