add alpha channel to an image in simpleitk

I have a group of rgb images and want to add an alpha channel, but am not sure how to do that or where to look?

is the easiest option to create an empty rgba file with similar attributes as the rgb file and copy the rgb portion into it?

What I want to do is create a sparse volume where each image matches the expected slice thickness and the gaps inbetween simply have the alpha set to zero. if there is a better way to represent a sparse volume I would be interested to know how that might work.

You can use the VectorIndexSelectionCast function to split an RGB image into separate components. Then create an alpha image of matching dimensions and use the Compose function to combine all four channels into an RGBA image.

Here’s an example

rgb = sitk.Image(100,100,sitk.sitkVectorUInt8)

r = sitk.VectorIndexSelectionCast(rgb, 0)
g = sitk.VectorIndexSelectionCast(rgb, 1)
b = sitk.VectorIndexSelectionCast(rgb, 2)

a = sitk.Image(100,100,sitk.sitkUInt8)

rgba = sitk.Compose(r, g, b, a)
2 Likes