檢測直線(利用cv::ximgproc::FastLineDetector)

來源:互聯網
上載者:User

介紹:

在OpenCV3.0 以上版本的contrib模組中,有一個cv::ximgproc::FastLineDetector類。定義位置:

// 需要下載contrib模組opencv_contrib/modules/ximgproc/include/opencv2/ximgproc/fast_line_detector.hpp
所謂Fast,是相對於正式模組中的 cv::imgproc::LineSegmentDetector類來說,速度更快。FastLineDetector類依據下邊的論文實現:

// 論文Outdoor Place Recognition in Urban Environments using Straight Lines// 下載地址:http://cvlab.hanyang.ac.kr/~jwlim/files/icra14linerec.pdf
大致的原理是:

先在輸入映像上,應用canny邊緣檢測;然後根據在canny邊緣映像上進行分析,找到直線。


常式:

該常式對比了上述倆直線檢測運算元的運行耗時情況。

#include <iostream>#include <stdio.h>#include <unistd.h>#include <stdlib.h>#include <string.h>#include <string>#include <dirent.h>#include <unistd.h>#include <vector>#include <sstream>#include <fstream>#include <sys/io.h>#include <sys/times.h>#include <iomanip>#include <tuple>#include <cstdlib>using namespace std;#include "opencv2/imgproc.hpp"#include "opencv2/ximgproc.hpp"#include "opencv2/imgcodecs.hpp"#include "opencv2/highgui.hpp"using namespace cv;using namespace cv::ximgproc;int main(int argc, char** argv){    std::string in;    cv::CommandLineParser parser(argc, argv, "{@input|../samples/data/corridor.jpg|input image}{help h||show help message}");    if (parser.has("help"))    {        parser.printMessage();        return 0;    }    in = parser.get<string>("@input");    Mat image = imread(in, IMREAD_GRAYSCALE);    if( image.empty() )    {        return -1;    }    // Create LSD detector    Ptr<LineSegmentDetector> lsd = createLineSegmentDetector();    vector<Vec4f> lines_lsd;    // Create FLD detector    // Param               Default value   Description    // length_threshold    10            - Segments shorter than this will be discarded    // distance_threshold  1.41421356    - A point placed from a hypothesis line    //                                     segment farther than this will be    //                                     regarded as an outlier    // canny_th1           50            - First threshold for    //                                     hysteresis procedure in Canny()    // canny_th2           50            - Second threshold for    //                                     hysteresis procedure in Canny()    // canny_aperture_size 3             - Aperturesize for the sobel    //                                     operator in Canny()    // do_merge            false         - If true, incremental merging of segments    //                                     will be perfomred    int    length_threshold    = 10;    float  distance_threshold  = 1.41421356f;    double canny_th1           = 50.0;    double canny_th2           = 50.0;    int    canny_aperture_size = 3;    bool   do_merge            = false;    Ptr<FastLineDetector> fld = createFastLineDetector(            length_threshold,            distance_threshold,             canny_th1,             canny_th2,             canny_aperture_size,            do_merge);    vector<Vec4f> lines_fld;    // Because of some CPU's power strategy, it seems that the first running of    // an algorithm takes much longer. So here we run both of the algorithmes 10    // times to see each algorithm's processing time with sufficiently warmed-up    // CPU performance.    for(int run_count = 0; run_count < 10; run_count++) {        lines_lsd.clear();        int64 start_lsd = getTickCount();        lsd->detect(image, lines_lsd);        // Detect the lines with LSD        double freq = getTickFrequency();        double duration_ms_lsd = double(getTickCount() - start_lsd) * 1000 / freq;        std::cout << "Elapsed time for LSD: "                   << setw(10) << setiosflags(ios::right) << setiosflags(ios::fixed) << setprecision(2)                   << duration_ms_lsd << " ms." << std::endl;        lines_fld.clear();        int64 start = getTickCount();        // Detect the lines with FLD        fld->detect(image, lines_fld);        double duration_ms = double(getTickCount() - start) * 1000 / freq;        std::cout << "Ealpsed time for FLD: "                   << setw(10) << setiosflags(ios::right) << setiosflags(ios::fixed) << setprecision(2)                  << duration_ms << " ms." << std::endl;    }        // Show found lines with LSD    Mat line_image_lsd(image);    lsd->drawSegments(line_image_lsd, lines_lsd);    imshow("LSD result", line_image_lsd);    // Show found lines with FLD    Mat line_image_fld(image);    fld->drawSegments(line_image_fld, lines_fld);    imshow("FLD result", line_image_fld);    waitKey();        return 0;}
結果:




可以看出,倆演算法的效果差不多;但FLD要更快。


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.