Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
fread binary

Subject: fread binary

From: Jobo Baggins

Date: 4 Nov, 2010 14:53:59

Message: 1 of 4

I'm new to Matlab. I am trying to read in a file designed to store an
image. It is in binary format. There is a 128 byte header, followed by
option information, and then the pixel data. I wrote something like
this:

fid = fopen(filename, 'r'); % Read the image file
fob = fread(fid, 4, '*char'); % Read in file byte order information.
Give to var = machineformat

cols = fread(fid, 4, '*uint8', machineformat); % Read in number
of columns: 4 bytes unsigned integer. ncol
rows = fread(fid, 4, '*uint8', machineformat); % Read in number
of rows: 4 bytes unsigned integer. nrow
frames = fread(fid, 4, '*uint8', machineformat); % Read in number
of frames: 4 bytes unsigned integer. nframe

s_type = fread(fid, 4, '*uint8', machineformat); % Read in pixel
format: 4 bytes unsigned integer.
% Can be - 0: 32 bit floating point values, 1: 2 byte signed
% integer, 2: 1 byte unsigned

i_len = fread(fid, 4, '*uint8', machineformat); % Read in optional
information: 4 bytes unsigned integer
res = fread(fid, 104); % Read in reserved
spaces

data = fread(fid, precision, machineformat)'; % Read in pixel
data: format given by s_type

fclose(fid);

but that is as far as I have got. I don't really know how to extract
the image from the byte stream. I would be grateful for some ideas....
Thanks!

Subject: fread binary

From: Sean

Date: 4 Nov, 2010 15:02:04

Message: 2 of 4

Jobo Baggins <jobobaggins@gmail.com> wrote in message <71352d1b-39c7-4d4a-930e-5a9590a06568@t13g2000yqm.googlegroups.com>...
> I'm new to Matlab. I am trying to read in a file designed to store an
> image. It is in binary format. There is a 128 byte header, followed by
> option information, and then the pixel data. I wrote something like
> this:
>
> fid = fopen(filename, 'r'); % Read the image file
> fob = fread(fid, 4, '*char'); % Read in file byte order information.
> Give to var = machineformat
>
> cols = fread(fid, 4, '*uint8', machineformat); % Read in number
> of columns: 4 bytes unsigned integer. ncol
> rows = fread(fid, 4, '*uint8', machineformat); % Read in number
> of rows: 4 bytes unsigned integer. nrow
> frames = fread(fid, 4, '*uint8', machineformat); % Read in number
> of frames: 4 bytes unsigned integer. nframe
>
> s_type = fread(fid, 4, '*uint8', machineformat); % Read in pixel
> format: 4 bytes unsigned integer.
> % Can be - 0: 32 bit floating point values, 1: 2 byte signed
> % integer, 2: 1 byte unsigned
>
> i_len = fread(fid, 4, '*uint8', machineformat); % Read in optional
> information: 4 bytes unsigned integer
> res = fread(fid, 104); % Read in reserved
> spaces
>
> data = fread(fid, precision, machineformat)'; % Read in pixel
> data: format given by s_type
>
> fclose(fid);
>
> but that is as far as I have got. I don't really know how to extract
> the image from the byte stream. I would be grateful for some ideas....
> Thanks!

look at FSEEK to skip the header all together
doc fseek

Subject: fread binary

From: ImageAnalyst

Date: 4 Nov, 2010 15:21:48

Message: 3 of 4

Isn't "data" your image? Here, check out this function I wrote for
reading an image out of a raw binary file containing lots of images:
The key part is really this:
if stHeader.BytesPerVoxel == 1
oneSlice = fread(fileHandle, [x_size y_size], '*uint8');
elseif stHeader.BytesPerVoxel == 2
oneSlice = fread(fileHandle, [x_size y_size], '*int16'); % It will
be a 2D array after this.
else
error('Unsupported BytesPerVoxel %d', stHeader.BytesPerVoxel);
end




%-----------------------------------------------------------------------------------------------

function [oneSlice, stHeader] = Read_RAW_Slice(fullFileName,
stInputParameters)

