英文:
SimpleITK Filtering with sitk.ConnectedThresholdImageFilter() wrong output
问题
以下是您要翻译的部分:
我已尝试多种方法来使用sitk.ConnectedThresholdImageFilter()
分割灰质、白质和脑脊液图像,但不幸的是,我无法成功。请告诉我我做错了什么。
这是示例代码:
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("图像:")
display_image(image)
print("掩模:")
display_image(mask)
masked_image = sitk.Mask(image, mask)
print("掩模图像:")
display_image(masked_image)
gm_filter = sitk.ConnectedThresholdImageFilter()
gm_filter.SetLower(1) # 灰质强度的下限
gm_filter.SetUpper(100) # 灰质强度的上限
gm_image = gm_filter.Execute(masked_image)
print("灰质图像:")
display_image(gm_image)
这是输出结果:
一切都正常,直到"灰质图像"部分。我真的无法弄清楚我在sitk.ConnectedThresholdImageFilter()
方法上做错了什么。提前感谢。
英文:
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.
答案1
得分: 1
你尚未指定区域生长的种子点,请在感兴趣的区域内提供一个种子点。
您可以使用ConnectedThresholdImageFilter
的AddSeed
方法来实现。
编辑:让我们尝试AddSeed
方法。
例如:
from glob import glob
import SimpleITK as sitk
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)
英文:
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)
答案2
得分: 0
我可以使用名为 sitk.BinaryThreshold()
和 sitk.And()
的方法一起来实现我所需的类似功能。
lower_threshold, upper_threshold = 100, 200 # 灰质的阈值
gm_image = sitk.BinaryThreshold(image, lower_threshold, upper_threshold, 1, 0)
gm_image = sitk.And(gm_image, mask)
print("灰质图像:")
display_image(gm_image)
我仍然想知道 sitk.ConnectedThresholdImageFilter()
有什么问题。
英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论