1. 程式人生 > >opencv中的標準霍夫線變換(HoughLines)和統計霍夫變換(HoughLinesP)

opencv中的標準霍夫線變換(HoughLines)和統計霍夫變換(HoughLinesP)

一、下面首先對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) //把浮點數轉化成整數