# Cylinder fitting

**URL:** <https://discourse.slicer.org/t/cylinder-fitting/22523>\
**Category:** Support\
**Tags:** registration\
**Created:** [March 15, 2022, 5:18pm UTC](https://discourse.slicer.org/t/cylinder-fitting/22523 "2022-03-15T17:18:21Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![alyssan](https://avatars.discourse-cdn.com/v4/letter/a/3da27b/32.png) [@alyssan](https://discourse.slicer.org/u/alyssan)\
**Post date:** [March 15, 2022, 5:18pm UTC](https://discourse.slicer.org/t/cylinder-fitting/22523/1 "2022-03-15T17:18:21Z")

</div>

I have a segmentation that has a cylindical shape but is uneven and has different radii throughout. I would like to calculate the radius at different intervals to find the largest and smallest radius. Then fit a cylinder to that segmentation that has that radius and the correct angle. How could I achieve this? Thanks

---

<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 16, 2022, 8:06pm UTC](https://discourse.slicer.org/t/cylinder-fitting/22523/2 "2022-03-16T20:06:02Z")

</div>

You can use Segment Statistics module to get:

- center of gravity: on point along the cylinder axis
- principal axis: direction of the cylinder axis
- volume: you can use this value along with the length of the cylinder (from oriented bounding box size) to get the average radius of the cylinder

Alternatively, you can use VMTK extension to get all the cylinder axis points and radius values along the axis.

If you know the expected cylinder length and radius then you can also use Model Registration or Segment Registration modules (provided by extensions).

---

<div class="post-metadata">

**Author:** ![alyssan](https://avatars.discourse-cdn.com/v4/letter/a/3da27b/32.png) [@alyssan](https://discourse.slicer.org/u/alyssan)\
**Post date:** [March 18, 2022, 3:59pm UTC](https://discourse.slicer.org/t/cylinder-fitting/22523/3 "2022-03-18T15:59:16Z")

</div>

Great, thank you for your help. I am still struggling with how to find the length can you explain this a bit more?

Also, I am still not clear on how to generate a cylinder that fits my segmentation. Can I do this using the General Registration module?

---

<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 19, 2022, 12:30am UTC](https://discourse.slicer.org/t/cylinder-fitting/22523/4 "2022-03-19T00:30:04Z")

</div>

Length is provided by the orientation d bounding box size.

You can use a model node to display the cylinder. Set the output of a [vtkCylinderSource](https://vtk.org/doc/nightly/html/classvtkCylinderSource.html) as input polydata of the model. See many of examples in the script repository: [Script repository — 3D Slicer documentation](https://slicer.readthedocs.io/en/latest/developer_guide/script_repository.html)

---

<div class="post-metadata">

**Author:** ![alyssan](https://avatars.discourse-cdn.com/v4/letter/a/3da27b/32.png) [@alyssan](https://discourse.slicer.org/u/alyssan)\
**Post date:** [March 21, 2022, 4:29pm UTC](https://discourse.slicer.org/t/cylinder-fitting/22523/5 "2022-03-21T16:29:49Z")

</div>

I am having trouble using the python code to generate the bounding box. Its is giving me an error AttributeError: ‘MRMLCorePython.vtkMRMLLabelMapVolumeDisplayNode’ object has no attribute ‘GetVisibleSegmentIDs’

Is there a way to fix this or generate bounding box manually?

---

<div class="post-metadata">

**Author:** ![alyssan](https://avatars.discourse-cdn.com/v4/letter/a/3da27b/32.png) [@alyssan](https://discourse.slicer.org/u/alyssan)\
**Post date:** [March 21, 2022, 4:30pm UTC](https://discourse.slicer.org/t/cylinder-fitting/22523/6 "2022-03-21T16:30:34Z")

</div>

The error is given for the code, segStatLogic.computeStatistics()

---

<div class="post-metadata">

**Author:** ![alyssan](https://avatars.discourse-cdn.com/v4/letter/a/3da27b/32.png) [@alyssan](https://discourse.slicer.org/u/alyssan)\
**Post date:** [March 21, 2022, 4:36pm UTC](https://discourse.slicer.org/t/cylinder-fitting/22523/7 "2022-03-21T16:36:44Z")

</div>

Under what header in the script repository would an example of generating a cylinder be? I cant locate it

---

<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 21, 2022, 6:12pm UTC](https://discourse.slicer.org/t/cylinder-fitting/22523/8 "2022-03-21T18:12:24Z")

</div>

> [@alyssan](#):
>
> I am having trouble using the python code to generate the bounding box. Its is giving me an error AttributeError: ‘MRMLCorePython.vtkMRMLLabelMapVolumeDisplayNode’ object has no attribute ‘GetVisibleSegmentIDs’

Segment Statistics module requires segmentation input. You can [load a binary volume as segmentation; or load it as labelmap and then convert it to segmentation](https://slicer.readthedocs.io/en/latest/user_guide/modules/segmentations.html#import-an-existing-segmentation-from-volume-file).

> [@alyssan](#):
>
> Under what header in the script repository would an example of generating a cylinder be? I cant locate it

There are a number of relevant examples in the Models section. For example [this one](https://slicer.readthedocs.io/en/latest/developer_guide/script_repository.html#show-a-simple-surface-mesh-as-a-model-node).

---

<div class="post-metadata">

**Author:** ![alyssan](https://avatars.discourse-cdn.com/v4/letter/a/3da27b/32.png) [@alyssan](https://discourse.slicer.org/u/alyssan)\
**Post date:** [March 23, 2022, 3:20pm UTC](https://discourse.slicer.org/t/cylinder-fitting/22523/9 "2022-03-23T15:20:01Z")

</div>

Thank you for your help it is much appreciated.

I tried out the code:  
implant = vtk.vtkCylinderSource()  
implant.SetHeight(100)  
implant.SetRadius(8)  
implant.SetCenter(-78.778,102.725,-181.217)  
implant.SetResolution(100)

implantNode = slicer.modules.models.logic().AddModel(implant.GetOutputPort())  
implantNode.GetDisplayNode().SetColor(0,0,1)  
implantNode.GetDisplayNode().SetOpacity(1)

I used some of the values I obtained from segment statistics. However, I am trying to automate this process. Is there a way that I can use variables to input for the radius and height instead of values?

---

<div class="post-metadata">

**Author:** ![alyssan](https://avatars.discourse-cdn.com/v4/letter/a/3da27b/32.png) [@alyssan](https://discourse.slicer.org/u/alyssan)\
**Post date:** [March 23, 2022, 3:21pm UTC](https://discourse.slicer.org/t/cylinder-fitting/22523/10 "2022-03-23T15:21:06Z")

</div>

I am also having trouble with some of the values. Is their a variable that contains the length? And how do I find the angle of rotation so that the cylinder is in the same direction?

---

<div class="post-metadata">

**Author:** ![alyssan](https://avatars.discourse-cdn.com/v4/letter/a/3da27b/32.png) [@alyssan](https://discourse.slicer.org/u/alyssan)\
**Post date:** [March 23, 2022, 3:25pm UTC](https://discourse.slicer.org/t/cylinder-fitting/22523/11 "2022-03-23T15:25:46Z")

</div>

The segment statistics gives me the ferret diameter which is not the correct diameter of my segmentation.

I tried using VMTK to extract centerline however it only gives me one value for the radius. How do I get multiple values along the length of the segmentation as displayed in the example picture? Thank you!

![pic1](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/6/b/6bde17d61287c4527c6474ed69bdd8010f8b7850.png)

![pic2](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/3/2/329574b453aa14ff988ebc85a58ce88aaa7887ca.png)

---

<div class="post-metadata">

**Author:** ![chir.set](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/chir.set/32/66982_2.png) [@chir.set](https://discourse.slicer.org/u/chir.set)\
**Post date:** [March 23, 2022, 6:00pm UTC](https://discourse.slicer.org/t/cylinder-fitting/22523/12 "2022-03-23T18:00:55Z")

</div>

> [@alyssan](#):
>
> VMTK to extract centerline however it only gives me one value for the radius. How do I get multiple values

‘[Cross-section analysis](https://github.com/vmtk/SlicerExtension-VMTK/blob/master/Docs/CrossSectionAnalysis.md)’ module should fulfil this part of your needs. You can get diameters at each centerline point in a table, and plot them in a graph.

---

<div class="post-metadata">

**Author:** ![alyssan](https://avatars.discourse-cdn.com/v4/letter/a/3da27b/32.png) [@alyssan](https://discourse.slicer.org/u/alyssan)\
**Post date:** [March 23, 2022, 8:19pm UTC](https://discourse.slicer.org/t/cylinder-fitting/22523/13 "2022-03-23T20:19:51Z")

</div>

I am not sure what I am doing wrong but I can not figure out how to get my segmentation(1st pic) to look like the 2nd with all the centerline points along the line. I tried using cross section analysis but cannot get it to work.

---

<div class="post-metadata">

**Author:** ![alyssan](https://avatars.discourse-cdn.com/v4/letter/a/3da27b/32.png) [@alyssan](https://discourse.slicer.org/u/alyssan)\
**Post date:** [March 23, 2022, 8:24pm UTC](https://discourse.slicer.org/t/cylinder-fitting/22523/14 "2022-03-23T20:24:55Z")

</div>

Is their a step i need to do between creating the centerline and using the cross section analysis tool?

---

<div class="post-metadata">

**Author:** ![chir.set](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/chir.set/32/66982_2.png) [@chir.set](https://discourse.slicer.org/u/chir.set)\
**Post date:** [March 23, 2022, 8:52pm UTC](https://discourse.slicer.org/t/cylinder-fitting/22523/15 "2022-03-23T20:52:18Z")

</div>

You are showing a centerline model in the first image. 'Extract centerline allows to generate a centerline curve too (second image). 'Cross-section analysis ’ works with both centerline models and curves. Prefer the curve variant as the centerline model is, though not really so, but nearly legacy.
