4

I have an image which consists of somehow dotted lines:

NOTE: Open the image, to enlage it and see all the small dots

Dotted lines

How can I use openCV to detect and parametrize those lines?

This image are the values of a laser range scanner on a robot and I need to get all the lines as good a possible.


The HoughLinesP function should be ideal for this?

I thried the following code:

    //converts laser range readings to a binary image.
    cv::Mat binaryImg = laserRangesToBinaryImage();

    cv::Mat cdst;
    cvtColor(binaryImg, cdst, CV_GRAY2BGR);

    std::vector<cv::Vec4i> lines;
    HoughLinesP(binaryImg, lines, 2, 5.0*CV_PI/180.0, 1, 2, 20 );
    for( size_t i = 0; i < lines.size(); i++ )
    {
        cv::Vec4i l = lines[i];
        line( cdst, cv::Point(l[0], l[1]), cv::Point(l[2], l[3]), cv::Scalar(0,0,255), 3, CV_AA);
    }

    cv::imshow("Hough output", cdst);

which results in about 50 to 60 lines (using openCV 2.8.3 on Ubuntu 14.04):

Hough transform results

The biggest problem here is that there are multiple separated line segments where a full line should be detected. So the segments aren't correctly connected. Some of the lines are too short or not even detected.

The optimal result should look like this (manually created) with about 20 line segments:

Manually annotated image

How can I achieve this result?

Stefan Profanter
  • 6,458
  • 6
  • 41
  • 73
  • Have you tried dilate and erode? this can join this points and turn them into lines, and then the algorithm may detected better. Also it is possible to check for segments that are close to each other and unite them. – api55 Mar 22 '15 at 22:30
  • you can either 1. use houghLines method instead of HoughLinesP 2. use gap parameter of hlp 3. cluster and connect your small line segments – Micka Mar 23 '15 at 06:34

1 Answers1

2

If you didn't already, have a look at this tutorial.

Basically, you should act on these three parameters (the last three parameters of the HoughLinesP function), until you reach the right length for the detected lines:

  1. threshold: The minimum number of intersections (in the Hough space) to “detect” a line.
  2. minLinLength: The minimum number of points that can form a line. Lines with less than this number of points are disregarded.
  3. maxLineGap: The maximum gap between two points to be considered in the same line.

Mathematical morphology (closing) could help too, as it was mentionned in the comments. However, the help will be limited: a small 3x3 kernel is recommended for such a task, so if some pixels on a line are too far from one another the gap won't be filled up anyway.

CTZStef
  • 1,675
  • 2
  • 18
  • 47