# Reslice / Reorient Slice view

**URL:** <https://discourse.slicer.org/t/reslice-reorient-slice-view/10093>\
**Category:** Support\
**Created:** [February 3, 2020, 7:51pm UTC](https://discourse.slicer.org/t/reslice-reorient-slice-view/10093 "2020-02-03T19:51:21Z")\
**Posts on this page:** 5\
**Page:** 2

<div class="post-metadata">

**Author:** ![manjula](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/manjula/32/80981_2.png) [@manjula](https://discourse.slicer.org/u/manjula)\
**Post date:** [February 8, 2020, 7:30pm UTC](https://discourse.slicer.org/t/reslice-reorient-slice-view/10093/21 "2020-02-08T19:30:54Z")

</div>

Prof @lassoan ,

I was following the discussion at

> [@lassoan](#):
>
> [github.com/Slicer/Slicer](https://github.com/Slicer/Slicer/pull/1274)
> 
> #### [ENH: Add vtkITK filter for computing label margins](https://github.com/Slicer/Slicer/pull/1274)
> 
> `Slicer:master` ← `Sunderlandkyl:vtkitk_margin`
> 
> opened Dec 3, 2019
> 
> [![Sunderlandkyl](https://avatars3.githubusercontent.com/u/9222709?v=4) Sunderlandkyl ](https://github.com/Sunderlandkyl)
> 
> [+302 -101](https://github.com/Slicer/Slicer/pull/1274/files)

At this momement, if we tell to grow the margin by 0.5 mm in a cylinder, is only 0.25 mm is grown on either side ? so if we want 0.5 in one side do we need to grow it by 1 mm ?

---

<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:** [February 8, 2020, 8:16pm UTC](https://discourse.slicer.org/t/reslice-reorient-slice-view/10093/22 "2020-02-08T20:16:21Z")

</div>

Margin size will mean the how much a boundary will be moved (if you set 1mm as margin then the object will be 2mm wider).

---

<div class="post-metadata">

**Author:** ![manjula](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/manjula/32/80981_2.png) [@manjula](https://discourse.slicer.org/u/manjula)\
**Post date:** [February 13, 2020, 8:03pm UTC](https://discourse.slicer.org/t/reslice-reorient-slice-view/10093/23 "2020-02-13T20:03:09Z")

</div>

Dear Prof @lassoan @Juicy

Now as margin growing working perfectly we started working on analysis of data and we noticed a very minor offset of the ROI.

We expected the Bar of the ROI to align to all the fiducials. we initially thought it was due to error in placement of marks and we incorporated auto update and played around

# Update slice plane automatically whenever points are changed

markupObservation = [markups, markups.AddObserver(slicer.vtkMRMLMarkupsNode.PointModifiedEvent, UpdateSlicePlane, 0)]

In Slice view it is nicely in plane but the ROI does not fit perfectly to that. Is this expected ? Is there something that need to be changed in the ROI code ?

 ![Screenshot](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/7/a/7a07b942872289d3ab1a1717418ee1e8c0fb35a5.jpeg)

 ![Screenshot_2](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/7/3/730dac6a30ff2235845bcf519ae00d6f1535af4a.jpeg)

The Coplanar script is working perfectly i think as you can see. It is something wrong with the ROI.

 ![Screenshot_3](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/5/a/5abef188bf3e2c5713d7e130635c29a39c58ebaa.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:** [February 13, 2020, 8:24pm UTC](https://discourse.slicer.org/t/reslice-reorient-slice-view/10093/24 "2020-02-13T20:24:22Z")

</div>

How did you position the ROI? Is it the oriented bounding box provided by Segment statistics?

---

<div class="post-metadata">

**Author:** ![manjula](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/manjula/32/80981_2.png) [@manjula](https://discourse.slicer.org/u/manjula)\
**Post date:** [February 13, 2020, 8:31pm UTC](https://discourse.slicer.org/t/reslice-reorient-slice-view/10093/25 "2020-02-13T20:31:04Z")

</div>

I could not figure out how to gt the bounding box with the segment statistics.  
I did it with the Script in the repository.

segmentationNode = getNode(‘Segmentation’)

# Compute bounding boxes

import SegmentStatistics  
segStatLogic = SegmentStatistics.SegmentStatisticsLogic()  
segStatLogic.getParameterNode().SetParameter(“Segmentation”, segmentationNode.GetID())  
segStatLogic.getParameterNode().SetParameter(“LabelmapSegmentStatisticsPlugin.obb\_origin\_ras.enabled”,str(True))  
segStatLogic.getParameterNode().SetParameter(“LabelmapSegmentStatisticsPlugin.obb\_diameter\_mm.enabled”,str(True))  
segStatLogic.getParameterNode().SetParameter(“LabelmapSegmentStatisticsPlugin.obb\_direction\_ras\_x.enabled”,str(True))  
segStatLogic.getParameterNode().SetParameter(“LabelmapSegmentStatisticsPlugin.obb\_direction\_ras\_y.enabled”,str(True))  
segStatLogic.getParameterNode().SetParameter(“LabelmapSegmentStatisticsPlugin.obb\_direction\_ras\_z.enabled”,str(True))  
segStatLogic.computeStatistics()  
stats = segStatLogic.getStatistics()

# Draw ROI for each oriented bounding box

import numpy as np  
for segmentId in stats[‘SegmentIDs’]:  
# Get bounding box  
obb\_origin\_ras = np.array(stats[segmentId,“LabelmapSegmentStatisticsPlugin.obb\_origin\_ras”])  
obb\_diameter\_mm = np.array(stats[segmentId,“LabelmapSegmentStatisticsPlugin.obb\_diameter\_mm”])  
obb\_direction\_ras\_x = np.array(stats[segmentId,“LabelmapSegmentStatisticsPlugin.obb\_direction\_ras\_x”])  
obb\_direction\_ras\_y = np.array(stats[segmentId,“LabelmapSegmentStatisticsPlugin.obb\_direction\_ras\_y”])  
obb\_direction\_ras\_z = np.array(stats[segmentId,“LabelmapSegmentStatisticsPlugin.obb\_direction\_ras\_z”])  
# Create ROI  
segment = segmentationNode.GetSegmentation().GetSegment(segmentId)  
roi=slicer.mrmlScene.AddNewNodeByClass(“vtkMRMLAnnotationROINode”)  
roi.SetName(segment.GetName()+’ bounding box’)  
roi.SetXYZ(0.0, 0.0, 0.0)  
roi.SetRadiusXYZ(_(0.5_obb\_diameter\_mm))  
# Position and orient ROI using a transform  
obb\_center\_ras = obb\_origin\_ras+0.5\*(obb\_diameter\_mm[0] \* obb\_direction\_ras\_x + obb\_diameter\_mm[1] \* obb\_direction\_ras\_y + obb\_diameter\_mm[2] \* obb\_direction\_ras\_z)  
boundingBoxToRasTransform = np.row\_stack((np.column\_stack((obb\_direction\_ras\_x, obb\_direction\_ras\_y, obb\_direction\_ras\_z, obb\_center\_ras)), (0, 0, 0, 1)))  
boundingBoxToRasTransformMatrix = slicer.util.vtkMatrixFromArray(boundingBoxToRasTransform)  
transformNode = slicer.mrmlScene.AddNewNodeByClass(‘vtkMRMLTransformNode’)  
transformNode.SetAndObserveMatrixTransformToParent(boundingBoxToRasTransformMatrix)  
roi.SetAndObserveTransformNodeID(transformNode.GetID())

[Previous page](https://discourse.slicer.org/t/reslice-reorient-slice-view/10093.md?page=1)
