# Howto get a centroid of a section in a slice?

**URL:** <https://discourse.slicer.org/t/howto-get-a-centroid-of-a-section-in-a-slice/24339>\
**Category:** Development\
**Tags:** python\
**Created:** [July 16, 2022, 8:59am UTC](https://discourse.slicer.org/t/howto-get-a-centroid-of-a-section-in-a-slice/24339 "2022-07-16T08:59:48Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![jumbojing](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/jumbojing/32/10811_2.png) [@jumbojing](https://discourse.slicer.org/u/jumbojing)\
**Post date:** [July 16, 2022, 8:59am UTC](https://discourse.slicer.org/t/howto-get-a-centroid-of-a-section-in-a-slice/24339/1 "2022-07-16T08:59:48Z")

</div>

I want to get each centroid of the pedicle sections in green slice(as shown below) by code.

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

---

<div class="post-metadata">

**Author:** ![jumbojing](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/jumbojing/32/10811_2.png) [@jumbojing](https://discourse.slicer.org/u/jumbojing)\
**Post date:** [July 17, 2022, 4:11am UTC](https://discourse.slicer.org/t/howto-get-a-centroid-of-a-section-in-a-slice/24339/2 "2022-07-17T04:11:35Z")

</div>

> [Place fiducial at centroid of current slice - Support - 3D Slicer Community](https://discourse.slicer.org/t/place-fiducial-at-centroid-of-current-slice/22169)  
> [Get Centroid from Script](https://discourse.slicer.org/t/get-centroid-from-script/13191)  
> [Centroid determination](https://discourse.slicer.org/t/centroid-determination/3541)  
> @lassoan@Juicy@jamesobutler @jcfr @pieper@ jmhuie @manjula @szhang

---

<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:** [July 17, 2022, 6:36am UTC](https://discourse.slicer.org/t/howto-get-a-centroid-of-a-section-in-a-slice/24339/3 "2022-07-17T06:36:08Z")

</div>

You can get the current slice image from the slice logic of the slice widget at shown in [this example](https://slicer.readthedocs.io/en/latest/developer_guide/script_repository.html#extract-randomly-oriented-slabs-of-given-shape-from-a-volume).

However, probably you can get the ideal pedicle screw axis position and orientation directly in 3D by segmenting a pedicle. It can be as simple as Paint a sphere with “Editable intensity range” set to bone. You could further improve result by applying Island effect → Extract largest island. To fill the internal holes in the segment you can use Wrap Solidify effect. You can get the axis position and direction of this bone segment using Segment Statistics module. You can get the centerline curve of the pedicle using VMTK extension’s Extract centerline module.

---

<div class="post-metadata">

**Author:** ![jumbojing](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/jumbojing/32/10811_2.png) [@jumbojing](https://discourse.slicer.org/u/jumbojing)\
**Post date:** [July 17, 2022, 7:34am UTC](https://discourse.slicer.org/t/howto-get-a-centroid-of-a-section-in-a-slice/24339/4 "2022-07-17T07:34:49Z")

</div>

> [@lassoan](#):
>
> You can get the current slice image from the slice logic of the slice widget at shown in [this example](https://slicer.readthedocs.io/en/latest/developer_guide/script_repository.html#extract-randomly-oriented-slabs-of-given-shape-from-a-volume).

我没有看懂这个例子

> I didn’t understand [this example](https://slicer.readthedocs.io/en/latest/developer_guide/script_repository.html#extract-randomly-oriented-slabs-of-given-shape-from-a-volume). 😰

> [@lassoan](#):
>
> However, probably you can get the ideal pedicle screw axis position and orientation directly in 3D by segmenting a pedicle. It can be as simple as Paint a sphere with “Editable intensity range” set to bone. You could further improve result by applying Island effect → Extract largest island. To fill the internal holes in the segment you can use Wrap Solidify effect.

由于一些骨质疏松等的特殊病例，segmenting椎弓根with script总是不理想。

> Due to some special cases such as osteoporosis, segmenting a pedicle with script is not always ideal.

> [@lassoan](#):
>
> You can get the axis position and direction of this bone segment using Segment Statistics module. You can get the centerline curve of the pedicle using VMTK extension’s Extract centerline module.

我一直在探索影像学意义上的理想的椎弓根的轴，利用Slicer以及像VMTK这样的插件。。。可是因为上面提到的原因，没成功 😰 👿

> I’ve been exploring the ideal pedicle axis in the imaging sense, using Slicer and plugins like VMTK. . . However, for the reasons mentioned above, it was unsuccessful.

---

<div class="post-metadata">

**Author:** ![jumbojing](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/jumbojing/32/10811_2.png) [@jumbojing](https://discourse.slicer.org/u/jumbojing)\
**Post date:** [July 18, 2022, 1:54pm UTC](https://discourse.slicer.org/t/howto-get-a-centroid-of-a-section-in-a-slice/24339/5 "2022-07-18T13:54:24Z")

</div>

> [@Centroid determination](https://discourse.slicer.org/t/centroid-determination/3541/7):
>
> ```auto
> import numpy as np
> print(np.average(array('ModelX'), axis=0))
> 
> ```

老师您的意思👆🏻是说，多边形各顶点坐标的`np.average()'就是这个多边形的质心吗？

> Teacher, do you mean 👆🏻 that the `np.average()’ of the coordinates of each vertex of the polygon is the centroid of the polygon?

---

<div class="post-metadata">

**Author:** ![jumbojing](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/jumbojing/32/10811_2.png) [@jumbojing](https://discourse.slicer.org/u/jumbojing)\
**Post date:** [July 18, 2022, 2:02pm UTC](https://discourse.slicer.org/t/howto-get-a-centroid-of-a-section-in-a-slice/24339/6 "2022-07-18T14:02:28Z")

</div>

well,现在的问题就转化成了如何提取当前界面某个多边形的顶点坐标。。。

> Well, the current question now becomes how to extract the vertex coordinates of a polygon of the current section. . .

---

<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:** [July 19, 2022, 7:23am UTC](https://discourse.slicer.org/t/howto-get-a-centroid-of-a-section-in-a-slice/24339/7 "2022-07-19T07:23:48Z")

</div>

> [@jumbojing](#):
>
> Due to some special cases such as osteoporosis, segmenting a pedicle with script is not always ideal.

Yes, I know that segmentation of the pedicle is often not trivial, but there are many Segment Editor tools that can help with this. You could also train a neural network using MONAILabel extension to segment the pedicles fully automatically.

---

<div class="post-metadata">

**Author:** ![jumbojing](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/jumbojing/32/10811_2.png) [@jumbojing](https://discourse.slicer.org/u/jumbojing)\
**Post date:** [July 20, 2022, 5:14am UTC](https://discourse.slicer.org/t/howto-get-a-centroid-of-a-section-in-a-slice/24339/8 "2022-07-20T05:14:02Z")

</div>

> [@How to extract the vertex coordinates of a polygon of a model in the current section?](https://discourse.slicer.org/t/how-to-extract-the-vertex-coordinates-of-a-polygon-of-a-model-in-the-current-section/24380/3):
>
> Lassoan教授，好吧。。。我想出来个笨拙的解决方案：
> 
> > Prof.Lassoan, okay. . . I came up with a clumsy solution:
> > 
> > ![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/4/f/4f4806076a2457dc66b6bbecbdf3b6bc7445f458.jpeg)
> 
> 不过这个解决方案有个问题：就是提取的点和点之间的距离不均匀。。。谁有好办法呢？
> 
> > However, there is a problem with this solution: the distance between the extracted points is not uniform. . . Who has a better solution?
> 
> Such As Weighted when calculating the average?

> I want to weight the centroid obtained above by the reciprocal of voxel (CT value), how to do it?

---

<div class="post-metadata">

**Author:** ![jumbojing](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/jumbojing/32/10811_2.png) [@jumbojing](https://discourse.slicer.org/u/jumbojing)\
**Post date:** [July 24, 2022, 12:23am UTC](https://discourse.slicer.org/t/howto-get-a-centroid-of-a-section-in-a-slice/24339/9 "2022-07-24T00:23:30Z")

</div>

👆🏻这个不完美

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

完美截出轮廓，找到质心。。。

from[helperFunctions.py](https://github.com/SlicerIGT/SlicerBoneReconstructionPlanner/blob/699c9970f163a14c7289dd8c8a7aa8b7346969d3/BoneReconstructionPlanner/BRPLib/helperFunctions.py)
