OpenCV3.0 Examples学习笔记(15)-lsd_lines.cpp-LSD直线检测

这个系列的目的是通过对OpenCV示例,进一步了解OpenCV函数的使用,不涉及具体原理。

目录
简介
Example运行截图
Example分析
Example代码

简介
本文记录了对OpenCV示例 lsd_lines .cpp 的分析。
资料地址:http://docs.opencv.org/3.0.0/d8/dd4/lsd_lines_8cpp-example.html

这个示例主要演示了如何使用 OpenCV 对图像进行LSD直线检测。
相对于传统的Hough,LSD对参数的依赖少很多,因此实用性更高。

示例涉及到
(1)如何使用createLineSegmentDetector创建LineSegmentDetector对象;
(2)如何使用LineSegmentDetector对象进行LSD直线检测。

createLineSegmentDetector
创建一个LineSegmentDetector的智能指针,并初始化

函数原型:
CV_EXPORTS_W Ptr<LineSegmentDetector> createLineSegmentDetector(
    int _refine = LSD_REFINE_STD, double _scale = 0.8,
    double _sigma_scale = 0.6, double _quant = 2.0, double _ang_th = 22.5,
    double _log_eps = 0, double _density_th = 0.7, int _n_bins = 1024);

参数说明:
_refine:OpenCV中提供三种LSD初始化参数,如下:
LSD_REFINE_NONE,没有改良的方式;
LSD_REFINE_STD,标准改良方式,将带弧度的线(拱线)拆成多个可以逼近原线段的直线度;
LSD_REFINE_ADV,进一步改良方式,计算出错误警告数量,通过增加精度,减少尺寸进一步精确直线。

PS:其他参数暂时没有研究和使用过。完全使用默认值:)

LineSegmentDetector

用于实现Line segment detector算法的类。


示例涉及函数:
编号
函数
说明
1
void detect(InputArray _image, OutputArray _lines,  OutputArray width = noArray(), OutputArray prec = noArray(),  OutputArray nfa = noArray());
采用LSD算法检测直线,
直线结果存储在vector<Vec4f>中,以(x1, y1, x2, y2),形式
2
void drawSegments(InputOutputArray _image, InputArray lines);
绘制直线检测结果,
直线以vector<Vec4f>形式,(x1, y1, x2, y2)


PS:关于LineSegmentDetector具体内容可以查看 参考资料: 1.《 cv::LineSegmentDetector Class Referenceabstract

Example运行截图
原图
效果


Example分析
1.从命令行参数中加载图像,并以灰度模式
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);

2.声明LineSegmentDetector对象,用于lsd直线检测
#if 1
    Ptr<LineSegmentDetector> ls = createLineSegmentDetector(LSD_REFINE_STD);
#else
    Ptr<LineSegmentDetector> ls = createLineSegmentDetector(LSD_REFINE_NONE);
#endif

注意:
(1)显然写LSD的人和以前OpenCV代码的维护人员风格差异很大。

3.开始计时
double start = double(getTickCount());

4.声明直线检测存储对象
vector<Vec4f> lines_std;

5.lsd直线检测
ls->detect(image, lines_std);

6.结束计时
double duration_ms = (double(getTickCount()) - start) * 1000 / getTickFrequency();

7.声明绘制直线检测的图像
Mat drawnLines(image);

注意:
(1)采用这种方式声明的Mat是深拷贝,也就是说 drawnLines和image由各自的内存空间

8.绘制直线检测结果
ls->drawSegments(drawnLines, lines_std);

9.显示直线检测结果
imshow("Standard refinement", drawnLines);

Example代码
#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
#endif

    // Create and LSD detector with standard or no refinement.
#if 1
    Ptr<LineSegmentDetector> ls = createLineSegmentDetector(LSD_REFINE_STD);
#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);

    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;
}

参考资料:
3.《 LSD直线检测
  • 4
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值