Skip to content Skip to sidebar Skip to footer

Read .img Medical Image Without Header In Python

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 matri

Solution 1:

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!


Solution 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


Post a Comment for "Read .img Medical Image Without Header In Python"