微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何在 Python 中设置 SimpleITK `ConnectedThreshold` 过滤器的连接性?

如何解决如何在 Python 中设置 SimpleITK `ConnectedThreshold` 过滤器的连接性?

如何在 Python 中为应用于单通道 2D 图像的 SimpleITK ConnectedThreshold 过滤器设置像素连接?

import SimpleITK as sitk

# define a simple image from an array
img = sitk.GetimageFromArray([[128,128,0],[128,128],[0,0]])

# get the Region Growing segmentation
out = sitk.ConnectedThreshold(img,seedList=[(1,1)],lower=127,upper=129,replaceValue=42)

# print the result as a vector:
a = sitk.GetArrayViewFromImage(out)
print(a)

我得到了这个输出(在我看来,考虑了 8 个连接区域):

[[42 42  0]
 [42 42 42]
 [ 0 42  0]]

如何获得此输出(使用 4 个连接区域时获得)?

[[ 0 42  0]
 [42 42 42]
 [ 0 42  0]]

解决方法

How can I set in Python the connectivity for the SimpleITK `ConnectedThreshold` filter? 的此评论 https://stackoverflow.com/users/276168/d%c5%beenan 建议采用试错法。

首先原始问题有一个错误:[[128,128,0],[128,128],[0,0]] 必须给出与 4-connectivity 和 8-connectivity 相同的结果,因为:

  1. 对于 4-connectivity:(0,0)(0,1) 是 4-connected,与 (1,1) 处的种子 4-connected。
  2. 对于 8 连接:(0,0)(1,1) 是 8 连接。

因此,根据上述评论,我做了一些实验,我发现:关键字参数名称是 connectivity,值 0 表示 4-connectivity;值 1 表示 8 连通性,在我看来,任何其他值 >1 都会给出每个像素都为零值的图像。

这是代码:

import SimpleITK as sitk
import numpy as np

# define a simple image from an array
v = np.array([[128,0]])
print('input:\n',v)
img = sitk.GetImageFromArray(v)

# get the Region Growing segmentation
out = sitk.ConnectedThreshold(img,seedList=[(1,1)],lower=127,upper=129,replaceValue=42,connectivity=0)

# print the result as a vector:
a = sitk.GetArrayViewFromImage(out)
print('output,connectivity=0\n',a)

# get the Region Growing segmentation
out = sitk.ConnectedThreshold(img,connectivity=1)

# print the result as a vector:
a = sitk.GetArrayViewFromImage(out)
print('output,connectivity=1\n',connectivity=2)

# print the result as a vector:
a = sitk.GetArrayViewFromImage(out)
print('output,connectivity=2\n',a)

这是输出:

input:
 [[128   0   0]
 [  0 128   0]
 [  0   0   0]]
output,connectivity=0
 [[ 0  0  0]
 [ 0 42  0]
 [ 0  0  0]]
output,connectivity=1
 [[42  0  0]
 [ 0 42  0]
 [ 0  0  0]]
output,connectivity=2
 [[0 0 0]
 [0 0 0]
 [0 0 0]]

无论如何我找不到上述文档...所以我的结论是 SimpleITK 并不那么简单 :-)

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。