Hi there,
I’ve come across some unexpected behavior while using the procedural interface of the ConnectedComponentImageFilter in SimpleITK. When applying the filter in order to extract the largest connected components in a segmentation, I get only 5 connected components, while I expect hundreds or thousands. I’ve also doubled checked this by computing the connected components in Paraview.
In order to reproduce this, I’ve uploaded a version of the segmentation I am working on here, along with the results I get from Paraview and a minimal example of the code to reproduce this.
Blockquote
import SimpleITK as sitk
import numpy as np
# create some dummy checkerboard data
data = np.kron([[1, 0] * 20, [0, 1] *20] * 20, np.ones((10, 10, 10)))
some_zeros = np.zeros((10, 400, 400))
data = np.vstack((data, some_zeros, data,
data, some_zeros, data,
data, some_zeros, data,
data, some_zeros, data,
data, some_zeros, data,
data, some_zeros, data,
data, some_zeros, data,
data, some_zeros, data,
data, some_zeros, data,
data, some_zeros, data,
data, some_zeros, data,
data, some_zeros, data))
conn_comps = sitk.GetImageFromArray(data)
conn_comps = sitk.Cast(conn_comps, sitk.sitkInt8)
#print("Dummy Image before {}".format(conn_compsconn_comps))
conn_comps = sitk.ConnectedComponent(conn_comps)
#print("Dummy Image after {}".format(conn_comps))
sitk.WriteImage(conn_comps, 'conn_comps.mhd')
segmentation =sitk.ReadImage('segmentation.mhd')
# some cleanup
segmentation = sitk.BinaryOpeningByReconstruction(segmentation, 5, sitk.sitkBall)
fully_connected_neighborhood = False # face-connected neighbors only
# conn_comps = sitk.ScalarConnectedComponent(result, result, 10, fully_connected_neighborhood)
conn_comps = sitk.ConnectedComponent(segmentation, fully_connected_neighborhood)
sitk.WriteImage(conn_comps, 'conn_comps_2.mhd')
Blockquote
The upper dummy example creates a checkerboard pattern, for which the connected component filter produces the expected results. However, for the segmentation, it does not and I cannot figure out why.
I’ve tried v1.2.4 as well as SimpleITK 2.0 Release Candidate 1.