How to open a .bin file containing several frame images
5 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Camilla Zilianti
el 2 de Mayo de 2024
Comentada: Voss
el 3 de Mayo de 2024
Hello, I am trying to open in Matlab some binary files produced from a specific software which basically creates a series of images in time. For each image, a 32x32 pixel grid is created (so each image is composed of 1024 pixel values). Each binary file contains the images recorded in a 2 minute period, at 50 Hz, thus each file contains 6000 frames. To import the .bin file in Matlab the following file format has to be applied for each frame:
- 1 double: Time Stamp (0..1)
- 1 float: Dummy (Analog-Value)
- 1024 float: 32*32 pixel values in %, arranged in 32 rows, from the upper left corner to the lower right corner of the image
- 1 int: MinMax-Flag (0/-1/1)
- 1 int: Event Marker (0,1,2,...)
- 30 char: Event Text
- 1 int: Timing Error (0,1,2,...)
- 52 float: values of specific parameters of ventilation
Every frame consists of 4358 bytes. Data type sizes: double=8Byte, float=4Byte, int=4Byte, char=1Byte.
What I am interested in is actually only the 32*32 pixels values instant by instant (so the 1024 float). I know that when the file contains number with different precision I have to read each format one after the other. I tried to open and read the files with the fopen (first) and fread (then) function
fid = fopen('myfile')
doublereads = fread(fid,inf,'double')
Then I try to go back to the beginning of file just after the first double, with the command
fseek(fid,8,'bof')
But I get ans = 0.
So I do not know how to start reading the float part... As you probably noticed I an very new to Matlab, so sorry in advance for gross mistakes... I thank whoever would like to give me some help.
2 comentarios
Voss
el 2 de Mayo de 2024
"doublereads = fread(fid,inf,'double')"
That reads the entire file (because of the inf) as if it were filled with doubles, which is not the case.
"fseek(fid,8,'bof') But I get ans = 0."
fseek returns 0 when the operation (moving the file position pointer) was successful.
Respuesta aceptada
Voss
el 2 de Mayo de 2024
Editada: Voss
el 2 de Mayo de 2024
This would read the first frame:
filename = 'myfile.bin';
fid = fopen(filename,'r');
time_stamp = fread(fid,1,'double');
dummy = fread(fid,1,'single');
data = fread(fid,[32 32],'single').'; % <- may or may not want the transpose
minmax_flag = fread(fid,1,'int32');
event_marker = fread(fid,1,'uint32');
event_text = fread(fid,[1 30],'*char');
timing_error = fread(fid,1,'uint32');
vent_params = fread(fid,[1 52],'single');
fclose(fid);
Each fread call there will produce a variable of class double except for the *char one. (The * makes the output the same class as what was read, essentially.) For more information, see this section: https://www.mathworks.com/help/matlab/ref/fread.html#btp1twt-1-precision
Since you are only interested in the 32x32 single-precision values (which I assign to the data variable), you can omit assigning the other outputs from fread to any variable (just to avoid cluttering your workspace with variables you won't use). You can also stop reading and close the file after reading the 32x32 data. Example:
filename = 'myfile.bin';
fid = fopen(filename,'r');
fread(fid,1,'double'); % these could be replaced with a single fread call, e.g.,
fread(fid,1,'single'); % fread(fid,3,'single') or any call that reads 12 bytes
data = fread(fid,[32 32],'single').'; % <- may or may not want the transpose
fclose(fid);
To read all the images from the file you can do something like this:
filename = 'myfile.bin';
bytes_per_frame = 4358;
% calculate number of frames in the file:
S = dir(filename);
N_frames = S.bytes/bytes_per_frame;
% make sure the file size is exactly an integer number of frames:
err_msg = sprintf('file size inconsistent with %d bytes/frame',bytes_per_frame);
assert(~rem(N_frames,1), err_msg)
% initialize data (3D array containing all images' data):
data = zeros(32,32,N_frames);
% open the file:
fid = fopen(filename,'r');
for ii = 1:N_frames
% move the file position pointer to the start of the next image data:
fseek(fid,bytes_per_frame*(ii-1)+12,'bof');
% read and store 32x32 image data:
data(:,:,ii) = fread(fid,[32 32],'single');
end
% close the file:
fclose(fid);
% may or may not need to do this to get the image orientation right:
data = permute(data,[2 1 3]);
4 comentarios
Más respuestas (0)
Ver también
Categorías
Más información sobre Convert Image Type en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!