Search code examples
opencvcomputer-visionlinedetectionvision

What is the most basic line detection method in OpenCV


I'm just learning OpenCV, and have a question about line detection. I have a situation where I need to detect a horizontal black line on a white background. I am guaranteed that the line will always show up horizontally (within a few degrees) and need to detect where it is in the images from the camera.

My thought is, since it is always horizontal, I can just search vertically for the "edge" through a few columns on the image, and call it good. Maybe even narrow the amount of pixels I'm capturing from the camera as an extra boost in speed.

Is there a builtin function for this type of line detection though?

I don't need the extra power, and cannot afford the processing time of Canny or Hough, I just want to find a guaranteed horizontal line as fast as possible.

The images (with my solution running) look like this:

Left is thresholded image, right is solution running. Green bars are horizontal edges, red mark the tape strips I'm interested in tracking.


Solution

  • The method I ended up going with is a for loop. After thresholding the image, I search along two columns to find all the "edges" or changes in value. Then I process this list to find only horizontal pairs of edges.

    I then find all lines that are close enough together, and have a desired infill (boolean comparison against thresholded image), which effectively only finds the strips of tape I am interested in tracking.

    This takes about 1/50th the time of JUST the Canny call, not including the findContours, etc that are also necessary. I have not tested against Hough however, but I believe this will still be significantly faster.

    Since the biggest issue was processing speed, I made several other optimizations as well.

    Code can be found here (It's very well commented, I promise): Code as a gist