MATLAB Answers

read .img format image without header file

52 views (last 30 days)
aditi gupta
aditi gupta on 15 Oct 2013
Commented: Image Analyst on 31 Oct 2015
I am using chest Xray .img images .there are no header files .how do i read them in matlab. imread and dicomread are not working

Answers (2)

Image Analyst
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.
else
error('Unsupported BytesPerVoxel %d', stHeader.BytesPerVoxel);
end

Jonathan LeSage
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);
fclose(fid)
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:
  4 Comments
Image Analyst
Image Analyst on 31 Oct 2015
If it's a 2D gray scale image in a simple header + image data, you can guess. Guess at a header length. If you're wrong the image will look sheared horizontally. As you get closer the the correct header length the shear will be less and less until it doesn't look sheared at all when you have the exact header length.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!