如何在Python中设置SimpleTk`ConnectedThreshold`过滤器的连接性?
如何在Python中设置应用于单通道2D图像的SimpleIK如何在Python中设置SimpleTk`ConnectedThreshold`过滤器的连接性?,python,image-processing,itk,simpleitk,Python,Image Processing,Itk,Simpleitk,如何在Python中设置应用于单通道2D图像的SimpleIKConnectedThreshold过滤器的像素连接 import SimpleITK as sitk # define a simple image from an array img = sitk.GetImageFromArray([[128,128,0],[128,128,128],[0,128,0]]) # get the Region Growing segmentation out = sitk.ConnectedT
ConnectedThreshold
过滤器的像素连接
import SimpleITK as sitk
# define a simple image from an array
img = sitk.GetImageFromArray([[128,128,0],[128,128,128],[0,128,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个连通区域):
如何获得此输出(使用4连接区域时获得)
这一评论暗示了一种尝试和错误的方法
首先,原始问题有一个错误:[[128128,0],[128128128],[0128,0]]
必须给出与4连接性和8连接性相同的结果,因为:
(0,0)
与(0,1)
进行4-连接,该(0,1)
与(1,1)
处的种子进行4-连接(0,0)
与(1,1)
进行8-连接connectivity
,值0
表示4-连接性;值1
表示8连接性,在我看来,任何其他值>1
给出的图像中每个像素都有一个零值
代码如下:
import SimpleITK as sitk
import numpy as np
# define a simple image from an array
v = np.array([[128,0,0],[0,128,0],[0,0,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,seedList=[(1,1)],lower=127,upper=129,replaceValue=42,connectivity=1)
# print the result as a vector:
a = sitk.GetArrayViewFromImage(out)
print('output, connectivity=1\n',a)
# get the Region Growing segmentation
out = sitk.ConnectedThreshold(img,seedList=[(1,1)],lower=127,upper=129,replaceValue=42,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]]
无论如何,我找不到上面的文档。。。所以我的结论是SimpleTk并不是那么简单:-)可能是我的设置
连接性
参数:@Dženan,好吧,但我如何用Python编写它呢?可能是…replaceValue=42,连接性=FaceConnectivity
或类似的东西。嗯,相对于原始ITK来说,它很简单。@DaveChen:-)我听说你是SimpleTk的开发人员:有专门针对Python的文档吗?否则,我最好直接将ITK与Python结合使用,不是吗?谢谢。在本例中,与许多其他ITK过滤器一样,SimpleTk的文档直接从ITK的文档中提取。从文档的角度来看,它们没有太大的不同。我不喜欢ITK的模板化以及它在python中的公开方式,但是如果它更适合您的需要,那就试试吧。
import SimpleITK as sitk
import numpy as np
# define a simple image from an array
v = np.array([[128,0,0],[0,128,0],[0,0,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,seedList=[(1,1)],lower=127,upper=129,replaceValue=42,connectivity=1)
# print the result as a vector:
a = sitk.GetArrayViewFromImage(out)
print('output, connectivity=1\n',a)
# get the Region Growing segmentation
out = sitk.ConnectedThreshold(img,seedList=[(1,1)],lower=127,upper=129,replaceValue=42,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]]