views:

66

answers:

4

Hello,

I need a little help on an image analysis algorithm in Java. I basically have images like this: alt text

So, as you might guessed, I need to count the lines.

What approach do you think would be best?

Thanks, Smaug

A: 

First, decide the angle that is allowed for a "vertical" line. I'd say 45 degrees.

Then get the background colors - grab the pixles in each corner, for example. Or more properly - get the most predominant colors.

Then start to inspect pixels horizontally. Whenever a non-background color is encountered, assume a line starts there. On the next row of pixels (or after no more than X rows, if it is possible for a line to not be completely continous) check whether there's a non-background color in the "neighbour" pixels, determined by the angle you chose initially. So for 45 degrees you'd have to check more pixels than for 10 degrees.

I'm not sure this is sufficient, and it might yield imperfect results, but there comes your role to optimize it.

Bozho
+3  A: 

A simple segmentation algorithm can help you out. Heres how the algorithm works:

  • scan pixels from left to right and record the position of the first black (whatever the color of your line is) pixel.
  • carry on this process unless you find one whole scan when you don't find the black pixel. Record this position as well.
  • We are just interested in the Y positions here. Now using this Y position segment the image horizontally.
  • Now we are going to do the same process but this time we are going to scan from top to bottom (one column at a time) in the segment we just created.
  • This time we are interested in X positions.
  • So in the end we get every lines extents or you can say a bounding box for every line.
  • The total count of these bounding boxes is the number of lines.

You can do many optimizations in the algorithm according to your needs.

Faisal Feroz
+1 nice explanation. This is used in OCRs as well for character segmentation.
Tingu
Is there a Java API to achieve this?
SidCool
A: 

It depends on how much they look like that.

  1. Bring the image to 1-bit (black and white) in a way that preserves the lines and brings the background to pure white
  2. Perhaps do simple cleanup like speck removal (remove any small black components).

Then,

  1. Find a black pixel
  2. Use flood-fill algorithms to find its extent
  3. See if the shape meets the criteria for being a line (lineCount++ if so)
  4. remove it
  5. Repeat this until there are no black pixels

A lot depends on how good you do #3, some ideas

  1. Use Hough just on this section to check that you have one line, and that it is vertical(ish)
  2. (after #1) rotate it to the vertical and check its width/height ratio
Lou Franco