 # Calculate angle between two segments

Dear 3dslicer community.

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

Here is the image I have 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:

Many thanks,

AurelienCD

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.

@AurelienCD

Hello! I would use the script repository: https://www.slicer.org/wiki/Documentation/Nightly/ScriptRepository#Measure_angle_between_two_markup_lines

1 Like

@AurelienCD

I would consider using Principal Component Analysis, specifically the sklearn implementation.

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.

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

1 Like

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

Thank you Andras, I will use that new module.

thank you everyone for your help.

all the best,

Aurélien

1 Like