1

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: enter image description here Mask: enter image description here Masked image: enter image description here GM image: enter image description here

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.

Burak
  • 29
  • 1
  • 9

2 Answers2

1

You haven't specified the seed point for the region growing, provide a seed point within the region of interest.

You can do that with the AddSeed method of the ConnectedThresholdImageFilter

edit: lets try the AddSeed method

For example:

from glob import glob
import SimpleITK as sitk

data_dir = "<path to data dir>"
image_dir = data_dir + "images/" 
image_filenames = sorted(glob(image_dir + '*.nii.gz'))

mask_dir = data_dir + "masks/" 
mask_filenames = sorted(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)

masked_image = sitk.Mask(image, mask)

gm_filter = sitk.ConnectedThresholdImageFilter()
gm_filter.SetLower(1)
gm_filter.SetUpper(100)
gm_filter.AddSeed((50, 50, 50))
gm_image = gm_filter.Execute(masked_image)
Burak
  • 29
  • 1
  • 9
Saxtheowl
  • 4,136
  • 5
  • 23
  • 32
  • 1
    ```--------------------------------------------------------------------------- AttributeError Traceback (most recent call last) Cell In[67], line 31 29 gm_filter.SetLower(1) # Lower threshold for GM intensities 30 gm_filter.SetUpper(100) # Upper threshold for GM intensities ---> 31 gm_filter.SetSeed((50, 50, 50)) 32 gm_image = gm_filter.Execute(masked_image) 33 print("GM image:") AttributeError: 'ConnectedThresholdImageFilter' object has no attribute 'SetSeed'``` You mean SetSeedList? – Burak May 13 '23 at 15:52
  • Thank you for your attention. Unfortunately, I get the same output as it is now. GM image is a blank image. – Burak May 13 '23 at 21:23
  • I use BinaryThreshold() and it's doing fine for now btw. – Burak May 13 '23 at 21:38
  • 1
    Do your separate answer and validate it if you think you fixed your problem – Saxtheowl May 14 '23 at 09:30
  • Although it solves my problem, I didn't write it separately as an answer because it is not a final solution and does not explain the problem with the `ConnectedThresholdImageFilter()` method, but if you say so, so be it. I'm still wondering what the problem with sitk.ConnectedThresholdImageFilter() is, btw. – Burak May 15 '23 at 17:49
0

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: Gray matter image

I still wonder what the problem with sitk.ConnectedThresholdImageFilter() is.

Burak
  • 29
  • 1
  • 9