Opening multipage image file, saved as .bin file.

24 次查看(过去 30 天)
Hello!
I have a program that saves sequences of images as binary (.bin) files, and I'm having some trouble being able to open these as the time series' that they are, as opposed to just one image.
The image files that I have are 16bit, 1024 x 1024 pixels, across 3 imaging channels, with 'n' time points (this is different for each file). The ordering of the files is 1 2 3 1 2 3 1 2 3, where 1 is the first channel, 2 is the second channel, etc.
I am using fopen and fread to open the file - when I don't specify the size of the file when using fread, I get a HUGE double array which I can't seem to reshape into the original file, as the length of the double does not seem to be related to the number of pixels or any other image parameters. When I use fread with the size specified my output is (what looks like) the first image of my file, but no others.
Is there a way to open the .bin file with fopen, and then import each individual image with fread in a loop?
Thanks!

回答(1 个)

Chunru
Chunru 2021-6-4
fid = fopen('your_binary_file', 'b');
x = fread(fid, inf, 'int16'); % it could be 'uint16', depending on how data is written
nimg = length(x)/(1024*1024*3); % it should be an integer
x = reshape(x, [3, 1024, 1024, nimg]); % ColorChannel x M x N X Nimg
x = permute(x, [2 3 1 4]); % M X N X ColorChanel X Nimg
imshow(x(:,:,:, 1)); % Show the 1st image
  2 个评论
Lucy Heap
Lucy Heap 2021-6-4
So this is very close to what I was trying to use, but the same problem is occuring where the nimg value is not an integer, which I do not understand...
As a side, multiple images are saved for each file, to make sure none of them are >1GB in file size - Is there a way to open and concatenate together a bunch of these binary files?
Chunru
Chunru 2021-6-4
编辑:Chunru 2021-6-4
To concatenate multiple files:
x =[];
for i=1:nfiles
current_x = read_from_current_file;
x = [x; current_x]; % check out the oritention of current_x
end
Or you can read images one by one
fid = fopen(as_before)
while true
x = fread(fid, [3, 1024, 1024], 'int16');
if numel(x) < 3*1024*1024
% read the rest from next file and cancatenate with the x
% break if there is no more file to read
else
% do your processing on the loaded image
end
end

请先登录,再进行评论。

类别

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

Community Treasure Hunt

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

Start Hunting!

Translated by