Not sure what you mean by “get dimensions of mask”, possibly the mask’s bounding box?
If that is correct, then the class you are looking for is LabelShapeStatisticsImageFilter.
The code below illustrates how to use the filter and process the whole “video” (I’m assuming we are working with an US video which can have more than one mask at each time point - multiple objects of interest which were segmented):
import SimpleITK as sitk
file_name = "second_file_with_mask.nii"
segmentation = sitk.ReadImage(file_name)
label_shape_filter = sitk.LabelShapeStatisticsImageFilter()
label_shape_filter.SetBackgroundValue(0)
segmentation_information = []
for i in range(segmentation.GetDepth()): # go over all timepoints
label_shape_filter.Execute(segmentation[:,:,i])
if label_shape_filter.GetNumberOfLabels()>0:
seg_info = {"timepoint":i}
seg_info["stats"] = []
for label in label_shape_filter.GetLabels(): # for each label in this timepoint
label_stats = {"label":label}
label_stats["bounding_box"] = label_shape_filter.GetBoundingBox(label)
label_stats["number_of_pixels"] = label_shape_filter.GetNumberOfPixels(label)
# add any additional shape information we are interested in (centroid, oriented bounding box, length of perimeter...) which is supported by the filter.
seg_info["stats"].append(label_stats)
segmentation_information.append(seg_info)
print(segmentation_information)
Thank you @zivy for your help, your code brings me near the solution. I assume that the mask can be treated as a polygon. I would like to get the corners of the polygon as x,y values.
The closest thing you can do in SimpleITK is use the BinaryContourImageFilter and then get the coordinates of the contour, but these are not ordered, so you would be getting the border pixel locations but in random order.