Read .img medical image without header in python
Asked Answered
I

2

5

I have a radiograph .img file without the header file. However, the researchers who have published the file have given this information about it

High resolution (2048 × 2048 matrix size, 0.175mm pixel size)
Wide density range (12-bit, 4096 gray scale)
Universal image format (no header, big-endian raw data)

I am trying to open the file using Python but unable to do so. Could someone suggest any method to read this image file?

Inquisitorial answered 5/10, 2015 at 10:33 Comment(6)
Do you know the exact format? I know matplotlib is good with png files. Check out matplotlib.org/users/image_tutorial.htmlHarneen
This question is similar to yours: Read img medical image without header in Matlab. It only remains to do the translation in Python. Can you give me the hyperlink of an image you want to read ? I would post an answer with a Python code which does the job. Thanks.Woodchuck
@Baptiste Please found the link of file below: dropbox.com/s/qcs4u0o6psmdhhb/JPCLN146.IMG?dl=0Inquisitorial
@Inquisitorial Thanks! In fact, I came across the JSRT database before you answer. Take a look at the solution I have proposed below.Woodchuck
@Baptiste Thanks a lot for your answerInquisitorial
@Inquisitorial You're welcome. By the way, since you're new to StackOverflow, don't forget to click to accept the answer.Woodchuck
W
9

I found some radiograph images, like yours, by downloading the JSRT database. I have tested the following code on the first image of this database: JPCLN001.IMG.

import matplotlib.pyplot as plt
import numpy as np

# Parameters.
input_filename = "JPCLN001.IMG"
shape = (2048, 2048) # matrix size
dtype = np.dtype('>u2') # big-endian unsigned integer (16bit)
output_filename = "JPCLN001.PNG"

# Reading.
fid = open(input_filename, 'rb')
data = np.fromfile(fid, dtype)
image = data.reshape(shape)

# Display.
plt.imshow(image, cmap = "gray")
plt.savefig(output_filename)
plt.show()

It produces an output file JPCLN001.PNG which looks like this:

First image of the JSRT database

I hope I have answered to your question. Happy coding!

Woodchuck answered 5/10, 2015 at 14:1 Comment(0)
S
2

Just in case anybody else is looking at these images and wants to convert them in batches, or outside of Python and without needing any programming knowledge... you can convert them pretty readily at the command line in Terminal with ImageMagick (which is installed on most Linux distros anyway, and available for OS X and Windows) like this:

convert -size 2048x2048 -depth 16 -endian MSB -normalize gray:JPCLN130.IMG -compress lzw result.tif

which makes them into compressed 16-bit TIF files that can be viewed in any application. They also then take up half the space on disk without loss of quality since I specified LZW compression.

Likewise, if you want 16-bit PNG files, you can use:

convert -size 2048x2048 -depth 16 -endian MSB -normalize gray:JPCLN130.IMG result.png

enter image description here

Squaw answered 8/11, 2015 at 10:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.