I have tried many things to segment gray_matter, white_matter and cs_fluid images using sitk.ConnectedThresholdImageFilter()
. unfortunately, I couldn't. Please, let me know what I am doing wrong.
Here is the example code:
data_dir = "<path to data dir>"
image_dir = data_dir + "images/"
image_filenames = sorted(glob.glob(image_dir + '*.nii.gz'))
mask_dir = data_dir + "masks/"
mask_filenames = sorted(glob.glob(mask_dir + '*.nii.gz'))
image_filename = image_filenames[0]
mask_filename = mask_filenames[0]
image = sitk.ReadImage(image_filename)
mask = sitk.ReadImage(mask_filename)
print("image_filename:", image_filename)
print("Image:")
display_image(image)
print("Mask:")
display_image(mask)
masked_image = sitk.Mask(image, mask)
print("Masked image:")
display_image(masked_image)
gm_filter = sitk.ConnectedThresholdImageFilter()
gm_filter.SetLower(1) # Lower threshold for GM intensities
gm_filter.SetUpper(100) # Upper threshold for GM intensities
gm_image = gm_filter.Execute(masked_image)
print("GM image:")
display_image(gm_image)
And, here is the output:
Image: Mask: Masked image: GM image:
It's all good up to "GM image". I really couldn't figure out what I am doing wrong with the sitk.ConnectedThresholdImageFilter()
method. Thanks in advance.
I could do something similar to what I needed by using methods called sitk.BinaryThreshold()
and sitk.And()
together.
lower_threshold, upper_threshold = 100, 200 # Thresholds for gray matter
gm_image = sitk.BinaryThreshold(image, lower_threshold, upper_threshold, 1, 0)
gm_image = sitk.And(gm_image, mask)
print("GM image:")
display_image(gm_image)
Here's the result for the GM image:
I still wonder what the problem with sitk.ConnectedThresholdImageFilter()
is.