How to modify an array of a SITK object

Hi, I’m a beginner in using SITK. I imagine this should be an easy thing to do but I couldn’t find anything that worked. Basically, I have a DICOM image that I’m loading with SITK. I also have a binary mask for this DICOM image. How can I easily apply this mask to this SITK image? I wanna keep the metadata on the original image intact

Input DICOM Image

input_img = sitk.ImageSeriesReader()
dicom_names = input_img.GetGDCMSeriesFileNames(sys.argv[1])
input_img.SetFileNames(dicom_names)

Mask = np.array(mask)

Now, how can I apply Mask to the input_img without modifying the metadata?

Thanks for your help!

Hello @javidx ,

See code below:

# Read first series from the given directory (sys.argv[1])
input_image = sitk.ReadImage(sitk.ImageSeriesReader_GetGDCMSeriesFileNames(sys.argv[1]))

# mask is a binary numpy array, so get the corresponding SimpleITK image
mask_image = sitk.GetImageFromArray(mask)
mask_image.CopyInformation(input_image)

# apply mask to input_img
foreground_image = input_image*sitk.Cast(mask_image, input_image.GetPixelID())
1 Like

Thank you so much! Worked like a charm!