Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 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?

like image 303
Rahul Avatar asked Oct 05 '15 10:10

Rahul


1 Answers

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!

like image 167
Flabetvibes Avatar answered Sep 21 '22 18:09

Flabetvibes