void HoughLinesP(InputArray image,OutputArray lines,double rho,double theta,int threshold,double minLineLength=0,double maxLineGap=0);
void HoughLines(InputArray Image,OutputArray lines,double rho,double theta,int threshold,double srn=0,double stn=0);
e.g:
#include <opencv2\core\core.hpp>
#include <opencv2\highgui\highgui.hpp>
#include <opencv2\imgproc\imgproc.hpp>
#include <iostream>
using namespace cv;
using namespace std;
int main(void)
{
Mat srcImage = imread("picture4.jpg");
if (!srcImage.data)
{
return -1;
}
imshow("srcImage", srcImage);
Mat edgeMat, houghMat;
//Canny边缘检测,二值图像
Canny(srcImage, edgeMat,50,200,3);
cvtColor(edgeMat, houghMat,CV_GRAY2BGR);
#if 0
//标准的霍夫检测
vector<Vec2f> lines;
HoughLines(edgeMat, 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][0];
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(houghMat,pt1,pt2,Scalar(0,0,255),3,CV_AA);
}
#else
//统计概率的霍夫变换
vector<Vec4i> lines;
HoughLinesP(edgeMat,lines,1,CV_PI/180,50,50,10);
for (size_t i = 0; i < lines.size(); i++)
{
Vec4i l = lines[i];
line(houghMat, Point(l[0], l[1]), Point(l[2],l[3]),Scalar(0,0,255),3,CV_AA);
}
#endif
imshow("Hough", houghMat);
waitKey(0);
return 0;
}
运行结果如下: