sitk.ConnectedComponent is order deterministic?

Hello I try to understand is the order (int value) of connected components is always the same if we are executing algorithm on the same array?

for reference description of connected components in simple Itk

ConnectedComponentImageFilter labels the objects in a binary image (non-zero pixels are considered to be objects, zero-valued pixels are considered to be background). Each distinct object is assigned a unique label. The filter experiments with some improvements to the existing implementation, and is based on run length encoding along raster lines. If the output background value is set to zero (the default), the final object labels start with 1 and are consecutive. If the output background is set to a non-zero value (by calling the SetBackgroundValue() routine of the filter), the final labels start at 0, and remain consecutive except for skipping the background value as needed. Objects that are reached earlier by a raster order scan have a lower label. This is different to the behaviour of the original connected component image filter which did not produce consecutive labels or impose any particular ordering.

Hello @Jakub_Mitura,

The ConnectedComponents algorithm should be deterministic. That is given an input and parameters the output should be the same. In the above: "Objects that are reached earlier by a raster order scan have a lower label. " describes the expected order of the labels of the components.

2 Likes

Hello @Jakub_Mitura,

It should be according to the documentation you quoted and is readily verifiable:

import SimpleITK as sitk

binary_image = sitk.Image([128,128], sitk.sitkUInt8)

# four "objects" with increasing raster order scanning (scan along x, then y)
binary_image[10:20,10:20] = 1 # expected to have a label of 1
binary_image[25:35, 10:20] = 1 # expected to have a label of 2
binary_image[25:35,25:35] = 1 # expected to have a label of 3
binary_image[40:50,40:50] = 1 # expected to have a label of 4

sitk.Show(sitk.ConnectedComponent(binary_image))

I would not rely on this in terms of identifying specific objects in an image. A simple example, frontal chest x-ray and the algorithm segmented the lungs. Let’s assume correct segmentation without any spurious detections. In a real world scenario, you should not expect label 1 to be the right lung and label 2 to be the left lung:

  1. Some people have a single lung.
  2. Even if you have two labels, you don’t know if label 1 is the left lung or the right lung because that depends on the image view (posterior-anterior, the more common view, or anterior-posterior if the patient is in worse condition).
3 Likes

ok thanks! I understand the idea - but I was considering exactly the same mask loaded from hard drive without any futher processing - so in the case when there is no variability as far as I understand sth should give the same labeling , thanks @zivy ! and @blowekamp

2 Likes