# How to determine the value of an angle starting from an image? [duplicate]

I have two lines drawn with Mathematica, how do I calculate the angle between these two lines to have the numeric value of that angle? Thank you

• I suggest you start by taking a look at ImageLines[] – dr.blochwave Oct 29 '15 at 7:56
• I have only two lines that create an angle and I would extrapolate the angle in degrees, not the matrix. Just a number – Luca Samuele Capizzi Oct 29 '15 at 15:10
• No I have only a image. No coordinates?? Two lines like a "V" and i would like to know the angle degrees value. 45°? 70°? 90°? 132°? Can you help me? – Luca Samuele Capizzi Oct 29 '15 at 15:23
• I recommend a protractor. – bill s Oct 29 '15 at 15:47

I take it, you have drawn these lines yourself, did you? If so, you know their coordinates:

Line[{{x1,y1},{x2,y2}}]


and

  Line[{{X1,Y1},{X2,Y2}}]


Then using a standard expression of the vector analysis one finds the angle

v1 = {x2 - x1, y2 - y1}; v2 = {X2 - X1, Y2 - Y1}; ArcCos[v1.v2/(Sqrt[v1.v1]*Sqrt[v2.v2])] For example, if

 x1 = 0;
x2 = 0;
y1 = 1;
y2 = 2;
X1 = 0;
X2 = 1;
Y1 = 0;
Y2 = 1;


one finds

ArcCos[v1.v2/(Sqrt[v1.v1]*Sqrt[v2.v2])]

(* \[Pi]/4  *)


Have fun!

Later edit: to address your comment. In that case I have a function"metrics" to do such things. Please find below the description along with the code

Description of the function metrics[...]

The function metrics[...] is designed to measure coordinates of points of any image "image", distances between any two image points, radii of roundings and angles.

The argument "image" must have the head "Image", since it uses few functions specific for images. If the desired object has the head "Graphics" it should be wrapped by the function "Image".

Operating the function metrics[...]

By default all measurement tools are switched off. They may be independently switched on by checking the corresponding checkboxes.

The desired points are selected by locators. The latter have several appearances that are different for different tools.

The gauges on top of the screen control the sizes of the locators and the thicknesses of all lines.

The "Show right angle" checkbox switches on the right angle tool, which is the orientation, rather than the measurement tool.

The measured values can be read in the panels below the image.

Both the coordinates, the distance and radius are measured in pixels and then rescaled and returned in millimeters. The angle is returned both in degrees and in radians.

The current value of the coordinates, distance, radius and angle can be memorized in the global variables: copyCoordinates, copyDistance, copyRadius and copyAngle by pressing the corresponding buttons below the image. By evaluating the corresponding variables one gets the value in question in the notebook.

 metrics[image_] :=
