# Get a leftmost/rightmost coordinate with SimpleITK

**URL:** <https://discourse.itk.org/t/get-a-leftmost-rightmost-coordinate-with-simpleitk/4282>\
**Category:** Uncategorized\
**Created:** [July 20, 2021, 9:27am UTC](https://discourse.itk.org/t/get-a-leftmost-rightmost-coordinate-with-simpleitk/4282 "2021-07-20T09:27:51Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![sandyaa013](https://discourse.itk.org/letter_avatar_proxy/v4/letter/s/65b543/32.png) [@sandyaa013](https://discourse.itk.org/u/sandyaa013)\
**Post date:** [July 20, 2021, 9:27am UTC](https://discourse.itk.org/t/get-a-leftmost-rightmost-coordinate-with-simpleitk/4282/1 "2021-07-20T09:27:52Z")

</div>

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)

---

<div class="post-metadata">

**Author:** ![zivy](https://discourse.itk.org/user_avatar/discourse.itk.org/zivy/32/1726_2.png) [@zivy](https://discourse.itk.org/u/zivy)\
**Post date:** [July 20, 2021, 12:40pm UTC](https://discourse.itk.org/t/get-a-leftmost-rightmost-coordinate-with-simpleitk/4282/2 "2021-07-20T12:40:35Z")

</div>

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):

```auto
# 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]

```

---

<div class="post-metadata">

**Author:** ![sandyaa013](https://discourse.itk.org/letter_avatar_proxy/v4/letter/s/65b543/32.png) [@sandyaa013](https://discourse.itk.org/u/sandyaa013)\
**Post date:** [July 20, 2021, 2:01pm UTC](https://discourse.itk.org/t/get-a-leftmost-rightmost-coordinate-with-simpleitk/4282/3 "2021-07-20T14:01:53Z")

</div>

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

 ![image](https://discourse.itk.org/uploads/default/original/2X/a/a8d044207bcfb58ec1715f593c23d7780784001f.png)

---

<div class="post-metadata">

**Author:** ![zivy](https://discourse.itk.org/user_avatar/discourse.itk.org/zivy/32/1726_2.png) [@zivy](https://discourse.itk.org/u/zivy)\
**Post date:** [July 20, 2021, 2:54pm UTC](https://discourse.itk.org/t/get-a-leftmost-rightmost-coordinate-with-simpleitk/4282/4 "2021-07-20T14:54:18Z")

</div>

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:

```auto
# 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]

```

---

<div class="post-metadata">

**Author:** ![sandyaa013](https://discourse.itk.org/letter_avatar_proxy/v4/letter/s/65b543/32.png) [@sandyaa013](https://discourse.itk.org/u/sandyaa013)\
**Post date:** [July 21, 2021, 3:56am UTC](https://discourse.itk.org/t/get-a-leftmost-rightmost-coordinate-with-simpleitk/4282/5 "2021-07-21T03:56:36Z")

</div>

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. "

---

<div class="post-metadata">

**Author:** ![zivy](https://discourse.itk.org/user_avatar/discourse.itk.org/zivy/32/1726_2.png) [@zivy](https://discourse.itk.org/u/zivy)\
**Post date:** [July 21, 2021, 12:43pm UTC](https://discourse.itk.org/t/get-a-leftmost-rightmost-coordinate-with-simpleitk/4282/6 "2021-07-21T12:43:06Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![sandyaa013](https://discourse.itk.org/letter_avatar_proxy/v4/letter/s/65b543/32.png) [@sandyaa013](https://discourse.itk.org/u/sandyaa013)\
**Post date:** [July 23, 2021, 9:15am UTC](https://discourse.itk.org/t/get-a-leftmost-rightmost-coordinate-with-simpleitk/4282/7 "2021-07-23T09:15:17Z")

</div>

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.

 ![image3](https://discourse.itk.org/uploads/default/original/2X/4/4022b8df7aaa6906ddca19c9a78dd971adb8d9f6.jpeg)  
Thanks a lot again!

Here is my code now:

```auto
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

```

---

<div class="post-metadata">

**Author:** ![dzenanz](https://discourse.itk.org/user_avatar/discourse.itk.org/dzenanz/32/1093_2.png) [@dzenanz](https://discourse.itk.org/u/dzenanz)\
**Post date:** [July 23, 2021, 3:45pm UTC](https://discourse.itk.org/t/get-a-leftmost-rightmost-coordinate-with-simpleitk/4282/8 "2021-07-23T15:45:38Z")

</div>

I think this is more of a Slicer question, than SimpleITK one. Since you already [posted it on the Slicer forum](https://discourse.slicer.org/t/get-a-leftmost-rightmost-coordinate-with-simpleitk/18811/), it is probably best if main conversation is continued there.
