Get a leftmost/rightmost coordinate with SimpleITK

I’m trying to do a project on slicer with simpleITK.

I create a labelmap volume node and read it as a SimpleITKimage.
Now I show this labelmap volume in red, green and yellow slice window.
I want to know how to get a leftmost/rightmost coordinate in slice window.
Since I’m not familiar with SimpleITK, I have no idea about the problem.
Thank you.

Here is my code:
labelmapVolumeNode = slicer.mrmlScene.AddNewNodeByClass(“vtkMRMLLabelMapVolumeNode”)
labelmapVolumeNode.SetName(“Test_volume”)
slicer.modules.segmentations.logic().ExportAllSegmentsToLabelmapNode(segmentationNode, labelmapVolumeNode, slicer.vtkSegmentation.EXTENT_REFERENCE_GEOMETRY)
test_volume = slicer.util.getNode(“Test_volume”)
inputImage = sitkUtils.PullVolumeFromSlicer(test_volume)

Hello @sandyaa013,

Not sure that this answers your question, not too clear what it is you want to do, so a bit of guessing here:

If you want to get the world coordinates of a specific index in the image (corners of the volume):

# two opposite corners, add all other index combinations if needed
corner_indexes = [[0]*inputImage.GetDimension(),[sz-1 for sz in inputImage.GetSize()]]
corner_coordinates = [inputImage.TransformIndexToPhysicalPoint(index) for index in corner_indexes]

Sorry, I explain my problem again.
What I want to do is to get the coordinate where red arrow points to.
Thank you.

Hi @sandyaa013,

Thanks for the clarification. So there’s one additional step, getting the bounding box of the specific label of interest. The code should look something like this:

# Assuming inputImage has discrete labels (e.g. 1, 2)
label_shape_filter = sitk.LabelShapeStatisticsImageFilter()
label_shape_filter.Execute(inputImage) 

# The bounding box's first "dim" entries are the starting index and
# last "dim" entries the size. Get the bounding box for label 2
bounding_box = label_shape_filter.GetBoundingBox(2)
corner_indexes = [bounding_box[0:inputImage.GetDimension()]]
corner_indexes.append([i+sz-1 for i, sz in zip(min_indexes, bounding_box[inputImage.GetDimension():])])

corner_coordinates = [inputImage.TransformIndexToPhysicalPoint(index) for index in corner_indexes]
1 Like

Thanks a lot !
However, I’m very new to simpleITK. I’m not sure about what labels mean.

I search on google and it says that LabelShapeStatisticsImageFilter() converts a label image to a label map and valuates the shape attributes.

I’m confused about it beacuse I think my input is already a label map node before reading as an inputImage.

The error I get is " itk::ERROR: itk::ERROR: LabelMap(000001B9E8DB2570): No label object with label 2. "

Hello @sandyaa013,

Please don’t copy paste code blindly.

In the code, the comment says “Get the bounding box for label 2” . If you only have a single label then the only valid value is 1. Likely that is your situation.

Thank you.
However, I try the code with index= (image.GetSize()[0], image.GetSize()[1], image.GetSize()[2]) and use FiducialNode to show in 3d view, the position looks like not situated at the corner.
Here is my testing code:
inputImage.TransformIndexToPhysicalPoint(index)

Then, I try a stupid way to find the leftmost/rightmost coordinates.
I visualize what I’m going to do. I want to find the coordinate of every point on green line, and than I will fill the hole by replacing 0 with 1 in the image.


Thanks a lot again!

Here is my code now:

image = sitkUtils.PullVolumeFromSlicer(test_volume)
direction = image.GetDirection()
image = sitk.Flip(image, [direction[0] < 0, direction[4] < 0, direction[8] < 0])
image_shape = image.GetSize()
target_img = image[0:image_shape[0]-1, 0:image_shape[1]-1, 0]
array = sitk.GetArrayFromImage(target_img)

for j in range(image_shape[0]-1):
    for k in range(image_shape[1]-1):
        if(array[k][j]>0):
            print(k,j,array[k][j])
            break

I think this is more of a Slicer question, than SimpleITK one. Since you already posted it on the Slicer forum, it is probably best if main conversation is continued there.