Line segment detection OpenCV C++ source code

(Update: A faster version of LSWMS is now available. Visit the new post to check it out!)

I am happy to announce that I am sharing a little piece of code I’ve been working in for some time. It is the line segment detector, written in C++, and using OpenCV 2.x libraries.

Here you can find a more detailed description of the algorithm and the code (it is prepared for CMake compilation with a CMakeLists.txt file, and a main.cpp sample using the LSWMS class).Its name is LSWMS (Line Segment detection using Weighted Mean-Shift), and it is a variant of the SSWMS algorithm we published recently (and which was derived from my work during my PhD thesis in cooperation with Dr. Carlos Cuevas at the Grupo de Tratamiento de Imágenes).

This method is based on a random sampling strategy combined with growing procedures based on the Bresenham algorithm and refinement using Mean-Shift.

Compared with the Hough-based method currently used by the OpenCV function HoughLinesP, the LSWMS has the following properties:

  • Do not need to tune input parameters, it works the same for any kind of images
  • You can determine how many line segments you want as output
  • It runs faster in most situations (specially defining 100 line segments at maximum)
  • We get as output an estimation of the orientation error for each line segment
  • It has been fully programmed in C++/OpenCV2.x, so it is quite easy to follow and use

In summary, I worked on this because I’ve always found the Hough method (ant specially the PPHT) pretty difficult to tune (I mean, to find the “good” values for each specific problem), and many times the noise of non-linear-like elements make that the output of Hough is not useful at all.

Here you are some images that show the result of using the LSWMS (in red), and the HoughLinesP of OpenCV (in blue) for the same image. Notice that the Hough method generates too much line segments in noisy regions, while the LSWMS keeps detecting only good line segments.

With the LSWMS I solved the problem of parameter tuning (no need to tune parameters), and also made a good line segment detection method available for real-time requisites (working with small videos of about 320×240 and asking for 100 line segments the method can run at 30 fps without problem in a laptop).

Hope you find it useful (and in case so, please add a cite to my paper about it! Thanks!).

This entry was posted in Computer vision, OpenCV and tagged , , , . Bookmark the permalink.

13 Responses to Line segment detection OpenCV C++ source code

  1. Pingback: Line Segment Detection C++ code (better than Hough line detection in openCV) | Next Virtual Reality

  2. Lyn says:

    Hi Marcos,

    I’m so happy to use LSWMS. Would you please tell me what hardware configuration you were using to achieve “real time”? Gracias!


    • Hi Lyn,

      Thanks for writing!
      Actually this implementation is not optimized at all. I did it a couple of years ago only for testing purposes and never had the chance to go into deep C/C++.
      It runs in real-time in PC architectures, such as Intel Core Duo. I believe I have tested it on my (old) laptop, with 2 GB RAM 1.3 GHz.
      Best regards!
      De nada!


  3. Hi I’m very new to opencv. I am now testing Darwin’s example:
    I would try to apply their approach in the construction of autonomous vehicle.
    Is it possible to have access to your code?
    Anyway thank you for the work you have published.
    .. / ntmeneses (lisbon)

  4. Pingback: (Faster) Line segment detection OpenCV C++ source code | Marcos Nieto's Blog

  5. Joel Teply says:

    Really good job, thanks for your work. I was already going to throw away hough and write my own custom algorithm, but this is such a good start that I’ll work with it. Hough is just awful.

  6. Ragavan says:

    Hi Marco

    I am working on line detection algorithms, when I search for it your page is appeared. I went through your code, I have some difficulties to understand the algorithm which you used. So could you give me a short explanation about the algorithm?

    Thank you

  7. Matt says:

    Hi Marko,

    Thank you for your job. Reulsts seems to be amazing.
    Is there a way to find a python implementation of your library?
    In any case, it is really impressive.

    Thank you,


    • Hi,

      Thanks for your possitive comments. Alas, I am not providing new versions of these pieces of SW, so sorry about that. Probably you can use the OpenCV’s Python implementation to create the LSWMS in that language.



Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s