版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_38204686/article/details/79346348
//两点之间的距离
double juli(int x1,int y1,int x2,int y2)
{
return sqrt(double((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)));
}
//输入经过霍夫变换得到的直线 存储起点和终点的vector 直线最短距离 点之间间隔最长距离
void HoughLinesP(Mat &img,vector<Vec2f> &lines,vector<Vec4i> &points, double minLineLength=0,double maxLineGap=1)
{
Vec4i point;
uchar *p;
float r,theta;
bool start,end;
int x,y;
for(size_t i = 0;i < lines.size(); i++)
{
r = lines[i][0];
theta = lines[i][1];
start = false;
end = false;
//水平 极径与x轴90 直线与x轴0
if(sin(theta) > 0.99999)
{
y = cvRound(r);
p = img.ptr<uchar>(y);
for(x = 0; x < img.cols; x++)
{
if( (p[x] != 0) || (x == img.cols - 1) )
{
//起点
if(start == false)
{
point[0] = x;
point[1] = y;
start = true;
continue;
}
//终点
if(end == false)
{
//初始一个终点
if(juli(point[0],point[1],x,y) <= maxLineGap)
{
point[2] = x;
point[3] = y;
end = true;
}
//不符合则重新确立起点
else
{
point[0] = x;
point[1] = y;
}
continue;
}
//移动终点
if(juli(point[2],point[3],x,y) <= maxLineGap)
{
point[2] = x;
point[3] = y;
}
else
{
//确定一条直线
if(juli(point[0],point[1],point[2],point[3]) >= minLineLength)
{
points.push_back(point);
}
start = false;
end = false;
}
}
}
}
else
{
for(y = 0; y < img.rows; y++)
{
x = cvRound((r-y*sin(theta))/cos(theta));
p = img.ptr<uchar>(y);
if( (p[x] != 0) || (y == img.rows - 1) )
{
//起点
if(start == false)
{
point[0] = x;
point[1] = y;
start = true;
continue;
}
//终点
if(end == false)
{
//初始一个终点
if(juli(point[0],point[1],x,y) <= maxLineGap)
{
point[2] = x;
point[3] = y;
end = true;
}
//不符合则重新确立起点
else
{
point[0] = x;
point[1] = y;
}
continue;
}
//移动终点
if(juli(point[2],point[3],x,y) <= maxLineGap)
{
point[2] = x;
point[3] = y;
}
else
{
//确定一条直线
if(juli(point[0],point[1],point[2],point[3]) >= minLineLength)
{
points.push_back(point);
}
start = false;
end = false;
}
}
}
}
}
}