Vector type single channel image

I have a dicom image thats shape is (1136, 852, 1) and PixelID is ‘vector of 8-bit unsigned integer’ . I want to cast this to float32 but as I can not cast vector type to simple float, following online advice I tried:

sitk_scan = sitk.Cast(sitk_scan, sitk.sitkVectorFloat32)
sitk_scan = sitk_scan[:,:,0]

But afterwards its shape is (1136, 852) and PixelID is vector of 32-bit float. And I still can not cast this to sitkFloat32.

sitk::ERROR: Filter does not support casting from casting vector of 32-bit float to 32-bit float

So how can I cast this 1 channel image into a simple sitkFloat32 type?

Hello @Franciska,

The meaning of the original pixel type ‘vector of 8-bit unsigned integer’ is that for every pixel you have multiple values (likely a three channel image). This can mean that the image is a color image in RGB color space or that it is a scalar/grayscale image masquerading as a color one, a single channel repeated three times.

Given your desire to convert this image type to float32 and not a vector of float32 you are likely assuming this is a scalar image. To extract the first channel and cast to float, then convert to a 2D image from a 3D slice:

sitk_scan = sitk.VectorIndexSelectionCast(sitk_scan, 0, sitk.sitkFloat32)
sitk_scan = sitk_scan[:,:,0]

To characterize data before working with it, take a look at the characterize_data.py script in the SimpleITK notebooks repository. The data may surprise you, content is not what it visually looks like, vector images that visually look like a scalar, color images that should be scalar/grayscale etc.

3 Likes

Hello,

The concepts in SimpleITK/ITK are a little different than in numpy. In SimpleITK/ITK an Image has a size (x,y,z…) and the number of components ( defaults to 1 for scalars). So in (1136, 852,1), the 1 is the size in the z dimension not the number of components. Based on the information provided, we don’t know how many channels are in the sitk_scan image.

You should display all the details of your original sitk_scan image with print(sitk_scale.ToString()). If the image of a vector pixel type, it will say “VectorImage”, and later say “VectorLength: 3” or similar. This can be accessed via the sitk::Image interface with GetNumberOfComponentsPerPixel.

The 3D image of vectors can becopied to a 4D image of scalar with sitk_scan4d = sitk_scan.ToVector(inPlace=false) which may be want you are expecting.

The VectorIndexSelectionCastImageFilter can be used to select a specific channel. And the VectorMagnitudeImageFilter can be use to compute the l2 norm to get a scalar.

2 Likes