David
David

Reputation: 53

Detecting straight lines in an image

I'm trying to extract a portion of an image separated by straight horizontal lines. The image is of a text document of which most is irrelevant for my purposes. The header portion of the document is usually all that I need and is separated by two straight horizontal lines (the line style does vary slightly with one line or a double line with one thicker than the other.) with one on the top and bottom of the header section. The length of the header section is variable so I cannot set a fixed position.

How do I go about extracting this portion of the image? I've looked into a few things like using Hough Transformations and libraries like OpenCV or Accord.net but all of this seems like overkill for something so simple. I'd prefer not to have to use an additional library if possible.

So ideally,I want to input an image and it gives me the position of every straight horizontal line it detects and then I use that to set the bounds for cropping the image. Can anyone point me in the right direction?

Upvotes: 2

Views: 5883

Answers (2)

Neil
Neil

Reputation: 666

If you know how thick the lines are you could run through the first 10 pixels of every row in the image so x < 10 and y < imageheight.

Run through this line by line with a bool check value that you set to true every pixel row and false if you find a pixel which is not the color of the line.

Then have a count that gets set to 0 if the bool value that checks the line is false and increments if it is true.

You could break the loop if you find that count > linethickness and use that location of y to know where the line is.

Upvotes: 0

Timothy Groote
Timothy Groote

Reputation: 8643

This is tipycally done with a Hough transform.

It's hard to explain how it works in a single answer post, and probably past the scope of your question, but working implementations of hough transforms can be found in many vision libraries like OpenCV and AForge (there is also a .NET version of aforge called AForge.NET)

A Hough transform will output detected lines, and allow you to filter them by angle. That should make it fairly easy to filter out everyhting that is not a horizontal line.

The image below is an example of lines detected with a Hough Transform, translated back to the normal image. (overlayed as red lines)

enter image description here

Upvotes: 8

Related Questions