% Check syntax. Must have two arguments.
if (nargin ~= 2)
error('Usage: [image2D, stHeader] = Read_RAW_Slice(fullFileName,
stInputParameters)');
end
if (ischar(fullFileName) ~= 1)
error('Requires a string filename as an argument.');
end
if ~exist(fullFileName, 'file')
error('Error: file passed in to Read_RAW_Slice\n%s\ndoes not
exist.', fullFileName);
end

% Read in header and get 3D image array dimensions.
stHeader = Read_RAW_Header(fullFileName);
% Note: fread() requires that x_size and y_size be doubles.
x_size = double(stHeader.x_size);
y_size = double(stHeader.y_size);
z_size = double(stHeader.z_size);

% Determine the parameters that they passed in.
if isa(stInputParameters, 'double') || isa(stInputParameters,
'integer')
% They didn't pass in a structure - they just passed in a single
number (scalar).
% Interpret this number as the slice number.
sliceNumber = int32(stInputParameters);
% For the rest, just take the entire slice.
xStart = int32(1);
yStart = int32(1);
xEnd = int32(x_size);
yEnd = int32(y_size);
Subsampling = int32(1);
else
% They passed in a structure. Make sure they passed in valid
numbers.
% Assign defaults to any fields in stInputParameters that they
didn't set.
stValidParameters = ValidateInputParameters(stInputParameters,
x_size, y_size, z_size);
sliceNumber = int32(stValidParameters.ZStart);
xStart = int32(stValidParameters.XStart);
yStart = int32(stValidParameters.YStart);
xEnd = int32(stValidParameters.XEnd);
yEnd = int32(stValidParameters.YEnd);
Subsampling = int32(stValidParameters.Subsample);
end

% Make sure they asked for a slice that is within range.
if (sliceNumber > z_size)
% Error and exit if they didn't.
error('Slice number must be within the range 1 to %d.\nYou asked for
slice %d.', z_size, sliceNumber);
end

% Open file for reading.
fileHandle = fopen(fullFileName, 'rb', stHeader.EndianArg);
if (fileHandle == -1)
error(['Error opening ', fullFileName, ' for input.']);
end

% Skip past header of stHeader.HeaderSize bytes.
bytesToSkip = int32(stHeader.HeaderSize);
fseek(fileHandle, bytesToSkip, 'bof');

% Now, additionally, skip past (sliceNumber - 1) slices
% so that we end up at the beginning byte of the slice that we want.
bytesToSkip = bytesToSkip + int32(x_size * y_size *
stHeader.BytesPerVoxel * (sliceNumber - 1));
fseek(fileHandle, bytesToSkip, 'bof');

% Read in image2D image data
% Get original, full-sized 2-D slice.
% Note: fread() requires that x_size and y_size be doubles.
% Note: fread() will return a 2D array if you pass in a 2D array for
the number of bytes, as in [x_size y_size].
if stHeader.BytesPerVoxel == 1
oneSlice = fread(fileHandle, [x_size y_size], '*uint8');
elseif stHeader.BytesPerVoxel == 2
oneSlice = fread(fileHandle, [x_size y_size], '*int16'); % It will
be a 2D array after this.
else
error('Unsupported BytesPerVoxel %d', stHeader.BytesPerVoxel);
end

% Close the file.
fclose(fileHandle);

% Subsample and/or crop it, if requested.
if Subsampling ~= 1 || xStart ~= 1 || yStart ~= 1 || xEnd ~= x_size
|| yEnd ~= y_size
oneSlice = oneSlice(xStart:Subsampling:xEnd,
yStart:Subsampling:yEnd);
end

return; % from Read_RAW_Slice()

Subject: fread binary

From: Jobo Baggins

Date: 4 Nov, 2010 22:47:21

Message: 4 of 4

On Nov 4, 3:21 pm, ImageAnalyst <imageanal...@mailinator.com> wrote:
> Isn't "data" your image?  Here, check out this function I wrote for
> reading an image out of a raw binary file containing lots of images:
> The key part is really this:
>         if stHeader.BytesPerVoxel == 1
>                 oneSlice = fread(fileHandle, [x_size y_size], '*uint8');
>         elseif stHeader.BytesPerVoxel == 2
>                 oneSlice = fread(fileHandle, [x_size y_size], '*int16'); % It will
> be a 2D array after this.
>         else
>                 error('Unsupported BytesPerVoxel %d', stHeader.BytesPerVoxel);
>         end
>
> %--------------------------------------------------------------------------­---------------------
>
> function [oneSlice, stHeader] = Read_RAW_Slice(fullFileName,
> stInputParameters)
>
>         % Check syntax.  Must have two arguments.
>         if (nargin ~= 2)
>                 error('Usage: [image2D, stHeader] = Read_RAW_Slice(fullFileName,
> stInputParameters)');
>         end
>         if (ischar(fullFileName) ~= 1)
>                 error('Requires a string filename as an argument.');
>         end
>         if ~exist(fullFileName, 'file')
>                 error('Error: file passed in to Read_RAW_Slice\n%s\ndoes not
> exist.', fullFileName);
>         end
>
>         % Read in header and get 3D image array dimensions.
>         stHeader = Read_RAW_Header(fullFileName);
>         % Note: fread() requires that x_size and y_size be doubles.
>         x_size = double(stHeader.x_size);
>         y_size = double(stHeader.y_size);
>         z_size = double(stHeader.z_size);
>
>         % Determine the parameters that they passed in.
>         if isa(stInputParameters, 'double') || isa(stInputParameters,
> 'integer')
>                 % They didn't pass in a structure - they just passed in a single
> number (scalar).
>                 % Interpret this number as the slice number.
>                 sliceNumber = int32(stInputParameters);
>                 % For the rest, just take the entire slice.
>                 xStart = int32(1);
>                 yStart = int32(1);
>                 xEnd = int32(x_size);
>                 yEnd = int32(y_size);
>                 Subsampling = int32(1);
>         else
>                 % They passed in a structure.  Make sure they passed in valid
> numbers.
>                 % Assign defaults to any fields in stInputParameters that they
> didn't set.
>                 stValidParameters = ValidateInputParameters(stInputParameters,
> x_size, y_size, z_size);
>                 sliceNumber = int32(stValidParameters.ZStart);
>                 xStart = int32(stValidParameters.XStart);
>                 yStart = int32(stValidParameters.YStart);
>                 xEnd = int32(stValidParameters.XEnd);
>                 yEnd = int32(stValidParameters.YEnd);
>                 Subsampling = int32(stValidParameters.Subsample);
>         end
>
>         % Make sure they asked for a slice that is within range.
>         if (sliceNumber > z_size)
>                 % Error and exit if they didn't.
>                 error('Slice number must be within the range 1 to %d.\nYou asked for
> slice %d.', z_size, sliceNumber);
>         end
>
>         % Open file for reading.
>         fileHandle = fopen(fullFileName, 'rb', stHeader.EndianArg);
>         if (fileHandle == -1)
>                 error(['Error opening ', fullFileName, ' for input.']);
>         end
>
>         % Skip past header of stHeader.HeaderSize bytes.
>         bytesToSkip = int32(stHeader.HeaderSize);
>         fseek(fileHandle, bytesToSkip, 'bof');
>
>         % Now, additionally, skip past (sliceNumber - 1) slices
>         % so that we end up at the beginning byte of the slice that we want.
>         bytesToSkip = bytesToSkip + int32(x_size * y_size *
> stHeader.BytesPerVoxel * (sliceNumber - 1));
>         fseek(fileHandle, bytesToSkip, 'bof');
>
>         % Read in image2D image data
>         % Get original, full-sized 2-D slice.
>         % Note: fread() requires that x_size and y_size be doubles.
>         % Note: fread() will return a 2D array if you pass in a 2D array for
> the number of bytes, as in [x_size y_size].
>         if stHeader.BytesPerVoxel == 1
>                 oneSlice = fread(fileHandle, [x_size y_size], '*uint8');
>         elseif stHeader.BytesPerVoxel == 2
>                 oneSlice = fread(fileHandle, [x_size y_size], '*int16'); % It will
> be a 2D array after this.
>         else
>                 error('Unsupported BytesPerVoxel %d', stHeader.BytesPerVoxel);
>         end
>
>         % Close the file.
>         fclose(fileHandle);
>
>         % Subsample and/or crop it, if requested.
>         if Subsampling ~= 1 || xStart ~= 1 || yStart ~= 1 || xEnd ~= x_size
> || yEnd ~= y_size
>                 oneSlice = oneSlice(xStart:Subsampling:xEnd,
> yStart:Subsampling:yEnd);
>         end
>
>         return; % from Read_RAW_Slice()

Very grateful for your help. I will try out key part. Thanks!

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us