(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!).