opencv 筆記19 Imgproc_HoughLines

#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"

#include <iostream>

using namespace cv;
using namespace std;

void help()
{
 cout << "\nThis program demonstrates line finding with the Hough transform.\n"
         "Usage:\n"
         "./houghlines <image_name>, Default is pic1.jpg\n" << endl;
}

int main(int argc, char** argv)
{
 const char* filename = argc >= 2 ? argv[1] : "pic1.jpg";

 Mat src = imread(filename, 0);
 if(src.empty())
 {
     help();
     cout << "can not open " << filename << endl;
     return -1;
 }

 Mat dst, cdst;
 Canny(src, dst, 50, 200, 3);
 cvtColor(dst, cdst, CV_GRAY2BGR);

 #if 0
  vector<Vec2f> 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<Vec4i> 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;
}

void HoughLines(Mat& image, vector<Vec2f>& lines, double rho, double theta, int threshold, double srn=0, double stn=0)

Parameters:                          
  • image – The 8-bit, single-channel, binary source image. The image may be modified by the function
  • lines – The 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) and \theta is the line rotation angle in radians ( 0 \sim \textrm{vertical line}, \pi/2 \sim \textrm{horizontal line} )
  • rho – Distance resolution of the accumulator in pixels
  • theta – Angle resolution of the accumulator in radians
  • threshold – The accumulator threshold parameter. Only those lines are returned that get enough votes ( >\texttt{threshold} )
  • srn – For the multi-scale Hough transform it is the divisor for the distance resolution rho . The coarse accumulator distance resolution will be rho and the accurate accumulator resolution will be rho/srn . If both srn=0 and stn=0 then the classical Hough transform is used, otherwise both these parameters should be positive.
  • stn – For the multi-scale Hough transform it is the divisor for the distance resolution theta
void HoughLinesP(Mat& image, vector<Vec4i>& lines, double rho, double theta, int threshold, double minLineLength=0, double maxLineGap=0)

Parameters:          
  • image – The 8-bit, single-channel, binary source image. The image may be modified by the function
  • lines – The 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 line segment detected.
  • rho – Distance resolution of the accumulator in pixels
  • theta – Angle resolution of the accumulator in radians
  • threshold – The accumulator threshold parameter. Only those lines are returned that get enough votes ( >\texttt{threshold} )
  • minLineLength – The minimum line length. Line segments shorter than that will be rejected
  • maxLineGap – The maximum allowed gap between points on the same line to link them.

統計霍夫變換較好!




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章