# 利用OpenCV的霍夫变换线检测函数HoughLines()得到直线的ρ和θ值后绘制直线的原理详解

https://blog.csdn.net/wenhao_ir/article/details/51774444

OpenCV的霍夫变换线检测函数HoughLines()是利用极坐标下的参数ρ和θ值来表示直线的，我们在实际应用中往往要根据直线的参数ρ和θ值来绘制出直线，那么怎么绘制呢？

void cv::line	(	InputOutputArray img,
Point 	pt1,
Point 	pt2,
const Scalar & 	color,
int 	thickness = 1,
int 	lineType = LINE_8,
int 	shift = 0
)


//博主微信/QQ 2487872782
//有问题可以联系博主交流
//有图像处理需求也可联系博主
//图像处理技术交流QQ群 271891601

//OpenCV版本：3.0
//VS版本：2012

#include
#include
#include
#include

#include

using namespace cv;
using namespace std;

#define HOUGH_VOTE 100

int main()
{
if (!srcImg1.data)
{
return -1;
}

vector<Vec2f> lines;
float pi180 = (float)CV_PI / 180;

HoughLines(srcImg1, lines, 1, pi180, HOUGH_VOTE, 0, 0);
int numLines = lines.size();

for (int l = 0; l<numLines; l++)
{
float theta = lines[l][1];
cout <<"检测到的第"<< l+1<<"条线的角度为"<<":" << theta* 180 / CV_PI<< endl;

}

waitKey(0);

return 0;

}


p t 1. x = x 0 − L s i n ( θ ) = x 0 + ( − L s i n ( θ ) ) pt1.x = x0-Lsin(θ)=x0+(-Lsin(θ))
p t 1. y = y 0 + L c o s ( θ ) pt1.y = y0+Lcos(θ)

p t 2. x = x 0 + L s i n ( θ ) = x 0 − ( − L s i n ( θ ) ) pt2.x = x0+Lsin(θ)=x0-(-Lsin(θ))
p t 2. y = y 0 − L c o s ( θ ) pt2.y = y0-Lcos(θ)

x 0 = ρ c o s ( θ ) x0=ρcos(θ)
y 0 = ρ s i n ( θ ) y0=ρsin(θ)

	HoughLines(magImg, lines, 1, pi180, HOUGH_VOTE, 0, 0);
int numLines = lines.size();
int L = 1000;
for (int l = 0; l<numLines; l++)
{
float rho = lines[l][0], theta = lines[l][1];
Point pt1, pt2;
double a = cos(theta), b = sin(theta);
double x0 = a*rho, y0 = b*rho;
pt1.x = cvRound(x0 + L * (-b));
pt1.y = cvRound(y0 + L * (a));
pt2.x = cvRound(x0 - L * (-b));
pt2.y = cvRound(y0 - L * (a));
line(linImg, pt1, pt2, Scalar(255, 0, 0), 3, 8, 0);
}