SimpleITK的`sitk.ConnectedThresholdImageFilter()`输出错误。

huangapple go评论58阅读模式
英文:

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)

这是输出结果:

图像:
SimpleITK的`sitk.ConnectedThresholdImageFilter()`输出错误。
掩模:
SimpleITK的`sitk.ConnectedThresholdImageFilter()`输出错误。
掩模图像:
SimpleITK的`sitk.ConnectedThresholdImageFilter()`输出错误。
灰质图像:
SimpleITK的`sitk.ConnectedThresholdImageFilter()`输出错误。

一切都正常,直到"灰质图像"部分。我真的无法弄清楚我在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 = &quot;&lt;path to data dir&gt;&quot;
image_dir = data_dir + &quot;images/&quot; 
image_filenames = sorted(glob.glob(image_dir + &#39;*.nii.gz&#39;))

mask_dir = data_dir + &quot;masks/&quot; 
mask_filenames = sorted(glob.glob(mask_dir + &#39;*.nii.gz&#39;))

image_filename = image_filenames[0]
mask_filename = mask_filenames[0]

image = sitk.ReadImage(image_filename)
mask = sitk.ReadImage(mask_filename)

print(&quot;image_filename:&quot;, image_filename)
print(&quot;Image:&quot;)
display_image(image)
print(&quot;Mask:&quot;)
display_image(mask)

masked_image = sitk.Mask(image, mask)
print(&quot;Masked image:&quot;)
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(&quot;GM image:&quot;)
display_image(gm_image)

And, here is the output:

Image:
SimpleITK的`sitk.ConnectedThresholdImageFilter()`输出错误。
Mask:
SimpleITK的`sitk.ConnectedThresholdImageFilter()`输出错误。
Masked image:
SimpleITK的`sitk.ConnectedThresholdImageFilter()`输出错误。
GM image:
SimpleITK的`sitk.ConnectedThresholdImageFilter()`输出错误。

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

你尚未指定区域生长的种子点,请在感兴趣的区域内提供一个种子点。

您可以使用ConnectedThresholdImageFilterAddSeed方法来实现。

编辑:让我们尝试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 = &quot;&lt;path to data dir&gt;&quot;
image_dir = data_dir + &quot;images/&quot; 
image_filenames = sorted(glob(image_dir + &#39;*.nii.gz&#39;))

mask_dir = data_dir + &quot;masks/&quot; 
mask_filenames = sorted(glob(mask_dir + &#39;*.nii.gz&#39;))

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)

这是灰质图像的结果:
SimpleITK的`sitk.ConnectedThresholdImageFilter()`输出错误。

我仍然想知道 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(&quot;GM image:&quot;)
display_image(gm_image)

Here's the result for the GM image:
SimpleITK的`sitk.ConnectedThresholdImageFilter()`输出错误。

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

huangapple
  • 本文由 发表于 2023年5月13日 21:24:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/76242955.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定