Manipulate[
DynamicModule[{distance = 0, coordinates = {0, 0}, rad = 0,
angle1 = 0, angle2 = 0, x1 = ImageDimensions[image][]/3,
y1 = ImageDimensions[image][]/2.,
x2 = 2*ImageDimensions[image][]/3.,
y2 = 2 ImageDimensions[image][]/3,
x3 = 3 ImageDimensions[image][]/4.,
y3 = 3 ImageDimensions[image][]/4,
x4 = 0.5*ImageDimensions[image][],
y4 = 0.5*ImageDimensions[image][],
x5 =
0.4*ImageDimensions[image][] +
0.05*ImageDimensions[image][],
y5 = 0.4*ImageDimensions[image][] +
0.05*ImageDimensions[image][],
x6 = 0.8*ImageDimensions[image][],
y6 = 0.3*ImageDimensions[image][],
x7 = 0.3*ImageDimensions[image][],
y7 = 0.7*ImageDimensions[image][],
x8 = 0.4*ImageDimensions[image][],
y8 = 0.4*ImageDimensions[image][],
x9 = 0.5*ImageDimensions[image][],
y9 = 0.4*ImageDimensions[image][],
x10 = 0.4*ImageDimensions[image][],
y10 = 0.5*ImageDimensions[image][],

loc1 =
Graphics[{Blue, Opacity[If[showLine, 1, 0]],
AbsoluteThickness[thickness], Circle[{0, 0}, 1]},
loc2 =
Graphics[{Opacity[If[showTarget, 1, 0]], Thick,
Arrow[{{-0.02, 0}, {-0.002, 0}}]}, {Darker@Red,
Arrow[{{0.02, 0}, {0.002, 0}}]}, {Darker@Red,
Arrow[{{0, -0.02}, {0, -0.002}}]}}, ImageSize -> {30, 30}],
loc3 =
Graphics[{Darker[Green], Opacity[If[showCircle, 1, 0]],
AbsoluteThickness[thickness], Circle[{0, 0}, 1]},
loc4 =
Graphics[{Purple, Opacity[If[showAngle, 1, 0]],
AbsoluteThickness[thickness], Circle[{0, 0}, 1]},
locCross3 =
Graphics[{Darker@Green, Opacity[If[showCircle, 1, 0]],
AbsoluteThickness[thickness], {Line[{{-0.01, 0.}, {0.01, 0}}],
Line[{{0, -0.01}, {0, 0.01}}]}}, ImageSize -> radius1],
locCross4 =
Graphics[{Purple, Opacity[If[showAngle, 1, 0]],
AbsoluteThickness[thickness], {Line[{{-0.01, 0.}, {0.01, 0}}],
Line[{{0, -0.01}, {0, 0.01}}]}}, ImageSize -> radius1],
loc5 =
Graphics[{Darker@Cyan, Opacity[If[showRightAngle, 1, 0]],
AbsoluteThickness[thickness], Circle[{0, 0}, 1]},
},

distance =
Dynamic[Round[Sqrt[(x1 - x2)^2 + (y1 - y2)^2]*25.4/72, 0.01]];
coordinates = Dynamic[Round[{x3, y3}*25.4/72, 0.01]];
Dynamic[Round[Sqrt[(x4 - x5)^2 + (y4 - y5)^2]*25.4/72, 0.01]];
angle1 =
Dynamic[Round[
ArcCos[((x6 - x7)^2 + (y6 - y7)^2 + (x7 - x8)^2 + (y7 -
y8)^2 - (x8 - x6)^2 - (y8 - y6)^2)/(2*
Sqrt[(x6 - x7)^2 + (y6 - y7)^2]*
Sqrt[(x7 - x8)^2 + (y7 - y8)^2])] // Chop, 0.01]];
angle2 =
Dynamic[Round[
360./(2 \[Pi])*
ArcCos[((x6 - x7)^2 + (y6 - y7)^2 + (x7 - x8)^2 + (y7 -
y8)^2 - (x8 - x6)^2 - (y8 - y6)^2)/(2*
Sqrt[(x6 - x7)^2 + (y6 - y7)^2]*
Sqrt[(x7 - x8)^2 + (y7 - y8)^2])] // Chop, 0.01]];

Column[{
LocatorPane[
Dynamic[{{x1, y1}, {x2, y2}, {x3, y3}, {x4, y4}, {x5, y5}, {x6,
y6}, {x7, y7}, {x8, y8}, {x9, y9}, {x10, y10}}],
Dynamic@Show[{image,

Graphics[{RGBColor[0, 0, 0.7], Dashed,
Opacity[If[showLine, 1, 0]], AbsoluteThickness[thickness],
Dynamic[Line[{{x1, y1}, {x2, y2}}]]
}],

Graphics[{Darker[Green], Opacity[If[showCircle, 1, 0]],
AbsoluteThickness[thickness],
Circle[{x4, y4}, Sqrt[(x4 - x5)^2 + (y4 - y5)^2]]}],

Graphics[{Purple, Opacity[If[showAngle, 1, 0]],
AbsoluteThickness[thickness],
Dynamic[Line[{{x6, y6}, {x7, y7}, {x8, y8}}]]}],

Graphics[{Darker@Cyan, Opacity[If[showRightAngle, 1, 0]],
AbsoluteThickness[thickness],
Dynamic[Line[{{x9, y9}, {x10, y10}}]],
Dynamic[
Rotate[Line[{{x9, y9}, {x10, y10}}],
90 Degree, {x10, y10}]]
}]

},
ImageSize -> ImageDimensions[image]],
Appearance -> {loc1, loc1, loc2, locCross3, loc3, loc4,
locCross4, loc4, loc5, loc5}
],
Row[{Panel[
If[showLine,
Row[{Style["Distance:", 10, Blue], Spacer,
Style[distance, 10, Blue], Style[" mm", 10, Blue]}],
Style["Distance:  --", 10, Blue]], ImageSize -> {160, 50},
Alignment -> Center],
Panel[
If[showTarget,
Row[{Style["Coordinate: ", 10, Darker@Red],
Style[coordinates, 10, Darker@Red],
Style[" mm", 10, Darker@Red]}],
Style["Coordinate: --", 10, Darker@Red]],
ImageSize -> {160, 50}, Alignment -> Center]},
Alignment -> Center],
Row[{Panel[
If[showCircle,
Style[" mm", 10, Darker@Green]}],
Style["--", 10, Darker[Green]]]}], ImageSize -> {160, 50},
Alignment -> Center],
Panel[
Row[{Style["Angle: ", 10, Purple],
If[showAngle,
Row[{Style[angle1, 10, Purple],
Style[" rad=", 10, Purple], Style[angle2, 10, Purple],
Style["\[Degree]", 10, Purple]}],
Style["--", 10, Purple]]}], ImageSize -> {160, 50},
Alignment -> Center]
}, Alignment -> Center],
Row[{Button[Style["Copy distance", 10, Blue],
Clear[copyDistance]; copyDistance = N[distance]],
Button[Style["Copy coordinate", 10, Darker@Red],
Clear[copyCoordinates]; copyCoordinates = N[coordinates]],
"To get value the second argument should be True"]],
Button[Style["Copy angle", 10, Purple], Clear[copyAngle];
copyAngle =
If[showAngle, N[angle1],
"To get value the third argument should be True"]]
}, Alignment -> Center]

}, Alignment -> Center]
],

Column[{
Row[{Control[{{showLine, False,
Style["Distance measurement tool", 12]}, {True, False}}],
Spacer,
Control[{{showTarget, False,
Style["Coordinate measurement tool", 12]}, {True, False}}]}],

Row[{Control[{{showAngle, False,
Row[{Style["Angle measurement tool", 12],
Spacer}]}, {True, False}}],
Spacer,
Control[{{showCircle, False,
Spacer}]}, {True, False}}]}, Alignment -> Left],
Control[{{showRightAngle, False,
Row[{Style["Show right angle", 12], Spacer}]}, {True,
False}}],

Row[{Style["Locator size", 12], Spacer}]}, 3, 50,
HorizontalGauge[##, ImageSize -> {200, 40}] &}],

Control[{{thickness, 0.1, Style["Lines thickness", 12]}, 0.1, 5,
HorizontalGauge[##, ImageSize -> {200, 40}] &}]
}, Alignment -> Left],

ControlType -> {Checkbox, Checkbox, Checkbox, Checkbox, Checkbox,
HorizontalGauge, HorizontalGauge, HorizontalGauge},
ControlPlacement -> {Top, Top, Top, Top, Top, Top, Top, Top},
SaveDefinitions -> True];


That is how it should look like. I applied it to an arbitrary experimantal image: Have fun still!

• There is also VectorAngle – Kuba Oct 29 '15 at 9:29
• @Kuba Thank you, I did not know. Why do not you give it as an answer. It is better than mine. – Alexei Boulbitch Oct 29 '15 at 12:07
• Feel free to include it, the main thing is still to notice that OP said "drawn" in MMA so he is probably aware of coordinates in some form. – Kuba Oct 29 '15 at 12:08
• No I have only a image. Two lines like a "V" and i would like to know the angle degrees value. 45°? 70°? 90°? 132°? Can you help me? – Luca Samuele Capizzi Oct 29 '15 at 15:21
• @Luca Samuele Capizzi See the edit – Alexei Boulbitch Oct 29 '15 at 15:42