3

I have an MHA file and when I write

from medpy.io import load
image_data, image_header = load("HG/0001/VSD.Brain.XX.O.MR_Flair/VSD.Brain.XX.O.MR_Flair.684.mha")
print(image_data.shape)

I get a tuple (160, 216, 176). What do these dimensions represent (for reference these are brain tumor images from BRATS 2013)? Your help is appreciated.

Edit: on Jupyter for the slider to work I did

import matplotlib.pyplot as plt
from ipywidgets import interact
import numpy as np
%matplotlib inline

@interact(x=(0, image_data.shape[2]))
def update(x):
    plt.imshow(np.flip(image_data[x].T, 0))

but of course your code probably works on other editors

Sandipan Dey
  • 21,482
  • 2
  • 51
  • 63
AMRO
  • 51
  • 1
  • 6

2 Answers2

4

According to the documentation, load(image) "Loads the image and returns a ndarray with the image’s pixel content as well as a header object."

Further down in medpy.io.load it says that image_data is "The image data as numpy array with order x,y,z,c.".


Edit: Because I was kind of curious to see what is actually in this file, I put together a quick script (heavily based on the slider demo) to take a look. I'll leave it here just in case it may be useful to someone. (Click on the "Layer" slider to select the z-coordinate to be drawn.)

from medpy.io import load

image_data, image_header = load("/tmp/VSD.Brain.XX.O.MR_Flair.684.mha")

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.widgets import Slider, Button, RadioButtons

fig, ax = plt.subplots()
plt.subplots_adjust(bottom=0.25)

axlayer = plt.axes([0.25, 0.1, 0.65, 0.03])
slider_layer = Slider(axlayer, 'Layer', 1, image_data.shape[2], valinit=1, valstep=1)

def update(val):
  layer = slider_layer.val
  ax.imshow(image_data[:,:,layer])
  fig.canvas.draw_idle()

slider_layer.on_changed(update)

ax.imshow(image_data[:,:,0])

plt.show()

sample screenshot from the above script

(This indirectly confirms that image_data holds a 3-D voxel image.)

fuenfundachtzig
  • 7,952
  • 13
  • 62
  • 87
  • My question is strictly on the image_data variable and the meaning of each dimension – AMRO Feb 15 '21 at 20:09
  • Well, my *guess* is that your mha file contains a 3-D image? In which case the 3 values likely are the number of pixels in each direction. (Sorry if you figured this much out yourself.) – fuenfundachtzig Feb 15 '21 at 20:13
0

Just to add on top the accepted answer, we can visualize the slices with subplots and animation too:

from medpy.io import load
image_data, image_header = load("VSD.Brain.XX.O.MR_Flair.684.mha")
image_data = image_data / image_data.max()

plt.figure(figsize=(20,32))
plt.gray()
plt.subplots_adjust(0,0,1,0.95,0.01,0.01)
for i in range(ct.shape[0]):
    plt.subplot(16,10,i+1), plt.imshow(image_data[i]), plt.axis('off')
plt.suptitle('Brain-Tumor CT-scan mha (raw) files', size=15)
plt.show()

enter image description here

enter image description here

Sandipan Dey
  • 21,482
  • 2
  • 51
  • 63