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?
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).
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.
hello,
I want to get centroid of all the roi including all labels, getcentroid of simpleitk works for specific labels, but to find the centroid of summing all labels?
import SimpleITK as sitk
# Our multi-label image
multi_label_image = sitk.Image([3]*3, sitk.sitkUInt8)
multi_label_image[0,0,0] = 1
multi_label_image[0,1,0] = 2
multi_label_image[0,0,1] = 2
multi_label_image[0,1,1] = 1
multi_label_image[0,2,1] = 3
multi_label_image[0,0,2] = 3
label_shape_filter = sitk.LabelShapeStatisticsImageFilter()
# Get the centroid per label
print('Multiple labels and their centroids:')
label_shape_filter.Execute(multi_label_image)
for label in range(1, label_shape_filter.GetNumberOfLabels()+1):
print(f'label{label}: centroid {label_shape_filter.GetCentroid(label)}')
# Get the centroid treating all labels as a single entity, assuming
# zero is the background label
background_label = 0
print('Multiple labels treated as a single label and its centroid:')
label_shape_filter.Execute(multi_label_image!=background_label)
for label in range(1, label_shape_filter.GetNumberOfLabels()+1):
print(f'label{label}: centroid {label_shape_filter.GetCentroid(label)}')
Question isn’t clear to me, as it is up to you to confirm that the code does what you want in a correct fashion. The code I provided illustrated how to (1) Get the centroid on a per label basis. (2) convert the per label mask to a single label mask and get the centroid of that.
If that is what you want your code looks ok, though there are some strange lines: mask = mask = sitk.ReadImage(maskName)
Again to clarify, when the mask/segmentation has multiple labels and you want the statistics for the object that consists of all labels we simply create a binary mask that represents it, mask!=background_label. If you want to combine a specific subset of labels into a single label, you can use the ChangeLabelImageFilter to change the subset of labels into a single label and then compute the statistics on that.
yes it worked, thanks for decoding code for me, i wanted to combine all labels and get the centroid of it. Please resolve my doubt that while calculating centroid for particular label ,do the function getcentroid() consider the dimension of particular label or whole image?
hello zivy,
can we find centroid using numpy value? my image is 3d brain image and all the non brain area set to 0, I got brain volume using size_brain=np.sum(np.nonzero(img)) , now i want to get centroid of brain area. so i have to get array from image or can assign mask as np.sum(np.nonzero(img))?
can we create binary mask here also ? whole brain pixel vs background pixels which is set to 0?
background_label = 0
print('whole brain area treated as a single label and its centroid:')
stats.Execute(mask>background_label)
for label in range(1, stats.GetNumberOfLabels()+1):
centroidofwb=stats.GetCentroid(label)
print("centroidofwb",centroidofwb)
You should not use numpy for these types of computations. Medical images are most often non-isotropic and they are in a specific location in physical space. When using numpy all of this information is lost as it treats the data as an isotropic array with the origin at [0,0,0].
The centroid is just summing the coordinates of the label voxels divided by the number of voxels, \left(\frac{\Sigma x_i}{n}, \frac{\Sigma y_i}{n},\frac{\Sigma z_i}{n}\right). This is in physical space.