# How to export individual slices or calculate a centroid for each slice of a segmentation?

**URL:** <https://discourse.slicer.org/t/how-to-export-individual-slices-or-calculate-a-centroid-for-each-slice-of-a-segmentation/28622>\
**Category:** Development\
**Created:** [March 28, 2023, 12:37pm UTC](https://discourse.slicer.org/t/how-to-export-individual-slices-or-calculate-a-centroid-for-each-slice-of-a-segmentation/28622 "2023-03-28T12:37:08Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Melanie](https://avatars.discourse-cdn.com/v4/letter/m/35a633/32.png) [@Melanie](https://discourse.slicer.org/u/Melanie)\
**Post date:** [March 28, 2023, 12:37pm UTC](https://discourse.slicer.org/t/how-to-export-individual-slices-or-calculate-a-centroid-for-each-slice-of-a-segmentation/28622/1 "2023-03-28T12:37:08Z")

</div>

I have some medical images, saved as .nrrd files. (segmented)

I would like to calculate the centroid for each slice along a single axis. (Entire segmnet is currently aligned)

Then create a line of best fit for this.

Any suggestions for this please,(I am very much a beginner and not of engineering background.)

Alternatively if I can export single slices as .png I think there is some function in matlab to do that. I am not sure how to export png files each slice. I use Mac with Mac OS 11.

Slicer 4.11

Thanks heaps

---

<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:** [March 28, 2023, 2:00pm UTC](https://discourse.slicer.org/t/how-to-export-individual-slices-or-calculate-a-centroid-for-each-slice-of-a-segmentation/28622/2 "2023-03-28T14:00:59Z")

</div>

You can get the segmentation as a 3D voxel array and access each slice using indexing. But most likely you will not need to deal with such low-level operations.

What is your overall goal? Would you like to extract the centerline of a segmented structure? Have you tried the [VMTK extension for this](https://github.com/vmtk/SlicerExtension-VMTK#the-vmtk-extension-for-3d-slicer) or [SegmentGeometry](https://github.com/jmhuie/Slicer-SegmentGeometry#segmentgeometry) extensions?

---

<div class="post-metadata">

**Author:** ![Melanie](https://avatars.discourse-cdn.com/v4/letter/m/35a633/32.png) [@Melanie](https://discourse.slicer.org/u/Melanie)\
**Post date:** [March 29, 2023, 7:01am UTC](https://discourse.slicer.org/t/how-to-export-individual-slices-or-calculate-a-centroid-for-each-slice-of-a-segmentation/28622/3 "2023-03-29T07:01:28Z")

</div>

Thanks @lassoan, I think segment geometry extension has it,

Yes, I would lilke to extract the centreline of the structure,  
Based on centroid of every slice of the segmentation.

So I selcetd computations as centroid (screenshot), I think Cx and Cy is the x and y coordinates of the centroid for each slice. Is that correct?

But I place a fiducialmarker the possible centroid, on a particlar slice, I get different coordinates for the marker,  
I think the module does what I want but I am not interpreting it correctly. Could you pls advice.  
Also how can I draw a best fit line for the series of centroids pls

Thanks a lot

 ![Screen Shot 2023-03-29 at 5.31.11 pm](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/e/0/e0adefadc457c9c6e565a9cf0a75e574747c6187.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:** [March 30, 2023, 3:33pm UTC](https://discourse.slicer.org/t/how-to-export-individual-slices-or-calculate-a-centroid-for-each-slice-of-a-segmentation/28622/4 "2023-03-30T15:33:34Z")

</div>

> [@Melanie](#):
>
> So I selcetd computations as centroid (screenshot), I think Cx and Cy is the x and y coordinates of the centroid for each slice. Is that correct?

See [documentation](https://github.com/jmhuie/Slicer-SegmentGeometry#output-details):

> - Cx: Centroid x-coordinates that correspond to the resampled and cropped volume exported by SegmentGeometry. Presented in IJK format.
> - Cy: Centroid y-coordinates that correspond to the resampled and cropped volume exported by SegmentGeometry. Presented in IJK format.

IJK refers to voxels coordinates. You can compute world (RAS) coordinates from voxel coordinates of a volume as shown in [this example](https://slicer.readthedocs.io/en/latest/developer_guide/script_repository.html#get-markup-control-point-ras-coordinates-from-volume-voxel-coordinates).

> [@Melanie](#):
>
> Also how can I draw a best fit line for the series of centroids pls

You can compute best fit line using SVD (see example in [SlicerHeart](https://github.com/SlicerHeart/SlicerHeart/blob/bb7a14a48f52dad67142ff14339e23761e1829aa/ValveAnnulusAnalysis/HeartValveLib/ValveModel.py#L1399-L1435)). You can draw the computed line using a line markup.

---

<div class="post-metadata">

**Author:** ![Melanie](https://avatars.discourse-cdn.com/v4/letter/m/35a633/32.png) [@Melanie](https://discourse.slicer.org/u/Melanie)\
**Post date:** [April 1, 2023, 3:48am UTC](https://discourse.slicer.org/t/how-to-export-individual-slices-or-calculate-a-centroid-for-each-slice-of-a-segmentation/28622/5 "2023-04-01T03:48:15Z")

</div>

Thanks very much @lassoan

Works well
