1. 程式人生 > >LSD在opencv中的實現

LSD在opencv中的實現

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