# Resampling Segmentation and Volume about a User-Defined Axis For Plotting

**URL:** <https://discourse.slicer.org/t/resampling-segmentation-and-volume-about-a-user-defined-axis-for-plotting/8392>\
**Category:** Support\
**Created:** [September 11, 2019, 10:17pm UTC](https://discourse.slicer.org/t/resampling-segmentation-and-volume-about-a-user-defined-axis-for-plotting/8392 "2019-09-11T22:17:33Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![abniesen](https://avatars.discourse-cdn.com/v4/letter/a/a9a28c/32.png) [@abniesen](https://discourse.slicer.org/u/abniesen)\
**Post date:** [September 11, 2019, 10:17pm UTC](https://discourse.slicer.org/t/resampling-segmentation-and-volume-about-a-user-defined-axis-for-plotting/8392/1 "2019-09-11T22:17:33Z")

</div>

Hi,

I have segmented a CT scan of the proximal femur as shown below.  
 ![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/2/8/28aebe03715d1201da6463406e3dfcc261ccaedf.png)  
I am looking to plot the cross-sectional area of this segmentation along the axis of the femoral neck using SliceAreaPlot.py (([Example module that computes and plots the cross sectional area of each visible segment. Direction of cross-section can be picked. · GitHub](https://gist.github.com/lassoan/62370c6b0552f7df1111eb7fc37abfd2)).  
Currently, the code only allows calculation in the coronal, axial, or sagittal planes. Thus, I need help reorienting my segmentation and volume along the axis of the femoral neck.

For a first try, I first reoriented the axes by applying a linear transform to my segmentation and volume as shown below.  
 ![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/a/5/a5b107167f65b03a273296ca8db846c1bcd6fa51.png)  
I then applied the SlicePlotArea.py code. However, the plot did not take the transform into account and instead plotted the original axis.

I am looking for another method to try. I believe I need to resample my segmentation and volume, but I’m not sure how to do this. When I tried using Resample Scalar/Vector/DWI Volume Module along with my transform, all of my views became black. ![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/1/5/152ac4b760a15996bb66026b443b347dc191da7c.png)

Please provide some help!

Thanks,  
Abby

---

<div class="post-metadata">

**Author:** ![hherhold](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/hherhold/32/12199_2.png) [@hherhold](https://discourse.slicer.org/u/hherhold)\
**Post date:** [September 12, 2019, 12:12am UTC](https://discourse.slicer.org/t/resampling-segmentation-and-volume-about-a-user-defined-axis-for-plotting/8392/2 "2019-09-12T00:12:22Z")

</div>

Did you try hardening the transform before running SliceAreaPlot?

---

<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:** [September 12, 2019, 2:43am UTC](https://discourse.slicer.org/t/resampling-segmentation-and-volume-about-a-user-defined-axis-for-plotting/8392/3 "2019-09-12T02:43:24Z")

</div>

Since a linear transform is applied, hardening would not resample the volume.

A simple solution would be to create an ROI node, rotate it with a transform so that it is aligned with the axis directions that you need and use that in Crop volume module to crop/resample the volume.

You can set up the transform manually, using sliders and/or interactively moving/rotating in a 3D view (in Transforms module, enable Display / Interaction / Visible in 3D view). Or, create the ROI automatically from an open curve node (in recent Slicer Preview version) by dropping a curve point in the middle of the femur head, a second point along the femur, and copy-pasting this code into the Python console:

```python
femurLineNode = slicer.mrmlScene.GetFirstNodeByClass("vtkMRMLMarkupsCurveNode")
femurToWorldMatrix = vtk.vtkMatrix4x4()
femurLineNode.GetCurvePointToWorldTransformAtPointIndex(0,femurToWorldMatrix)
femurToWorldTransform = slicer.mrmlScene.AddNewNodeByClass("vtkMRMLTransformNode")
femurToWorldTransform.SetAndObserveMatrixTransformToParent(femurToWorldMatrix)
roiNode = slicer.mrmlScene.AddNewNodeByClass("vtkMRMLAnnotationROINode")
roiNode.SetXYZ(0, 0, 30)
roiNode.SetRadiusXYZ(40, 40, 60)
roiNode.SetAndObserveTransformNodeID(femurToWorldTransform.GetID())

```

Choose Open Curve on toolbar then click the first curve point:

 ![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/b/0/b0f2b2a4085c72b32de0f249dd4f699d72ec0311.png)

Go to a different slice, place the second contour point then right-click to finish the curve:

 ![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/d/8/d83adebb6297156233e63f28bd5aaa357edb249b.jpeg)

Copy-paste the code snippet above to automatically generate ROI:

 ![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/4/2/42a0b69377455520c4a57fddbd79a015d2816c2b.jpeg)

Use Crop Volume module (you can leave all settings at default) to crop&resample the volume using the oriented ROI:

 ![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/d/c/dc5c49b9cc6e9068aad0959b818351b9cadf1d0e.jpeg)

You can rotate slice views to align with volume axes by clicking pushpin icon at the top-left corner of the slice view controller and clicking “Rotate to volume plane” button.

You can then perform cross section analysis using this cropped volume.

---

<div class="post-metadata">

**Author:** ![abniesen](https://avatars.discourse-cdn.com/v4/letter/a/a9a28c/32.png) [@abniesen](https://discourse.slicer.org/u/abniesen)\
**Post date:** [September 12, 2019, 7:19pm UTC](https://discourse.slicer.org/t/resampling-segmentation-and-volume-about-a-user-defined-axis-for-plotting/8392/4 "2019-09-12T19:19:06Z")

</div>

Thank you very much for the detailed response Andras! I was able to get the SliceAreaPlot code to work along my defined axis using the methods you described. I opted to manually set-up the ROI node and transform.

I greatly appreciate the help!

Best,  
Abby
