el-Hakeem
el-Hakeem

Reputation: 57

How to find the longest line of a binary image using matlab

enter image description here

As you can see in image, I have image that contain many lines. how can I get the longest line using matlab, then remove the other lines?

Upvotes: 1

Views: 3689

Answers (2)

Noremac
Noremac

Reputation: 3547

First, as mentioned by Lokesh A. R., the line you are wanting in your image is not the longest since it is not continuous.

If you really do want that line to be found, then take a look at dilation, which will expand the lines, cause small breaks in the line to connect, then reduce the line back to its same thickness. However, this risks connecting lines that you don't want to connect, and is probably not reliable unless the lines in the image are far enough apart.

If you really do want just the longest line, then I would suggest an algorithm (that you would have to code)

  1. iterate over the image pixels
    1. Mark each pixel you iterate over as "visited"
    2. When you find a pixel, look at all of its immediate 9 neighbors to check to see if the line continues, and follow it if it does (doing the same thing), keeping track of both how long it is, where it started (so you know where to go back to when you finish following the line), and which pixel coordinates are part of the line (for easy removal of the rest of the image).
      1. After following the line, go back to the line's starting point and start iterating again.
  2. Analyze your collection of lines that you have gathered and keep the one with the longest line (or has the most pixels).
  3. Set every pixel in the image to black unless it is part of the one line you are keeping.

This is a basic algorithm for detecting objects in an image and it may be possible that matlab can do this step. Take a look at mathworks page on object detection and see if it can "detect" the lines in your image and give you stats about them (like the number of pixels in each)

Upvotes: 2

rockinfresh
rockinfresh

Reputation: 2106

Here, read this: http://www.mathworks.com/help/images/ref/houghlines.html

Should basically answer your question. HoughLines is one of the more common method for detecting lines.

Upvotes: 0

Related Questions