# Center slice given a markup

**URL:** <https://discourse.slicer.org/t/center-slice-given-a-markup/25983>\
**Category:** Support\
**Created:** [October 31, 2022, 4:27pm UTC](https://discourse.slicer.org/t/center-slice-given-a-markup/25983 "2022-10-31T16:27:19Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![bserrano](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/bserrano/32/17034_2.png) [@bserrano](https://discourse.slicer.org/u/bserrano)\
**Post date:** [October 31, 2022, 4:27pm UTC](https://discourse.slicer.org/t/center-slice-given-a-markup/25983/1 "2022-10-31T16:27:20Z")

</div>

Hi,

I have a markups curve along a vessel and I would like the red slice to be automatically centred on the markups point. This would be very useful when setting a plane perpendicular to the centreline, how can I do this?

Furthermore, is it possible to obtain which coordinate in i,k,j of the perpendicular reformatted images corresponds to each point of the centreline in x,y,z?

Thanks,

Belén

---

<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:** [October 31, 2022, 4:39pm UTC](https://discourse.slicer.org/t/center-slice-given-a-markup/25983/2 "2022-10-31T16:39:32Z")

</div>

> [@bserrano](#):
>
> I have a markups curve along a vessel and I would like the red slice to be automatically centred on the markups point. This would be very useful when setting a plane perpendicular to the centreline, how can I do this?

The easiest is to click on the markup point, which centers all slice views on the point (except the view that you are interacting with, as you would not want the point to jump away from your mouse pointer).

You can also use [Cross-section analysis](https://github.com/vmtk/SlicerExtension-VMTK/blob/master/Docs/CrossSectionAnalysis.md) module in SlicerVMTK extension to explore/adjust a centerline.

> [@bserrano](#):
>
> Furthermore, is it possible to obtain which coordinate in i,k,j of the perpendicular reformatted images corresponds to each point of the centreline in x,y,z?

You can find code snippets for all these kind of commonly needed operations in the script repository. See for example [here](https://slicer.readthedocs.io/en/latest/developer_guide/script_repository.html#get-volume-voxel-coordinates-from-markup-control-point-ras-coordinates).

---

<div class="post-metadata">

**Author:** ![bserrano](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/bserrano/32/17034_2.png) [@bserrano](https://discourse.slicer.org/u/bserrano)\
**Post date:** [November 3, 2022, 4:05pm UTC](https://discourse.slicer.org/t/center-slice-given-a-markup/25983/4 "2022-11-03T16:05:16Z")

</div>

Hi,

The code you suggest ([voxel-coordinates-from-markup](https://slicer.readthedocs.io/en/latest/developer_guide/script_repository.html#get-volume-voxel-coordinates-from-markup-control-point-ras-coordinates)) works perfectly when using the original volume. However it does not work when using the resliced volume.

What I have is a centerline along a vessel. Then I use endoscopy and volume reslice driver modules to obtain a perpendicular view as in:

![imagen](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/e/6/e62b551785dfb0c5fadabd419c9556ff52b6adef.png)

I need to obtain the coordinate of the centerline point of the perpendicular image (red point in the image). What I’m doing now is

```auto
    curveNode = slicer.util.getNode("MarkupsCurve")
    points = vtk.util.numpy_support.vtk_to_numpy(curveNode.GetCurve().GetPoints().GetData())
    
    input("Adjust view") #Manually adjust view (I want to automatize this aswell)
    
    # Create a Fiducial so I can use getCenter
    myFidNode = slicer.mrmlScene.AddNewNodeByClass("vtkMRMLMarkupsFiducialNode")
    myFidNode.SetName("myFidNode")
    
    centers = []
    
    for index in range(0, len(points)):
        
        sliceNodeID = "vtkMRMLSliceNodeRed"
        
        # Get image data from slice view
        sliceNode = slicer.mrmlScene.GetNodeByID(sliceNodeID)
        appLogic = slicer.app.applicationLogic()
        sliceLogic = appLogic.GetSliceLogic(sliceNode)
        sliceLayerLogic = sliceLogic.GetBackgroundLayer()
        reslice = sliceLayerLogic.GetReslice()
        reslicedImage = vtk.vtkImageData()
        reslicedImage.DeepCopy(reslice.GetOutput())
        
        # Create new volume node using resliced image 
        volumeNodeResliced = slicer.mrmlScene.AddNewNodeByClass("vtkMRMLScalarVolumeNode")
        volumeNodeResliced.SetIJKToRASMatrix(sliceNode.GetXYToRAS())
        volumeNodeResliced.SetAndObserveImageData(reslicedImage)
        volumeNodeResliced.CreateDefaultDisplayNodes()
        volumeNodeResliced.CreateDefaultStorageNode()
        
        # Get voxels as a numpy array
        voxels = slicer.util.arrayFromVolume(volumeNodeResliced)
        
        # Get vessel center in (i,j)
        myFidNode.AddFiducialFromArray(points[index])
        center = getCenter("myFidNode", volumeNodeResliced)
        myFidNode.RemoveAllControlPoints()

```

Where getCenter is [voxel-coordinates-from-markup](https://slicer.readthedocs.io/en/latest/developer_guide/script_repository.html#get-volume-voxel-coordinates-from-markup-control-point-ras-coordinates). _voxels_ contains pixel information of the resliced image in redSlice view ([get-reformatted-image-from-a-slice-viewer-as-numpy-array](https://slicer.readthedocs.io/en/latest/developer_guide/script_repository.html#get-reformatted-image-from-a-slice-viewer-as-numpy-array)).

I want **center** and **voxels** to correspond but it’s not working in all slices.  
How can I fix that?

Thanks 😀

---

<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:** [November 3, 2022, 5:50pm UTC](https://discourse.slicer.org/t/center-slice-given-a-markup/25983/5 "2022-11-03T17:50:06Z")

</div>

I would recommend using [Curved Planar Reformat](https://github.com/PerkLab/SlicerSandbox#curved-planar-reformat) module (in Sandbox extension). It provides a straightened image (series of slices obtained by reslicing along the curve) and also a transform that you can conveniently use to transform between the original image space and the straightened image space.

If this is not what you want then you can use or customize [Cross-section analysis](https://github.com/vmtk/SlicerExtension-VMTK/blob/master/Docs/CrossSectionAnalysis.md) module (in SlicerVMTK extension), it probably already contains all the computations you need.
