Reading/casting .tif images into SimpleITK

Hello,

My lab has .tif images we need to do a groupwise registration on and I am having difficulty reading them into SimpleITK. If I understand correctly, to register images they need to be floats so I have tried reading in the images as float32 and reading as tifs and then casting to float32 using this code:

import SimpleITK as sitk
from SimpleITK import Cast as cast

fixfloat = sitk.ReadImage(“tmov.jpg”, outputPixelType = sitk.sitkFloat32)
fix = sitk.ReadImage(“tmov.jpg”)
fixcast = cast(fix, sitk.sitkFloat32)

The image being read is just a red triangle I made in Photoshop to test with.

tfix.tif (567.1 KB)

When I read it in converting to float32, the image ends up being blank when I save it out. When I read it it in as-is, the image displays properly when saved out but when I try to cast it I get the message:

:ERROR: Filter does not support casting from casting vector of 8-bit unsigned integer to 32-bit float

Because of this, the image version I can register is blank and the image showing the triangle can’t be registered. I would be extremely grateful for some advice on what I am doing wrong.

Thanks very much in advance!

Rock

Hello,

You are correct that the registration framework works with float and double images.

However, in simple pixel vector types are distinct from scalar pixel types. The error messages says "vector of 8-bit " so that is an indication that your fix image is a multi-component image and not a scalar. Since these are test images you may just want to save them as gray scale images.

You can do a “print(fix)” to look at some of the information such as the pixel type of the image. When displaying floating point images you need to be mindful of the data range ( min, max ) of the pixel and what the window the viewer is using or expecting from the data. For example Matplotlib’s imshow has vmin, and vmax parameters: matplotlib.pyplot.imshow — Matplotlib 3.10.8 documentation

3 Likes