Alessandro Jacopson
Alessandro Jacopson

Reputation: 18593

How can I set in Python the connectivity for the SimpleITK `ConnectedThreshold` filter?

How can I set in Python the pixel connectivity for the SimpleITK ConnectedThreshold filter applied to a single channel 2D image?

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)

I get this output (it seems to me 8-connected regions is considered):

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

How can I get this output (obtained when using the 4-connected regions)?

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

Upvotes: 0

Views: 871

Answers (1)

Alessandro Jacopson
Alessandro Jacopson

Reputation: 18593

This comment How can I set in Python the connectivity for the SimpleITK `ConnectedThreshold` filter? by https://stackoverflow.com/users/276168/d%c5%beenan suggests a trial and error approach.

First of all the original question has an error: [[128,128,0],[128,128,128],[0,128,0]] must give the same result with 4-connectivity and 8-connectivity because:

  1. for 4-connectivity: (0,0) is 4-connected with (0,1) that is 4-connected with the seed at (1,1).
  2. for 8-connectivity: (0,0) is 8-connected with (1,1).

So, following the above mentioned comment I did some experiments and I found that: the keyword argument name is connectivity and the value 0 means 4-connectivity; the value 1 means 8-connectivity and it seems to me that any other value >1 gives an image where every pixel has a zero value.

This is the code:

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)

This is the output:

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]]

Anyway I can't find a documentation for the above... so my conclusion is that SimpleITK is not so simple :-)

Upvotes: 1

Related Questions