LSD算法是一种直线检测的算法,比hough效果好,作者将代码和文章上传了,详见http://www.ipol.im/pub/art/2012/gjmr-lsd/。
opencv3.0也集成了其算法,这边说下如何在opencv里面调用。下面代码其实也是opencv给的example 可以在http://docs.opencv.org/master/d8/dd4/lsd_lines_8cpp-example.html#a9
#include <iostream>
#include <string>
#include "opencv2/core/core.hpp"
#include "opencv2/core/utility.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/imgcodecs.hpp"
#include "opencv2/highgui/highgui.hpp"
using namespace std;
using namespace cv;
int main(int argc, char** argv)
{
std::string in;
if (argc != 2)
{
std::cout << "Usage: lsd_lines [input image]. Now loading ../data/building.jpg" << std::endl;
in = "../data/building.jpg";
}
else
{
in = argv[1];
}
Mat image = imread(in, IMREAD_GRAYSCALE);//读入原图,需为灰度图像
#if 0
Canny(image, image, 50, 200, 3); // Apply canny edge//可选canny算子
#endif
// Create and LSD detector with standard or no refinement.
#if 1
Ptr<LineSegmentDetector> ls = createLineSegmentDetector(LSD_REFINE_STD);//或者两种LSD算法,这边用的是standard的
#else
Ptr<LineSegmentDetector> ls = createLineSegmentDetector(LSD_REFINE_NONE);
#endif
double start = double(getTickCount());
vector<Vec4f> lines_std;
// Detect the lines
ls->detect(image, lines_std);//这里把检测到的直线线段都存入了lines_std中,4个float的值,分别为起止点的坐标
double duration_ms = (double(getTickCount()) - start) * 1000 / getTickFrequency();
std::cout << "It took " << duration_ms << " ms." << std::endl;
// Show found lines
Mat drawnLines(image);
ls->drawSegments(drawnLines, lines_std);
imshow("Standard refinement", drawnLines);
waitKey();
return 0;
}
这边初学,有许多不正确的地方,还请批评指正。
对LSD原理的东西这两篇写的比较好
http://blog.csdn.net/carson2005/article/details/9326847
http://blog.csdn.net/polly_yang/article/details/10085401
大家有兴趣也可以对比下源码看看,我把opencv里面LSD的源码也整理出来了
http://download.csdn.net/detail/mollylee1011/8961213