这个系列的目的是通过对OpenCV示例,进一步了解OpenCV函数的使用,不涉及具体原理。
目录
简介
Example运行截图
Example分析
Example代码
简介
本文记录了对OpenCV示例
lsd_lines
.cpp
的分析。
这个示例主要演示了如何使用
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算法的类。
示例涉及函数:
|
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;
}
参考资料:
2.《
LSD在opencv中的实现
》
3.《
LSD直线检测
》