# 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? – holistic Feb 2 '14 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? – Alexei Boulbitch Feb 3 '14 at 8:10

## 2 Answers

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[#[[4]] &, 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[angle] (*radians*)

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 :) – holistic Feb 2 '14 at 12:08
Instead of Tuples and Partition you could use angle = Outer[VectorAngle, vector, vector, 1] – Simon Woods Feb 2 '14 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 '14 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 '14 at 12:16
Ok..I'll wait with the accept :). One can learn a lot by seeing different approaches to a problem. – holistic Feb 2 '14 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["http://i.stack.imgur.com/OiTnw.png"];
r = Radon[ Binarize@ColorNegate@i, Method -> "Radon"]


idr = ImageDimensions@r;
cc = Select[ComponentMeasurements[MorphologicalComponents[MaxDetect[r, .1]], "Centroid"]
[[All, 2]], #[[1]] < 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 = {#[[1]] Pi/First@idr,  Rescale[#[[2]], Last@idr {0, 1},
First@ImageDimensions@i {-1, 1}]} & /@ cc
s1 = FullSimplify[ Solve[#, y] & /@ ((#[[2]] == (x Cos[#[[1]] ] + y Sin[#[[1]] ])) & /@
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.

-