opencv中的标准霍夫线变换(HoughLines)和统计霍夫变换(HoughLinesP)

 

本文主要讲解标准霍夫线变换(HoughLines)和统计霍夫变换(HoughLinesP)函数,并对函数在调用中的一些细节进行些讲解。 
针对标准霍夫线变换(HoughLines)和统计霍夫变换(HoughLinesP)的一些细节和原理可以参考: 
http://www.opencv.org.cn/opencvdoc/2.3.2/html/doc/tutorials/imgproc/imgtrans/hough_lines/hough_lines.html#hough-lines

一、下面首先对HoughLines函数进行讲解: 
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 ) 
Parameters: 
1、image – 8-bit, single-channel binary source image. The image may be modified by the function. 
输入的图像应该是8为单通道二值图像 
2、lines—Output vector of lines. Each line is represented by a two-element vector (\rho, \theta) . \rho is the distance from the coordinate origin (0,0) (top-left corner of the image). \theta is the line rotation angle in radians ( 0 \sim \textrm{vertical line}, \pi/2 \sim \textrm{horizontal line} ). 
输出的直线,lines代表的是一个容器,包含(\rho, \theta)的容器 
3、rho – Distance resolution of the accumulator in pixels. 
极径参数的距离分辨率 
4、theta – Angle resolution of the accumulator in radians. 
极角参数的角度分辨率 
5、threshold – Accumulator threshold parameter. Only those lines are returned that get enough votes (>\texttt{threshold} ). 
设定的阈值,大于此阈值的交点,才会被认为是一条直线 
之后几个参数可以用默认值 
6、srn – For the multi-scale Hough transform, it is a divisor for the distance resolution rho . The coarse accumulator distance resolution is rho and the accurate accumulator resolution is rho/srn . If bothsrn=0 and stn=0 , the classical Hough transform is used. Otherwise, both these parameters should be positive. 
7、stn – For the multi-scale Hough transform, it is a divisor for the distance resolution theta. 
min_theta – For standard and multi-scale Hough transform, minimum angle to check for lines. Must fall between 0 and max_theta./ 
8、max_theta – For standard and multi-scale Hough transform, maximum angle to check for lines. Must fall between min_theta and CV_PI. 
之后几个参数可以用默认值

二、下面首先对HoughLinesP函数进行讲解: 
void HoughLinesP(InputArray image, OutputArray lines, double rho, double theta, int threshold, doubleminLineLength=0, double maxLineGap=0 ) 
Parameters: 
1、image – 8-bit, single-channel binary source image. The image may be modified by the function. 
输入的图像应该是8为单通道二值图像 
2、lines – Output vector of lines. Each line is represented by a 4-element vector (x_1, y_1, x_2, y_2) , where(x_1,y_1) and (x_2, y_2) are the ending points of each detected line segment. 
rho – Distance resolution of the accumulator in pixels. 
输出的矢量。输出的两点是线段的两个端点 
3、rho – Distance resolution of the accumulator in pixels. 
极径参数的距离分辨率 
4、theta – Angle resolution of the accumulator in radians. 
极角参数的角度分辨率 
5、threshold – Accumulator threshold parameter. Only those lines are returned that get enough votes (>\texttt{threshold} ). 
设定的阈值,大于此阈值的交点,才会被认为是一条直线 
6、minLineLength – Minimum line length. Line segments shorter than that are rejected. 
线段的最小长度 
7、maxLineGap – Maximum allowed gap between points on the same line to link them. 
点到直线被允许的最大距离 
三、对代码进行讲解

if 0 
  vector lines; 
  HoughLines(dst, lines, 1, CV_PI/180, 100, 0, 0 ); 
  for( size_t i = 0; i < lines.size(); i++ ) 
  { 
     float rho = lines[i][0], theta = lines[i][1]; 
     Point pt1, pt2; 
     double a = cos(theta), b = sin(theta); 
     double x0 = a*rho, y0 = b*rho; 
     pt1.x = cvRound(x0 + 1000*(-b)); 
     pt1.y = cvRound(y0 + 1000*(a)); 
     pt2.x = cvRound(x0 - 1000*(-b)); 
     pt2.y = cvRound(y0 - 1000*(a)); 
     line( cdst, pt1, pt2, Scalar(0,0,255), 3, CV_AA); 
  } 
#else 
     vector lines; 
     HoughLinesP(dst, lines, 1, CV_PI/180, 50, 50, 10 ); 
    for( size_t i = 0; i < lines.size(); i++ ) 
    { 
        Vec4i l = lines[i]; 
        line( cdst, Point(l[0], l[1]), Point(l[2], l[3]), Scalar(0,0,255), 3, CV_AA); 
   } 
#endif 
imshow(“source”, src); 
imshow(“detected lines”, cdst);

waitKey();

return 0; 
}

1、c++中 Vector 是一个类模板。不是一种数据类型。 Vector是一种数据类型。

2、 typedef    vec<uchar, 2>    Vec2b;
    typedef    vec<uchar, 3>    Vec3b;
    typedef    vec<uchar, 4>    Vec4b;

 typedef    vec<short, 2>    Vec2s;
 typedef    vec<short, 3>    Vec3s;
 typedef    vec<short, 4>    Vec4s;

 typedef    vec<int, 2>    Vec2i;
 typedef    vec<int, 3>    Vec3i;
 typedef    vec<int, 4>    Vec4i;

 typedef    vec<float, 2>    Vec2f;
 typedef    vec<float, 3>    Vec3f; 
 typedef    vec<float, 4>    Vec4f;
 typedef    vec<float, 6>    Vec6f;

 typedef    vec<double, 2>    Vec2d;
 typedef    vec<double, 3>    Vec3d; 
 typedef    vec<double, 4>    Vec4d;
 typedef    vec<double, 6>    Vec6d;

3、函数讲解:

 float rho = lines[i][0], theta = lines[i][1]; //返回极径和极角
 Point pt1, pt2;  
 double a = cos(theta), b = sin(theta);  //确定cos和sin的值
 double x0 = a*rho, y0 = b*rho;    //确定X0和Y0点

 pt1.x = cvRound(x0 + 1000*(-b)); 
 pt1.y = cvRound(y0 + 1000*(a)); 
 pt2.x = cvRound(x0 - 1000*(-b)); 
 pt2.y = cvRound(y0 - 1000*(a)); 
 line( cdst, pt1, pt2, Scalar(0,0,255), 3, CV_AA); 

通过X0和Y0向上向下个找两点,确定一条直线。1000只是个数,也可以用其他的数。

4、cvRound 
int cvRound (double value) //把浮点数转化成整数

  • 10
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值