简介
本文记录了对OpenCV示例
houghlines
.cpp
的分析。
这个示例主要演示了如何使用
HoughLinesP
对图像进行直线检测。
示例涉及到
HoughLinesP。
HoughLinesP
HoughLinesP函数就是利用概率霍夫变换来检测直线的。它的一般步骤为:
1、随机抽取图像中的一个特征点,即边缘点,如果该点已经被标定为是某一条直线上的点,则继续在剩下的边缘点中随机抽取一个边缘点,直到所有边缘点都抽取完了为止;
2、对该点进行霍夫变换,并进行累加和计算;
3、选取在霍夫空间内值最大的点,如果该点大于阈值的,则进行步骤4,否则回到步骤1;
4、根据霍夫变换得到的最大值,从该点出发,沿着直线的方向位移,从而找到直线的两个端点;
5、计算直线的长度,如果大于某个阈值,则被认为是好的直线输出,回到步骤1。
函数原型:
void HoughLinesP(InputArray image,OutputArray lines, double rho, double theta, int threshold, double minLineLength=0,double maxLineGap=0 )
参数说明:
image:输入图像,要求是8位单通道图像
lines:输出的直线向量,每条线用4个元素表示,即直线的两个端点的4个坐标值
rho和theta:分别为距离和角度的分辨率
threshold:阈值,即步骤3中的阈值
minLineLength:最小直线长度,在步骤5中要用到,即如果小于该值,则不被认为是一条直线
maxLineGap:最大直线间隙,在步骤4中要用到,即如果有两条线段是在一条直线上,但它们之间因为有间隙,所以被认为是两个线段,如果这个间隙大于该值,则被认为是两条线段,否则是一条。
PS:以上描述摘至参考资料 1.《 Opencv2.4.9源码分析——HoughLinesP 》 |
Example截图
原图
|
效果图
|
![]() |
![]() |
Example分析
1.从命令行参数加载图像
const string filename = argc >= 2 ? argv[1] : "../data/pic1.png";
Mat src = imread(filename, IMREAD_GRAYSCALE);
if (src.empty())
{
help();
cout << "can not open " << filename << endl;
return -1;
}
2.使用Canny函数对图像进行边缘检测
Mat mask;
cv::Canny(src, mask, 100, 200, 3);
注意:
Canny函数返回的图像为灰度图像,背景为黑色,前景为白色(OpenCV中所谓的二值化的灰度图,虽然还是灰度图)
关于Canny函数的使用可以参考:
OpenCV3.0 Examples学习笔记(6)-edge.cpp
3.创建显示检测效果的目标图像
Mat dst_cpu;
cv::cvtColor(mask, dst_cpu, COLOR_GRAY2BGR);
4.使用HoughLinesP对图像进行直线检测
vector<Vec4i> lines_cpu;
{
const int64 start = getTickCount();
cv::HoughLinesP(mask, lines_cpu, 1, CV_PI / 180, 50, 60, 5);
const double timeSec = (getTickCount() - start) / getTickFrequency();
cout << "CPU Time : " << timeSec * 1000 << " ms" << endl;
cout << "CPU Found : " << lines_cpu.size() << endl;
}
注意:
(1)这里使用
getTickCount检测算法运行时间;
5.在目标图像上绘制直线检测结果
for (size_t i = 0; i < lines_cpu.size(); ++i)
{
Vec4i l = lines_cpu[i];
line(dst_cpu, Point(l[0], l[1]), Point(l[2], l[3]), Scalar(0, 0, 255), 3, LINE_AA);
}
6.显示源图像和直线检测的结果图像
imshow("source", src);
imshow("detected lines [CPU]", dst_cpu);
Example代码
- #include <cmath>
- #include <iostream>
- #include "opencv2/core.hpp"
- #include <opencv2/core/utility.hpp>
- #include "opencv2/highgui.hpp"
- #include "opencv2/imgproc.hpp"
- #include "opencv2/cudaimgproc.hpp"
- using namespace std;
- using namespace cv;
- using namespace cv::cuda;
- static void help()
- {
- cout << "This program demonstrates line finding with the Hough transform." << endl;
- cout << "Usage:" << endl;
- cout << "./gpu-example-houghlines <image_name>, Default is ../data/pic1.png\n" << endl;
- }
- int main(int argc, const char* argv[])
- {
- const string filename = argc >= 2 ? argv[1] : "../data/pic1.png";
- Mat src = imread(filename, IMREAD_GRAYSCALE);
- if (src.empty())
- {
- help();
- cout << "can not open " << filename << endl;
- return -1;
- }
- Mat mask;
- cv::Canny(src, mask, 100, 200, 3);
- Mat dst_cpu;
- cv::cvtColor(mask, dst_cpu, COLOR_GRAY2BGR);
- Mat dst_gpu = dst_cpu.clone();
- vector<Vec4i> lines_cpu;
- {
- const int64 start = getTickCount();
- cv::HoughLinesP(mask, lines_cpu, 1, CV_PI / 180, 50, 60, 5);
- const double timeSec = (getTickCount() - start) / getTickFrequency();
- cout << "CPU Time : " << timeSec * 1000 << " ms" << endl;
- cout << "CPU Found : " << lines_cpu.size() << endl;
- }
- for (size_t i = 0; i < lines_cpu.size(); ++i)
- {
- Vec4i l = lines_cpu[i];
- line(dst_cpu, Point(l[0], l[1]), Point(l[2], l[3]), Scalar(0, 0, 255), 3, LINE_AA);
- }
- GpuMat d_src(mask);
- GpuMat d_lines;
- {
- const int64 start = getTickCount();
- Ptr<cuda::HoughSegmentDetector> hough = cuda::createHoughSegmentDetector(1.0f, (float) (CV_PI / 180.0f), 50, 5);
- hough->detect(d_src, d_lines);
- const double timeSec = (getTickCount() - start) / getTickFrequency();
- cout << "GPU Time : " << timeSec * 1000 << " ms" << endl;
- cout << "GPU Found : " << d_lines.cols << endl;
- }
- vector<Vec4i> lines_gpu;
- if (!d_lines.empty())
- {
- lines_gpu.resize(d_lines.cols);
- Mat h_lines(1, d_lines.cols, CV_32SC4, &lines_gpu[0]);
- d_lines.download(h_lines);
- }
- for (size_t i = 0; i < lines_gpu.size(); ++i)
- {
- Vec4i l = lines_gpu[i];
- line(dst_gpu, Point(l[0], l[1]), Point(l[2], l[3]), Scalar(0, 0, 255), 3, LINE_AA);
- }
- imshow("source", src);
- imshow("detected lines [CPU]", dst_cpu);
- imshow("detected lines [GPU]", dst_gpu);
- waitKey();
- return 0;
- }