# Extracting angles from a simple picture

I'm not sure if this is even possible with Mathematica, but I want to extract the angles between all lines in this picture: It consists of $12$ lines, so I want a matrix with $144$ entries. I really have no idea even how to begin. Any ideas?

• Thank you! Now I've got the endpoints of the 12 lines..that's nice so far. But how to extract the angles? Feb 2, 2014 at 11:55
• Is it not a rule at this site to ask the author to report some his own efforts in solving the problem, at least a minimal. Further, I suspect this question to be an off-topic, since the problem lies just in analytical geometry, that gives one such an information naturally by the way of a scalar products of the corresponding vectors, and as soon as this has been understood the M code becomes trivial, is it not? Feb 3, 2014 at 8:10

I'm not experienced in image processing so don't look how I will get rid of this alpha channel or whatoever it is in your image :P

pic = Import["http://static.gulli.com/media/2009/tenenbaum/necker_wuerfel.png"];
pic2 = ImageApply[#[] &, pic]

lines = ImageLines[pic2, .2, .05, "Segmented" -> True]; (*manually adjusted*)
lines // Length

12

vector = #2 - #1 & @@@ lines[[All, 1]];
angle = Outer[VectorAngle, vector, vector, 1]; (*Tuples earlier,
thanks to @SimonWoods*)

MatrixForm[Round[angle/Pi, .25]] (*parts of Pi*) • oh wow..I have no idea what you just did, but it works. I'm gonna take my time to understand the code now..thank you very much :) Feb 2, 2014 at 12:08
• Instead of Tuples and Partition you could use angle = Outer[VectorAngle, vector, vector, 1] Feb 2, 2014 at 12:13
• @holistic ok, if you get stuck just say and I will exaplain. p.s. it is good to hold on with an accept a day or two, better answers may appear and now others may be discouraged.
– Kuba
Feb 2, 2014 at 12:13
• @SimonWoods I was thinking about Outer but I forgot that it will skip the partition art for us. That's why I letf Tuples. Well, thanks, much better now. ;)
– Kuba
Feb 2, 2014 at 12:16
• Ok..I'll wait with the accept :). One can learn a lot by seeing different approaches to a problem. Feb 2, 2014 at 12:23

For illustrative reasons I'll show here a way to do it in raw form, starting from the Hough Transform (because it's rarely used in this site):

i = Import["https://i.stack.imgur.com/OiTnw.png"]; idr = ImageDimensions@r;
cc = Select[ComponentMeasurements[MorphologicalComponents[MaxDetect[r, .1]], "Centroid"]
[[All, 2]], #[] < 9/10 First@idr &];
angles = cc[[All, 1]] Pi/First@idr
m = SparseArray[{i_, j_} :> Abs[angles[[i]] - angles[[j]]], Length@angles {1, 1}] //
MatrixForm Edit

Following with the illustrative example, here is a way to get the lines equations:

cc2 = {#[] Pi/First@idr,  Rescale[#[], Last@idr {0, 1},
First@ImageDimensions@i {-1, 1}]} & /@ cc
s1 = FullSimplify[ Solve[#, y] & /@ ((#[] == (x Cos[#[] ] + y Sin[#[] ])) & /@
cc2)]
Plot[y /. s1, {x, -400, 400}, AspectRatio -> 1,
PlotRange -> {First@ImageDimensions@i {-1, 1},
Last@ImageDimensions@i {-1, 1}}]

y->267.552 +0.00199799 x
y->532.705 -1.99192 x
y->21609.1 -97.1997 x
y->13081.1 -97.1997 x
y->177.013 -1.98419 x
y->89.2196 +0.00262237 x
y->-177.013-1.98419 x
y->-89.2196+0.00262237 x
y->-13081.1-97.1997 x
y->-21609.1-97.1997 x
y->-532.705-1.99192 x
y->-267.552+0.00199799 x The coordinates are swapped as usual in an Image/Graphics mapping.