16
$\begingroup$

I'm trying to get my mind wrapped around Mathematica's image feature recognition functions. I'm intrigued by the ImageLines function for a couple future ideas I have but generally get very poor performance from it.

...as I will now demonstrate:

Let's import some very basic gridlines, surely this will be easy.

pic = Import[
    "http://www.samplewords.com/docthumbs/home-graphpaper-thumb.jpg"]

enter image description here

Straight out of the box, here's how Mathematica recognizes the lines at different thresholds (parallelized because of my impatience):

Row@ParallelTable[Graphics@Line@ImageLines[pic, i], {i, .1, 1, .1}]

enter image description here

Interesting... not what I would've expected.

Even if I dilate and binarize the image, I still get pretty bad results:

enter image description here

Playing with the distictness parameter, it gets a little better but still pretty weird.

enter image description here

So, Wolfram shows some pretty neat examples, and it seems to work well there.

How can I get improved results from the ImageLines function?

$\endgroup$
0

4 Answers 4

14
$\begingroup$

Mathematica's ImageLines function is looking for white lines on black background, try this:

ColorNegate@pic

Mathematica graphics

Graphics@Line@ImageLines@ColorNegate@pic

Mathematica graphics

Much more successful, not sure why it missed one, though.

$\endgroup$
12
$\begingroup$

Here you may see that with a little preconditioning, the match is perfect:

pic = ColorNegate@Import["http://www.samplewords.com/docthumbs/home-graphpaper-thumb.jpg"];
p = Closing[Binarize@ImageTake[pic, 480], 1]; (*See nikie's comment*)
Show[p, Graphics[{Thick, Orange, Line /@ ImageLines[p]}]]

Mathematica graphics

$\endgroup$
7
  • 1
    $\begingroup$ You come in with a doozy as soon as I accept another answer... $\endgroup$
    – kale
    Commented Sep 12, 2012 at 3:08
  • $\begingroup$ @kale It's OK. If you really need it (for other occasion, poor @s0urce!), the accepting is reversible :) $\endgroup$ Commented Sep 12, 2012 at 3:10
  • $\begingroup$ @belsarius, Ha. I know, but what cold, heartless, ingrate would rip away the satisfaction of an accepted answer? :) $\endgroup$
    – kale
    Commented Sep 12, 2012 at 3:12
  • 1
    $\begingroup$ @kale Now seriously: You should allow more time between issuing a question and accepting one answer. That way you will get more and better answers. Also remember that many users live in the antipodes, so they will not see your question until tomorrow. Let's say that two days is a good measure until accepting. $\endgroup$ Commented Sep 12, 2012 at 3:32
  • 3
    $\begingroup$ Just nitpicking: Erosion[Dilation[...] is the same as Closing[...]. I'm not sure if there's a performance difference, but it's more readable. $\endgroup$ Commented Sep 12, 2012 at 8:42
7
$\begingroup$

In image processing, 1 is white and 0 is black (in binary). This is opposite of what is normal in functions like ArrayPlot. For example:

Row@{Image[IdentityMatrix@10, ImageSize -> 200],
  ArrayPlot[IdentityMatrix@10, ImageSize -> 200]}

enter image description here

Thus, your lines need to be white on black for the algorithm to recognize it as a line. Otherwise, it tries to detect lines among the voids...

ParallelTable[Graphics@Line@ImageLines[Binarize@ColorNegate@pic, i], 
    {i, .1, 1, .1}]~Partition~4 // Grid

enter image description here

$\endgroup$
6
  • 3
    $\begingroup$ I beat you by 20 seconds! I'm excited. $\endgroup$
    – s0rce
    Commented Sep 12, 2012 at 2:06
  • $\begingroup$ @s0rce I'm slipping! ;) I did add more info though :) $\endgroup$
    – rm -rf
    Commented Sep 12, 2012 at 2:15
  • $\begingroup$ @s0rce and R.M, is this documented anywhere? Common knowledge among image-o-philes? $\endgroup$
    – kale
    Commented Sep 12, 2012 at 2:18
  • 1
    $\begingroup$ @kale Well, 1 is always on (pixel on) and 0 is always off (pixel off)... so the algorithms really only look for that. What is different is our perception of what white and black mean. The color scheme in ArrayPlot and also the ubiquitous use of black ink on white paper might have warped our way of looking at a black and white image, but really, all you need to consider are the 0s and 1s. $\endgroup$
    – rm -rf
    Commented Sep 12, 2012 at 2:22
  • $\begingroup$ @R.M, Thanks for the info. I actually did this the other day, but thought I was crazy for inverting the image. I'll +1 you and accept s0rce's answer simply because 20 seconds is 20 seconds and you have enough reputation to last until eternity. :) $\endgroup$
    – kale
    Commented Sep 12, 2012 at 2:58
7
$\begingroup$

It's a minor point, but when I was exploring the potential of ImageLines, I found the Segmented->True option quite useful. I was fascinated at the lines that ImageLines was finding...

lena = ExampleData[{"TestImage", "Lena"}];
Graphics[{AbsoluteThickness[0.4], 
   Map[Line, ImageLines[lena, .55, "Segmented" -> True]]}, 
  Background -> Black]  /. 
 Line[pts_] :> {RandomChoice[{Red, Green, Blue, Orange, White}], 
   Line[pts]}

lina

$\endgroup$
3
  • 1
    $\begingroup$ I wonder what could result with the full Lena ...+1 $\endgroup$ Commented Sep 12, 2012 at 18:13
  • $\begingroup$ @belisarius Szabolcs is probably the one for that... :) $\endgroup$
    – cormullion
    Commented Sep 13, 2012 at 6:09
  • $\begingroup$ @cormullion That was his swan song $\endgroup$ Commented Sep 13, 2012 at 6:15

Your Answer

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

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