1.计算rou最大值
2.初始化数组进行计分统计
3.数组存储rou和theta,注意数组不越界
4.进行三领域极大值抑制,设定阈值
5.计算出极坐标的角度和长度进行画线
6.结束
void Houghline(Mat img,Mat rgbimg)
{
plines lines[1000];
//vector <Vec2f> lines;
int size;
const double PI = 3.14159265;
//乘2防止后续计算结果小于0
//计算rou最大值
size = 2*sqrt(img.cols * img.cols + img.rows * img.rows);
//设立角度范围<0,180>取1°为步长
int angle;
angle = 180;
int** p = new int* [size];
for (int i = 0; i < size; i++)
{
p[i] = new int[angle ];
}
for (int i = 0; i < size; i++)
{
for (int j = 0; j < angle; j++)
{
p[i][j] = 0;
}
}
int n=0;
for (int i = 0; i < img.rows; i++)
{
for (int j = 0; j < img.cols; j++)
{
if (img.at <uchar>(i, j) != 0)
{
for (int k = 0; k < angle; k ++)
{
//防止越界
n = j * cos(k * PI / 180.0) + i * sin(k * PI / 180.0)+size/2;
p[n][k]++;
}
}
}
}
int count = 0;
int count1 = 0;
//非极大值抑制
for (int n = 0; n < size; n++)
{
for (int j = 0; j < angle; j++)
{
if (p[n][j] > 200 && p[n][j] > p[n-1][j] && p[n][j] > p[n ][j+1] &&
p[n][j] > p[n ][j-1] && p[n][j] > p[n + 1][j]&& p[n][j] > p[n - 1][j-1] && p[n][j] > p[n+1][j + 1] &&
p[n][j] > p[n+1][j - 1] && p[n][j] > p[n + 1][j-1])
{
lines[count++].rou = n-size/2;
lines[count1++].theta = j;
}
}
}
}