# Calculate angle between two segments

**URL:** <https://discourse.slicer.org/t/calculate-angle-between-two-segments/12412>\
**Category:** Support\
**Created:** [July 7, 2020, 9:39am UTC](https://discourse.slicer.org/t/calculate-angle-between-two-segments/12412 "2020-07-07T09:39:11Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![AurelienCD](https://avatars.discourse-cdn.com/v4/letter/a/c68b51/32.png) [@AurelienCD](https://discourse.slicer.org/u/AurelienCD)\
**Post date:** [July 7, 2020, 9:39am UTC](https://discourse.slicer.org/t/calculate-angle-between-two-segments/12412/1 "2020-07-07T09:39:12Z")

</div>

Dear 3dslicer community.

I try to write script which would calculate the angle between two segments.

Here is the image I have  
 ![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/c/4/c4771e2a6984c4137261da3f001da5c9a079ebdc.jpeg)

I obtained the segments using simple threshold

Then I would need to draw (automatically by python script as it is a part of an extension) lines through the segments and calculate the angle between the two lines.

As part of the extension I already have the center coordinates of the segments.

do you have any ideas ?

if it is needed, the whole script which give me the image is here:  
[https://github.com/AurelienCD/Radiotherapy\_Quality\_Control/blob/master/Twist\_MLC\_Alignment\_TOMOTHERAPY\_QC.py](https://github.com/AurelienCD/Radiotherapy_Quality_Control/blob/master/Twist_MLC_Alignment_TOMOTHERAPY_QC.py)

Many thanks,

AurelienCD

---

<div class="post-metadata">

**Author:** ![Mik](https://avatars.discourse-cdn.com/v4/letter/m/958977/32.png) [@Mik](https://discourse.slicer.org/u/Mik)\
**Post date:** [July 7, 2020, 12:05pm UTC](https://discourse.slicer.org/t/calculate-angle-between-two-segments/12412/2 "2020-07-07T12:05:32Z")

</div>

Greetings,

Get two points (e.g. along axis of symmetry) from each segment. Two points gives you a vector for each segment. Calculate cross or dot product of that vectors, that gives you either sine or cosine value of the angle.

The problem is how to calculate two points along axis of symmetry, or something like that? If the task is not on the plane, then you need three points.

---

<div class="post-metadata">

**Author:** ![vertebra](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/vertebra/32/7475_2.png) [@vertebra](https://discourse.slicer.org/u/vertebra)\
**Post date:** [July 7, 2020, 2:24pm UTC](https://discourse.slicer.org/t/calculate-angle-between-two-segments/12412/3 "2020-07-07T14:24:26Z")

</div>

@AurelienCD

Hello! I would use the script repository: [https://www.slicer.org/wiki/Documentation/Nightly/ScriptRepository#Measure\_angle\_between\_two\_markup\_lines](https://www.slicer.org/wiki/Documentation/Nightly/ScriptRepository#Measure_angle_between_two_markup_lines)

---

<div class="post-metadata">

**Author:** ![James\_Hoctor](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/james_hoctor/32/6895_2.png) [@James\_Hoctor](https://discourse.slicer.org/u/James_Hoctor)\
**Post date:** [July 7, 2020, 3:42pm UTC](https://discourse.slicer.org/t/calculate-angle-between-two-segments/12412/4 "2020-07-07T15:42:27Z")

</div>

@AurelienCD

I would consider using Principal Component Analysis, specifically the [sklearn implementation](https://scikit-learn.org/stable/modules/generated/sklearn.decomposition.PCA.html).

1. Form an n-by-2 matrix of points in image space, either taking the positions of all points in one segment, or all points on the boundary of a segment (experiment to find which works better for you). Call this matrix `X`.
2. Use `sklearn.decomposition.PCA().fit(X).components_` to get the first principal component. This should be a 2-vector that points along the length of the segment.
3. Measure the angles between these orientation vectors.

---

<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 7, 2020, 4:27pm UTC](https://discourse.slicer.org/t/calculate-angle-between-two-segments/12412/5 "2020-07-07T16:27:57Z")

</div>

Segment Statistics module solves this very nicely: it can compute oriented bounding box and principal axes directions for each segment, directly from the segmentation.

> [@New Segment Statistics: Oriented Bounding Box, Diameter and More](https://discourse.slicer.org/t/new-segment-statistics-oriented-bounding-box-diameter-and-more/10203):
>
> New statistics have been added to the Segment Statistics module: Centroid Oriented bounding box Feret diameter Perimeter Roundness Flatness Elongation Principal axes/moments These statistics are not enabled by default. To enable them, click on the “Options” button beside the “Labelmap Statistics” checkbox, and select the statistics that you would like calculated from the popup window. You can read the documentation for the Segment Statistics module [here](https://slicer.readthedocs.io/en/latest/user_guide/module_segmentstatistics.html). Oriented bounding box:

---

<div class="post-metadata">

**Author:** ![AurelienCD](https://avatars.discourse-cdn.com/v4/letter/a/c68b51/32.png) [@AurelienCD](https://discourse.slicer.org/u/AurelienCD)\
**Post date:** [July 8, 2020, 6:44am UTC](https://discourse.slicer.org/t/calculate-angle-between-two-segments/12412/6 "2020-07-08T06:44:37Z")

</div>

Thank you James for your correction of my code on github it help a lot. I am a beginner and comments are still necessary 🙂

---

<div class="post-metadata">

**Author:** ![AurelienCD](https://avatars.discourse-cdn.com/v4/letter/a/c68b51/32.png) [@AurelienCD](https://discourse.slicer.org/u/AurelienCD)\
**Post date:** [July 8, 2020, 6:50am UTC](https://discourse.slicer.org/t/calculate-angle-between-two-segments/12412/7 "2020-07-08T06:50:48Z")

</div>

Thank you Andras, I will use that new module.

thank you everyone for your help.

all the best,

Aurélien
