# Get a leftmost/rightmost coordinate with SimpleITK

**URL:** <https://discourse.slicer.org/t/get-a-leftmost-rightmost-coordinate-with-simpleitk/18811>\
**Category:** Support\
**Created:** [July 20, 2021, 8:45am UTC](https://discourse.slicer.org/t/get-a-leftmost-rightmost-coordinate-with-simpleitk/18811 "2021-07-20T08:45:51Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![sandyaa0313](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/sandyaa0313/32/11566_2.png) [@sandyaa0313](https://discourse.slicer.org/u/sandyaa0313)\
**Post date:** [July 20, 2021, 8:45am UTC](https://discourse.slicer.org/t/get-a-leftmost-rightmost-coordinate-with-simpleitk/18811/1 "2021-07-20T08:45:51Z")

</div>

Operating system: windows  
Slicer version: 4.11.20200930

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:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [July 20, 2021, 12:10pm UTC](https://discourse.slicer.org/t/get-a-leftmost-rightmost-coordinate-with-simpleitk/18811/2 "2021-07-20T12:10:04Z")

</div>

You can use `GetBounds` method of the volume node to get physical bounds (in RAS coordinate system).

Note that this is not very useful (gives just a rough approximation) in general, because the volume axes can be in arbitrary order and direction in physical space. If you want your code to be always accurate then you can use the IJKToRAS transform (that specifies origin, spacing, and IJK axis directions) and the image extent. You can get them by calling GetIJKToRAS method of the volume node and GetExtent method of the image data.

---

<div class="post-metadata">

**Author:** ![sandyaa0313](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/sandyaa0313/32/11566_2.png) [@sandyaa0313](https://discourse.slicer.org/u/sandyaa0313)\
**Post date:** [July 21, 2021, 7:47am UTC](https://discourse.slicer.org/t/get-a-leftmost-rightmost-coordinate-with-simpleitk/18811/3 "2021-07-21T07:47:15Z")

</div>

I’m not sure whether I complete the IJKToRAS transform.  
However, I have the error says that “‘Image’ object has no attribute ‘GetExtent’”.  
Thanks a lot.

I import SimpleITK as below:

import SimpleITK as sitk  
import sitkUtils

Here is my code now:

test\_volume = slicer.util.getNode(“Test\_volume”)  
direction = vtk.vtkMatrix4x4()  
test\_volume.GetRASToIJKMatrix(direction)  
test\_volume.ApplyTransformMatrix(direction)  
image = sitkUtils.PullVolumeFromSlicer(test\_volume)  
dims = image.GetExtent()

---

<div class="post-metadata">

**Author:** ![sandyaa0313](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/sandyaa0313/32/11566_2.png) [@sandyaa0313](https://discourse.slicer.org/u/sandyaa0313)\
**Post date:** [July 21, 2021, 10:25am UTC](https://discourse.slicer.org/t/get-a-leftmost-rightmost-coordinate-with-simpleitk/18811/4 "2021-07-21T10:25:48Z")

</div>

Now I fixed the error, but I don’t understand the function output.  
From image.GetSize(), I get (283, 206, 265).  
From vimage.GetExtent(), I get (0, 282, 0, 205, 0, 264).  
I’m not sure how to get the coordinate from them.  
Thank you.

Here is my code now:  
test\_volume = slicer.util.getNode(“Test\_volume”)  
direction = vtk.vtkMatrix4x4()  
test\_volume.GetRASToIJKMatrix(direction)  
test\_volume.ApplyTransformMatrix(direction)  
image = sitkUtils.PullVolumeFromSlicer(test\_volume)  
direction = image.GetDirection()  
print(image.GetSize())  
vimage = test\_volume.GetImageData()  
dims = vimage.GetExtent()  
print(dims)

The coordinates I want to get is where red arrow points to.

 ![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/6/5/65eb4a3578b380ec8d41cfda62fccfa80a7eb9e1.png)

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [July 21, 2021, 12:54pm UTC](https://discourse.slicer.org/t/get-a-leftmost-rightmost-coordinate-with-simpleitk/18811/5 "2021-07-21T12:54:23Z")

</div>

If you are interested in the bounding box of the content of a segment then you can get that directly from the segmentation object (in voxel coordinates). You can get oriented bounding box from Segment Statistics module.

There are so many kind of bounding boxes (aligned with image axes, patient axes, principal axes, oriented bounding box; potentially transformed using linear or non-linear transforms; in voxel or physical coordinate system) and so many ways to get them that it would help if you could write a bit about what would you like to use this for and how, because it would just take too much time to describe all the possibilities.

---

<div class="post-metadata">

**Author:** ![sandyaa0313](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/sandyaa0313/32/11566_2.png) [@sandyaa0313](https://discourse.slicer.org/u/sandyaa0313)\
**Post date:** [July 23, 2021, 9:03am UTC](https://discourse.slicer.org/t/get-a-leftmost-rightmost-coordinate-with-simpleitk/18811/6 "2021-07-23T09:03:06Z")

</div>

My goal is to receive the volume of red and blue area. Here is my algorithm.

1. Get the leftmost and rightmost coordinate in red slice window.
2. Go through the image, and fill the hole by replacing 0 with 1, then the blue area can be created.

My problems are:

- Because red and black area are two volume, I also wonder that if is possible to combine them into one labelmap volume. Since the the slice window can only show one labelmap in one time.
- map and visualize the point in 2d slice into 3d view. I used TransformIndexToPhysicalPoint() and fiducial node before, but the position was weird.

```auto
# It doesn't look like situated at the corner.
index= (image.GetSize()[0], image.GetSize()[1], image.GetSize()[2])
image.TransformIndexToPhysicalPoint(index)

```

I will explain my problem in detail below.

Now I’m trying to find the coordinate of green line in the red slice window. Just like the picture below. I want to go through every slice in red slice window and find the coordinates.

 ![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/8/b/8b336509dc577dd8c6b8d0210e2034e3f037c980.jpeg)  
 ![image2](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/2/6/269aef1b7485c3247c431997f334c76bb3829e70.jpeg)

I first cut the image into 2d named target image for testing.  
Once I touch a point that value is not zero, I break the loop.  
I want to know how to check this point’s coordinate in 3d view, in order to check this code works. Then I can modify the parameters to find the coordinates of green line.  
Thank you.

Here is my code:

```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):
            # I want to know how to map this point to 3d view.
            print(k,j,array[k][j])
            break

```

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [July 23, 2021, 12:46pm UTC](https://discourse.slicer.org/t/get-a-leftmost-rightmost-coordinate-with-simpleitk/18811/7 "2021-07-23T12:46:21Z")

</div>

You cannot implement such low-level image processing in Python. They would be way too slow. For example to iterate through a tiny 100x100x100 image you would do 1 million iterations. In C++ or other low-level languages this would take a fraction of a second, while in Python in can take minutes.

Instead, you need to think about how you can implement a feature using high-level operations. For example, you can do the projection that you described above by copying the entire shape shifted by one pixel, repeated 100 times, and it would be magnitudes faster than implement iterating through each pixel of the image.

I guess you are still working on this:

> [@Do projection from polydata to a model surface](https://discourse.slicer.org/t/do-projection-from-polydata-to-a-model-surface/18451/5):
>
> I’ve tried, but this module seems not to suit for my case. I already have chest model so I don’t have to use markup points to create the chest wall. Because the chest wall in breast reconstruction module is created by markup points, and markup points are situated on the breast model, it can create a closed model obviously. However, in my module, the chest wall model is from CT, and the breast model is from 3d scan, they are aligned but with some distance. I think these two modules have diffe…

I would recommend to have a look at the existing Slicer module for almost the same task ([GitHub - PerkLab/BreastReconstruction: Tool for breast reconstruction surgery planning](https://github.com/PerkLab/BreastReconstruction)) because we already have this kind of projection implemented in it.

---

<div class="post-metadata">

**Author:** ![sandyaa0313](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/sandyaa0313/32/11566_2.png) [@sandyaa0313](https://discourse.slicer.org/u/sandyaa0313)\
**Post date:** [August 5, 2021, 7:31am UTC](https://discourse.slicer.org/t/get-a-leftmost-rightmost-coordinate-with-simpleitk/18811/8 "2021-08-05T07:31:49Z")

</div>

Ok! I understand. I’ll reconsider my project. Thanks for your patience.
