18
$\begingroup$

I'm not sure if this is even possible with Mathematica, but I want to extract the angles between all lines in this picture:

enter image description here

It consists of $12$ lines, so I want a matrix with $144$ entries. I really have no idea even how to begin. Any ideas?

$\endgroup$
2
  • $\begingroup$ Thank you! Now I've got the endpoints of the 12 lines..that's nice so far. But how to extract the angles? $\endgroup$
    – holistic
    Feb 2, 2014 at 11:55
  • $\begingroup$ 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? $\endgroup$ Feb 3, 2014 at 8:10

2 Answers 2

21
$\begingroup$

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*)

enter image description here

$\endgroup$
5
  • $\begingroup$ 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 :) $\endgroup$
    – holistic
    Feb 2, 2014 at 12:08
  • 2
    $\begingroup$ Instead of Tuples and Partition you could use angle = Outer[VectorAngle, vector, vector, 1] $\endgroup$ Feb 2, 2014 at 12:13
  • 1
    $\begingroup$ @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. $\endgroup$
    – Kuba
    Feb 2, 2014 at 12:13
  • $\begingroup$ @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. ;) $\endgroup$
    – Kuba
    Feb 2, 2014 at 12:16
  • 1
    $\begingroup$ Ok..I'll wait with the accept :). One can learn a lot by seeing different approaches to a problem. $\endgroup$
    – holistic
    Feb 2, 2014 at 12:23
15
$\begingroup$

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"]

Mathematica graphics

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

Mathematica graphics

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

Mathematica graphics

The coordinates are swapped as usual in an Image/Graphics mapping.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.