#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include
using namespace cv;
using namespace std;
void help()
{
cout << "\nThis program demonstrates line finding with the Hough transform.\n"
"Usage:\n"
"./houghlines , 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 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;
}
void HoughLines(Mat& image, vector& 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 . is the distance from the coordinate origin (top-left corner of the image) and is the line rotation angle in radians ( )
- 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 ( )
- 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&
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 , where and 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 ( )
- 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.
|
统计霍夫变换较好!