Newbie question on using filters and access imagedata

Hi all,

I am working on a module for exporting image data from slicer to a format for our medical simulation systems. I have started with a simple python module for thresholding, scaling and normalizing the data (0 – 1) and writing them to a file format our simulation system can read. Getting the image data and writing them to file seems straight forward.

But for me it is not clear how to change the actual image data and get the values for writing them to an output file. I see people using vtk filters to change the displayed views, but they do not change the actual image data.

My code looks somewhat like this:

# getimage dimentions and spacing
imageData = inVolumeData.GetImageData()
imageDimention = imageData.GetDimensions()
scalarRange = imageData.GetScalarRange();
imageSpaceing = inVolumeData.GetSpacing()

# filter, let use existing filters and not make my own
shiftScale = vtk.vtkImageShiftScale()
shiftScale.SetInputData( imageData )
### how to get the raw data values for output the file?
### and update the slicer views?

You can do it quite easily using numpy:

However, I would strongly suggest not to rescale actual voxel values only for display: voxel values may have physical meaning (Hounsfield unit, etc) that you would lose by “normalizing” them; it may also increase storage requirements by a factor of 4-8x if you rescale to 0-1 range, because you need to use float or double data type; you may need to implement rescaling in the end-user software anyway, as window/level often need to be adjusted depending on the specific task and user preference. There are several more reasons.

Hi lassoan,

Thanks for your answer, I got it to work!

1 Like