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    Vec2b;
    typedef    vec    Vec3b;
    typedef    vec    Vec4b;

 typedef    vec    Vec2s;
 typedef    vec    Vec3s;
 typedef    vec    Vec4s;

 typedef    vec    Vec2i;
 typedef    vec    Vec3i;
 typedef    vec    Vec4i;

 typedef    vec    Vec2f;
 typedef    vec    Vec3f; 
 typedef    vec    Vec4f;
 typedef    vec    Vec6f;

 typedef    vec    Vec2d;
 typedef    vec    Vec3d; 
 typedef    vec    Vec4d;
 typedef    vec    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) //把浮点数转化成整数

你可能感兴趣的:(Opencv3.0学习)