read .img format image without header file

13 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
Walter Roberson
Walter Roberson on 31 Oct 2015
hadeel, it is not possible to know that without additional information from another source.
For example, suppose I tell you that I have three numbers, H, W, and D, that multiply together to give 60. Now I ask you to tell me what the three individual numbers are. How can you know if they are [2, 3, 10]; or [2 15 2]; or [5 4 3] ?
When you have a file without a header and with no other information then all you know is the total length of the file, which will be the product H * W * D, and that is never enough to determine the individual values, not even if the total length is a prime number.
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.

Categories

Find more on Read, Write, and Modify Image in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!