kirsch边缘检测算子

时间:2022-01-10 14:37:22

之前在项目中检测划痕的时候用到了Kirsch边缘检测算子,但是是用halcon,现在用opencv实现了一遍,不难,就是计算过程要花时间。
Kirsch算子是R.Kirsch提出来一种边缘检测算法,它采用8个3*3的模板对图像进行卷积,这8个模板代表8个方向,并取最大值作为图像的边缘输出,8个模板如下,它在保持细节和抗噪声方面都有较好的效果:
kirsch边缘检测算子

实现代码如下:
static int a0[3][3]={{+5,+5,+5},{-3,0,-3},{-3,-3,-3}};
static int a1[3][3]={{-3,+5,+5},{-3,0,+5},{-3,-3,-3}};
static int a2[3][3]={{-3,-3,+5},{-3,0,+5},{-3,-3,+5}};
static int a3[3][3]={{-3,-3,-3},{-3,0,+5},{-3,+5,+5}};
static int a4[3][3]={{-3,-3,-3},{-3,0,-3},{+5,+5,+5}};
static int a5[3][3]={{-3,-3,-3},{+5,0,-3},{+5,+5,-3}};
static int a6[3][3]={{+5,-3,-3},{+5,0,-3},{+5,-3,-3}};
static int a7[3][3]={{+5,+5,-3},{+5,0,-3},{-3,-3,-3}};

for (i=1;i<r-2;i++)
{
for (j=1;j<c-2;j++)
{
for (int x = 0; x < 8; x++)
{
sumKirsch[x] = 0;
}
for (k=0;k<=2;k++)
{
for (l=0;l<=2;l++)
{
sumKirsch[0] += (int)img1.at<uchar>(i - 1 + k, j - 1 + l)*a0[k][l];
sumKirsch[1] += (int)img1.at<uchar>(i - 1 + k, j - 1 + l)*a1[k][l];
sumKirsch[2] += (int)img1.at<uchar>(i - 1 + k, j - 1 + l)*a2[k][l];
sumKirsch[3] += (int)img1.at<uchar>(i - 1 + k, j - 1 + l)*a3[k][l];
sumKirsch[4] += (int)img1.at<uchar>(i - 1 + k, j - 1 + l)*a4[k][l];
sumKirsch[5] += (int)img1.at<uchar>(i - 1 + k, j - 1 + l)*a5[k][l];
sumKirsch[6] += (int)img1.at<uchar>(i - 1 + k, j - 1 + l)*a6[k][l];
sumKirsch[7] += (int)img1.at<uchar>(i - 1 + k, j - 1 + l)*a7[k][l];
}
}
max = sumKirsch[0];
for (m=1;m<8;m++)
{
if (max<sumKirsch[m])
max=sumKirsch[m];
}
if (max<0)
max=0;
if(max>255)
max=255;
img2.at<uchar>(i,j)=max;
}
}

结果如下,边缘提取效果还可以。
kirsch边缘检测算子