Connected components in python

(Krishna Nanda) #1

Hello all,

I am very new to SimpleITK. I am trying to get all the connected components from a 3D binary image (including the pixel locations of the various components) with multiple masks. The 3D binary image is of “Image” type and was itself obtained after thresholding. I tried using ConnectedComponentImageFilter and BinaryImageToLabelMapFilter, but couldn’t find methods that would give the actual locations of the pixels belonging to the components. How would I do this?


(Ziv Yaniv) #2

Hello @Krishna_Nanda,

Welcome to SimpleITK!

Not exactly sure what you want to do but here goes:

  1. You have a binary volume with multiple objects (connected regions with value 1).
  2. Use ConnectedComponentImageFilter to mark each region/object with a unique label.
  3. Use LabelShapeStatisticsImageFilter to obtain information on the shape of each object, then use the method GetCentroid which will give you the 3D “location” of each object based on the label you provide (you got these in previous step).

If you need the 3D location of a specific pixel/voxel in an image you can use the Image’s TransformIndexToPhysicalPoint.

As you are new to SimpleITK I recommend that your read about the Fundamental Concepts and the Common Conventions used by the toolkit. Also, possibly take a look at the toolkit’s Jupyter Notebook repository which illustrates these and shows various usage cases.