read .img format image without header file
39 views (last 30 days)
Image Analyst on 15 Oct 2013
Edited: Image Analyst on 15 Oct 2013
Use fread(). Here's a snippet from my code. Flexible enough for 8 bit and 16 bit images.
% 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.
error('Unsupported BytesPerVoxel %d', stHeader.BytesPerVoxel);
Jonathan LeSage on 15 Oct 2013
You could directly read the *.img file directly into the MATLAB workspace via the fopen and fread commands. Since you do not have the information in the header file, you will have to come up with the image dimensions and image bit depth precision. Consult the documentation of fopen and fread for further clarification:
Here is some sample code that could get you started:
fid = fopen('xray.img');
data = fread(fid,imageDimensions,imagePrecision);
Another potential option if you have the image processing toolbox are some built in image format read tools, such as analyze75read and nitfread. A full list can be found below: