在使用霍夫线变换之前,首先要对图像进行边缘检测的处理。
主要有三种:
HoughLines()
1.标注霍夫变换(SHT)
2.多尺度霍夫变换(MSHT)
HoughLinesP() 执行效率更高
3.累计概率霍夫变换(PPHT)
void HoughLines( InputArray image, OutputArray lines,
double rho, double theta, int threshold,
double srn = 0, double stn = 0,
double min_theta = 0, double max_theta = CV_PI );
image | 输入图像(8位单通道二进制图像) |
lines | 检测到线条的输出矢量。 |
rho | 以像素为单位的距离精度。 |
theta | 以弧度为单位的角度精度。 |
threshold | 累加平面的阈值参数。 |
srn | 默认值0, 对于多尺度霍夫变换,这是第三个参数进步尺度rho的除数距离。 |
stn | 默认值0.对于多尺度霍夫变换,这是第三个参数进步角度rho的除数距离。 如果srn stn都为0,则表示使用经典霍夫变换。 |
void HoughLinesP( InputArray image, OutputArray lines,
double rho, double theta, int threshold,
double minLineLength=0, double maxLineGap = 0);
image | 输入图像(8位单通道二进制图像) |
lines | 检测到线条的输出矢量。每一条线由具有4个元素的矢量(x1,y1)和(x2,y2) |
rho | 以像素为单位的距离精度。 |
theta | 以弧度为单位的角度精度。 |
threshold | 累加平面的阈值参数。 |
minLineLength | 默认值0, 表示最低线段长度。 |
maxLineGap | 允许同一行点与点之间连接起来的最大的距离 |
经过测试,HoughLinesP函数比HoughLine函数好用的多。
#include<opencv2/opencv.hpp>
#include<opencv2/highgui.hpp>
#include<opencv2/imgproc/imgproc.hpp>
#include<vector>
using namespace cv;
using namespace std;
int main()
{
Mat canny_dst,gray_dst,dst;
Mat src = imread("F:/1.jpg");
cvtColor(src, gray_dst, COLOR_RGB2GRAY);
//blur(gray_dst, gray_dst, Size(3, 3));
Canny(gray_dst, canny_dst, 50, 200, 3);
vector<Vec4i>lines;
HoughLinesP(canny_dst, lines, 1.0, CV_PI / 180, 80, 50, 10);
for (int i = 0; i < lines.size(); i++)
{
Vec4i l = lines[i];
line(src, Point(l[0],l[1]), Point(l[2], l[3]), Scalar(186, 88,255), 1, LINE_AA);
}
imshow("src", src);
imshow("canny_dst", canny_dst);
waitKey(0);
return 0;
}
功能:cvRound(), cvFloor(), cvCeil()函数讲解。
函数cvRound,cvFloor,cvCeil 都是用一种舍入的方法将输入浮点数转换成整数:
cvRound():返回跟参数最接近的整数值,即四舍五入;
cvFloor():返回不大于参数的最大整数值,即向下取整;
cvCeil():返回不小于参数的最小整数值,即向上取整;