# ROI Median Value Volumetric

**URL:** <https://discourse.slicer.org/t/roi-median-value-volumetric/15742>\
**Category:** Support\
**Tags:** segmentation\
**Created:** [January 29, 2021, 10:46pm UTC](https://discourse.slicer.org/t/roi-median-value-volumetric/15742 "2021-01-29T22:46:39Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![SmHoop](https://avatars.discourse-cdn.com/v4/letter/s/f6c823/32.png) [@SmHoop](https://discourse.slicer.org/u/SmHoop)\
**Post date:** [January 29, 2021, 10:46pm UTC](https://discourse.slicer.org/t/roi-median-value-volumetric/15742/1 "2021-01-29T22:46:40Z")

</div>

Hi,

Is there a feature that allows an ROI selection that will inform of basic metrics in the ROI such as median, mean, etc? I would like to select a cylinder through a volume and have the data of every slice available (eg median value in the ROI in every slice) possibly exported as a .csv.

Thank you!

---

<div class="post-metadata">

**Author:** ![pieper](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/pieper/32/8_2.png) [@pieper](https://discourse.slicer.org/u/pieper)\
**Post date:** [January 29, 2021, 11:01pm UTC](https://discourse.slicer.org/t/roi-median-value-volumetric/15742/2 "2021-01-29T23:01:14Z")

</div>

Without coding, you can do this by making a segmentation and then using the SegmentStatistics module.

---

<div class="post-metadata">

**Author:** ![SmHoop](https://avatars.discourse-cdn.com/v4/letter/s/f6c823/32.png) [@SmHoop](https://discourse.slicer.org/u/SmHoop)\
**Post date:** [January 29, 2021, 11:14pm UTC](https://discourse.slicer.org/t/roi-median-value-volumetric/15742/3 "2021-01-29T23:14:03Z")

</div>

Thanks for the reply. So I tried that earlier, but I’m only seeing metrics for the entire volume (this is like a 15 cm long cylinder at this point). Is there any way to show the slice by slice data or is this a slice by slice process if I wanted to do it this way?

---

<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:** [January 29, 2021, 11:31pm UTC](https://discourse.slicer.org/t/roi-median-value-volumetric/15742/4 "2021-01-29T23:31:31Z")

</div>

To compute slice-by-slice metrics, probably the simplest is to blank out areas outside the segment using Mask volume effect (provided by SegmentEditorExtraEffects extension) and then iterate through the slices using numpy (see [example](https://www.slicer.org/wiki/Documentation/Nightly/ScriptRepository#Get_axial_slice_as_numpy_array)).

---

<div class="post-metadata">

**Author:** ![SmHoop](https://avatars.discourse-cdn.com/v4/letter/s/f6c823/32.png) [@SmHoop](https://discourse.slicer.org/u/SmHoop)\
**Post date:** [February 1, 2021, 4:42pm UTC](https://discourse.slicer.org/t/roi-median-value-volumetric/15742/5 "2021-02-01T16:42:29Z")

</div>

This looks very promising! I have not used any python functionality with Slicer before. With the segment created (I made a cylinder with the SegmentEditorExtraEffects and then masked out the outside of the rest so I’m left with a 1cm cylinder of segment), do I start typing in the python code into the Python Interactor Window in the Slicer application or is there a way to save this segment as data somewhere and then use Python in a separate interface? This is my first experience with anything like this, I appreciate all the help!

---

<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 1, 2021, 4:45pm UTC](https://discourse.slicer.org/t/roi-median-value-volumetric/15742/6 "2021-02-01T16:45:21Z")

</div>

You can either use the built-in Python console (copy-paste code between a text editor and the console) or use a [Slicer Jupyter notebook](https://github.com/Slicer/SlicerJupyter). If you need to run the code often with a convenient user interface then you can put it into a scripted module (see [tutorials](https://slicer.readthedocs.io/en/latest/developer_guide/api.html#tutorials)).

---

<div class="post-metadata">

**Author:** ![SmHoop](https://avatars.discourse-cdn.com/v4/letter/s/f6c823/32.png) [@SmHoop](https://discourse.slicer.org/u/SmHoop)\
**Post date:** [February 1, 2021, 6:26pm UTC](https://discourse.slicer.org/t/roi-median-value-volumetric/15742/7 "2021-02-01T18:26:04Z")

</div>

With my masked data, I now have an image of 0s everywhere except a cylinder of values. I’m not sure I’m following the documentation correctly. I am trying to follow the example code below

import SampleData  
volumeNode = SampleData.SampleDataLogic().downloadMRHead()  
sliceIndex = 12

voxels = slicer.util.arrayFromVolume(volumeNode) # Get volume as numpy array  
slice = voxels[sliceIndex:,:] # Get one slice of the volume as numpy array

My segment is called “LF\_H04\_1mm\_Permuted masked” in Slicer. It appears that this is already a volume but maybe I’m missing something?

---

<div class="post-metadata">

**Author:** ![SmHoop](https://avatars.discourse-cdn.com/v4/letter/s/f6c823/32.png) [@SmHoop](https://discourse.slicer.org/u/SmHoop)\
**Post date:** [February 1, 2021, 8:25pm UTC](https://discourse.slicer.org/t/roi-median-value-volumetric/15742/8 "2021-02-01T20:25:32Z")

</div>

edit: I am able to export my transformed image (hardened), and masked as an .nrrd and just read into python/matlab. However it is not saving the rotations and transforms I had used to make the segment/mask even with a harden on the transform.

---

<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 1, 2021, 8:27pm UTC](https://discourse.slicer.org/t/roi-median-value-volumetric/15742/9 "2021-02-01T20:27:43Z")

</div>

Sorry, I don’t understand what you mean. Maybe you can add a few screenshots to illustrate the question.

---

<div class="post-metadata">

**Author:** ![SmHoop](https://avatars.discourse-cdn.com/v4/letter/s/f6c823/32.png) [@SmHoop](https://discourse.slicer.org/u/SmHoop)\
**Post date:** [February 1, 2021, 8:40pm UTC](https://discourse.slicer.org/t/roi-median-value-volumetric/15742/10 "2021-02-01T20:40:54Z")

</div>

Sure sorry about that.

Here is the initial imported NRRD recon (Apparently I accidentally saved the rotation I made permanent to the file? Normally had to rotate this image to make it look this way):

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

Here is the segment tube:

 ![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/f/6/f6b9f7d19cb00feaacccb1d349c01bda836304df.jpeg)

Here is the masked segment (all outside vals = 0):

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

At this point I save the masked segment nrrd and open in Matlab, but as you can see the image is rotated differently than expected:

 ![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/2/3/236c9c183ab3f2bb9203f1f44e8f760ce4d65f4c.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 1, 2021, 8:42pm UTC](https://discourse.slicer.org/t/roi-median-value-volumetric/15742/11 "2021-02-01T20:42:25Z")

</div>

Transform hardening on images is only performed by resampling if it is necessary (e.g., when applying non-linear transform), because resampling the image data is a lossy operation. Linear transforms are applied by changing the image geometry (image origin, spacing, and axis directions), without touching the voxel data, this preserving all information. If you want to resample the image data along axes defined by a transform then you can use one of the image resampling filters.

---

<div class="post-metadata">

**Author:** ![SmHoop](https://avatars.discourse-cdn.com/v4/letter/s/f6c823/32.png) [@SmHoop](https://discourse.slicer.org/u/SmHoop)\
**Post date:** [February 1, 2021, 8:48pm UTC](https://discourse.slicer.org/t/roi-median-value-volumetric/15742/12 "2021-02-01T20:48:58Z")

</div>

So rather than saving the Nrrd right after the masking step, I should resample the data so it is aligned the same way as I see in the windows? I’m not sure I see why the initial image is oriented such yet when saved and reopened in Matlab it is rotated back to it’s “original” position?

---

<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 1, 2021, 8:59pm UTC](https://discourse.slicer.org/t/roi-median-value-volumetric/15742/13 "2021-02-01T20:59:56Z")

</div>

Yes, if you want to have the image axis directions to be aligned with a particular physical axis directions then you need to resample the image.

You can create a volume with the desired axis directions (see example [here](https://www.slicer.org/wiki/Documentation/Nightly/ScriptRepository#Create_a_new_volume)) and use that as reference volume in “Resample scalar/vector/dwi volume” module.

However, it is probably even simpler to start everything with segmenting the jar. You can then get oriented bounding box of the jar fully automatically from Segment Statistics module. Example script to get the bounding box as ROI node is [here](https://discourse.slicer.org/t/new-segment-statistics-oriented-bounding-box-diameter-and-more/10203). If you set this ROI as input to Crop volume module then you get a cropped, resampled volume that has its axes aligned with the axes of the jar.

---

<div class="post-metadata">

**Author:** ![SmHoop](https://avatars.discourse-cdn.com/v4/letter/s/f6c823/32.png) [@SmHoop](https://discourse.slicer.org/u/SmHoop)\
**Post date:** [February 1, 2021, 9:08pm UTC](https://discourse.slicer.org/t/roi-median-value-volumetric/15742/14 "2021-02-01T21:08:09Z")

</div>

Okay I will give this a shot, thank you!

---

<div class="post-metadata">

**Author:** ![SmHoop](https://avatars.discourse-cdn.com/v4/letter/s/f6c823/32.png) [@SmHoop](https://discourse.slicer.org/u/SmHoop)\
**Post date:** [February 2, 2021, 3:49pm UTC](https://discourse.slicer.org/t/roi-median-value-volumetric/15742/15 "2021-02-02T15:49:59Z")

</div>

The second suggestion worked beautifully, thank you!
