(Faster) Line segment detection OpenCV C++ source code

Hi guys!
I’ve been working on enhancing the LSWMS line segment detector. You can find the new version in the trunk of the repository at sourceforige (https://sourceforge.net/projects/lswms/).
It is now really fast, and still quite accurate. The idea behind is that I profiled the performance of LSWMS and realized that the Mean-Shift part took most of the computational power, and in some cases, it added not that much accuracy.

Here you are some results:

Prado: 448 x 336,
LSWMS:      63 ms, 193 segments
LS:                 0 ms, 243 segments
PPHT:         109 ms, 228 segments

Road:  360 x 288
LSWMS:    62 ms, 137 segments
LS:            16 ms, 173 segments
PPHT:        31 ms, 110 segments

Church: 600 x 400
LSWMS:    124 ms, 258 segments
LS:            15 ms, 410 segments
PPHT:        78 ms, 469 segments

Station: 600 x 450
LSWMS:    156 ms, 469 segments
LS:             32 ms, 48 segments
PPHT:        125 ms, 679 segments


lswmsON_prado lswmsOFF_prado


lswmsON_road lswmsOFF_road


lswmsON_station lswmsOFF_station


lswmsON_church lswmsOFF_church

And the corresponding results using the PPHT algorithm from OpenCV 2.4.7.

ppht_prado ppht_road
ppht_station ppht_church

The results of LSWMS with and without weighted Mean-Shift are pretty similar. And better than PPHT. Also now LSWMS is faster than PPHT disabling Mean-Shift, so I would say that LSWMS is my preferred option for detecting line segments in real-time applications.

I must admit LSWMS generates noisy results, sometimes erroneous, but when I have used LSWMS in application like vanishing point detection it was the best option because it provided enough accuracy and great speed.

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

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

10 Responses to (Faster) Line segment detection OpenCV C++ source code

  1. Pingback: Line segment detection OpenCV C++ source code | Marcos Nieto's Blog

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

  3. Mark says:

    But why does it double the lines it finds?

    • Hi!
      This is not the usual behaviour. You can check some example output images in the blog.
      However, in this version I have removed some parts of the original algorithm and it may result in unexpected behaviour. That said, the lines are fitted to edges computed with Sobel. If your image has thick visual edges, the Sobel operator can lead to edges at both sides of the visual edge.
      Kind regards,


  4. ted says:

    Is there a way to define a minimum line length? I get a lot of noisy little lines which are not even close to a line and it misses a lot of clear long lines compared to houghLinesP.
    After some tests, I have the impression, that houghLinesP has actually better results.

    • Hi Ted,
      No directly. You can filter out the line segments afterwards. But if you are getting noisy, small line segments it might be due to the type of image. In my experiments (you can see some images in the blog) the results show nice long line segments, and some non-negligible amount of noisy ones. Normally, I would use these line segments for vanishing point estimation, so smaller line segments contribute less.
      Thanks for writing!

  5. Philippe says:

    Awesome…. needs to be included in opencv !
    When is this happening?

    • Hi!
      Thanks for your comment.
      I believe OpenCV 3 has a new line segment detector somewhere in contribs or in some other module (I have not checked it), and it seems to provide good, accurate results. For fast results, they rely on the Hough transform, although I believe it is worst than the LSWMS.

  6. Clarence says:

    Thanks for your great job! I download the code and find that it still uses WMS. Is it the right faster-LSWMS version without WMS?
    Look forward to your reply :)

    • Hi!
      There’s been some time I don’t check this SW (the lswms project) , but I’ve spotted that I included an argument to the LSWMS class constructor:

      LSWMS lswms(procSize, R, numMaxLSegs, useWMS, verbose);

      So, useWMS is a boolean that can be set to false. The main.cpp is an example on how to set this useWMS value through program argument.



Leave a Reply

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

WordPress.com Logo

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

Google photo

You are commenting using your Google 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 )

Connecting to %s