以上是opecv reference里面的说明。
Image必须是8位单通道图(可以使灰度图、二值图、边缘图等)
Rho:距离分辨率,一般为1
Theta:角度分辨率,一般为CV_PI/180
Threshold:阈值,只返回像素和大于threshold的直线
Srn:(猜测)距离缩放
Stn:(猜测)角度缩放
重点说明
Lines:
—p: 直线到原点(左上角)的距离
—:角度
这里的角度文档上有写是vertical line~0°,horizontal line~90°但是它没说方向是顺时针啊!
这导致我不知道下面这个角度到底是怎么计算的
An example using the Hough line detector can be found at opencv_source_code/samples/cpp/houghlines.cpp
大家可以再画图里画两条直线验证
比如下图片的角度是 90°,90°,91°(分别是三个不同粗细的线),从其最近的那个坐标起点开始,顺时针旋转到待测直线的位置,旋转的角度就是直线的角度
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp" #include <iostream> using namespace cv;
using namespace std; static void help()
{
cout << "\nThis program demonstrates line finding with the Hough transform.\n"
"Usage:\n"
"./houghlines <image_name>, Default is pic1.png\n" << endl;
} int main(int argc, char** argv)
{
const char* filename = argc >= ? argv[] : "d:/vs/cardPic/w.png"; Mat src = imread(filename, );
if(src.empty())
{
help();
cout << "can not open " << filename << endl;
return -;
} Mat dst, cdst;
Canny(src, dst, , , );
cvtColor(dst, cdst, COLOR_GRAY2BGR); vector<Vec2f> lines;
HoughLines(dst, lines, , CV_PI/, , , ); for( size_t i = ; i < lines.size(); i++ )
{
float rho = lines[i][], theta = lines[i][];
cout<<i<<":"<<theta*/CV_PI<<endl;
Point pt1, pt2;
double a = cos(theta), b = sin(theta);
double x0 = a*rho, y0 = b*rho;
pt1.x = cvRound(x0 + *(-b));
pt1.y = cvRound(y0 + *(a));
pt2.x = cvRound(x0 - *(-b));
pt2.y = cvRound(y0 - *(a));
line( cdst, pt1, pt2, Scalar(,,), , CV_AA);
} imshow("source", src);
imshow("detected lines", cdst); waitKey(); return ;
}