第一张是原图,第二张是这个最好canny结果图
//1,拷贝8位数组图像
byte[] jinzita1024 = new byte[1024 * 768]
int hh = 768;
int ww = 1024;第一张
for (int j = 0; j < hh; j++)
for (int i = 0; i < ww; i++)
{
jinzita1024[j * ww + i] = glob_buffer8[j * ww + i];
}
double[] tiduhe;
tiduhe = new double[ww * hh];
double[] gy = new double[ww * hh];
double[] gx = new double[ww * hh];
double[] angle = new double[ww * hh];
double[] CA_tempImage33gaos = new double[ww * hh];
/2,高斯平滑
for (int j = 1; j < (hh - 1); j++)
{
for (int i = 1; i < (ww - 1); i++)
{
int n0 = (j * ww + i);
CA_tempImage33gaos[n0] = (jinzita1024[n0 - ww - 1] + 2 * jinzita1024[n0 - ww] + jinzita1024[n0 - ww + 1] +
2 * jinzita1024[n0 - 1] + 4 * jinzita1024[n0] + 2 * jinzita1024[n0 + 1] +
jinzita1024[n0 + ww - 1] + 2 * jinzita1024[n0 + ww] + jinzita1024[n0 + ww + 1]) / 16.0;
}
}
for (int i = 1; i < (hh - 1); i++)
{
for (int j = 1; j < (ww - 1); j++)
{
// 3,此处改为 sobelx,sobely20220327
double Grady = CA_tempImage33gaos[(i - 1) * ww + j - 1] + 2 * CA_tempImage33gaos[(i - 1) * ww + j] + CA_tempImage33gaos[(i - 1) * ww + j + 1]
- CA_tempImage33gaos[(i + 1) * ww + j - 1] - 2 * CA_tempImage33gaos[(i + 1) * ww + j] - CA_tempImage33gaos[(i + 1) * ww + j + 1]
;
double Gradx = CA_tempImage33gaos[(i - 1) * ww + j + 1] + 2 * CA_tempImage33gaos[i * ww + j + 1] + CA_tempImage33gaos[(i + 1) * ww + j + 1]
- CA_tempImage33gaos[(i - 1) * ww + j - 1] - 2 * CA_tempImage33gaos[i * ww + j - 1] - CA_tempImage33gaos[(i + 1) * ww + j - 1];
tiduhe[i * ww + j] = Math.Sqrt(Gradx * Gradx + Grady * Grady);
// 4,这个函数博客里边有jiaoduAndxiangxian
angle[i * ww + j] = jiaoduAndxiangxian(Gradx, Grady) * 180 / Math.PI;//角度
gx[i * ww + j] = Gradx;
gy[i * ww + j] = Grady;
}
}5,非极大值抑制
double g1 = 0, g2 = 0, g3 = 0, g4 = 0;
double dTmp1 = 0.0, dTmp2 = 0.0;
double dWeight = 0.0;
double[] img_N = new double[ww * hh];
for (int i = 1; i < (ww - 1); i++)
{
for (int j = 1; j < (hh - 1); j++)
{
int fangbian = j * ww + i;
if (tiduhe[fangbian] == 0)
{
img_N[fangbian] = 0;
}
else
{
//材///
/// g1 g2 /
/// C /
/// g4 g3 /
///
if (((angle[fangbian] >= 45) && (angle[fangbian] < 90)) ||
((angle[fangbian] >= 225) && (angle[fangbian] < 270)))//
{
g1 = tiduhe[fangbian - ww - 1];
g2 = tiduhe[fangbian - ww];
g4 = tiduhe[fangbian + ww];
g3 = tiduhe[fangbian + ww + 1];
// if (gy[fangbian] != 0)//为什么这个是多余的?
// {
dWeight = Math.Abs(gx[fangbian] / (gy[fangbian])); // p->Gradx,q->Grady
dTmp1 = g1 * dWeight + g2 * (1 - dWeight);
dTmp2 = g3 * dWeight + g4 * (1 - dWeight);
// }
// else { }
}
材///
/ g1 /
/ g4 C g2 /
/ g3 /
/
// else
if (((angle[fangbian] >= 135) && (angle[fangbian] < 180)) ||//shuipingfangxiang
((angle[fangbian] >= 315) && (angle[fangbian] < 360)))//20220817
{ //int nPointIdx = i+j*w;
g3 = tiduhe[fangbian + ww - 1];
g2 = tiduhe[fangbian + 1];
g1 = tiduhe[fangbian - ww + 1];
g4 = tiduhe[fangbian - 1];
// if (gx[fangbian] != 0)//为什么这个是多余的?
// {
dWeight = Math.Abs(gy[fangbian] / (gx[fangbian])); // p->Gradx,q->Grady
dTmp1 = g1 * dWeight + g2 * (1 - dWeight);
dTmp2 = g3 * dWeight + g4 * (1 - dWeight);
// }
// else
// {
//先不处理20220817
// }
}
材///
/ g2 g1 /
/ C /
/ g3 g4 /
/
//else
if (((angle[fangbian] >= 90) && (angle[fangbian] <135)) ||
((angle[fangbian] >= 270) && (angle[fangbian] < 315))) //20220817
{ //int nPointIdx = i+j*w;
g2 = tiduhe[fangbian - ww];
g1 = tiduhe[fangbian - ww + 1];
g4 = tiduhe[fangbian + ww];
g3 = tiduhe[fangbian + ww - 1];
// if (gy[fangbian] != 0)//为什么这个是多余的?
// {
dWeight = Math.Abs(gx[fangbian] / (gy[fangbian])); // p->Gradx,q->Grady
dTmp1 = g1 * dWeight + g2 * (1 - dWeight);
dTmp2 = g3 * dWeight + g4 * (1 - dWeight);
// }
// else { }
}
//材///
/// g3 /
/// g4 C g2 /
/// g1 /
///
//else
if (((angle[fangbian] >= 0) && (angle[fangbian] < 45)) ||
((angle[fangbian] >= 180) && (angle[fangbian] < 225)))//这个是45度方向
{ //一共四个方向,已经判断了3个方向,这个可以不必判断了
g1 = tiduhe[fangbian + ww + 1];
g2 = tiduhe[fangbian + 1];
g3 = tiduhe[fangbian - ww - 1];
g4 = tiduhe[fangbian - 1];
// if (gx[fangbian] != 0)//为什么这个是多余的?
// {
dWeight = Math.Abs(gy[fangbian] / (gx[fangbian])); //
dTmp1 = g1 * dWeight + g2 * (1 - dWeight);
dTmp2 = g3 * dWeight + g4 * (1 - dWeight);
// }
// else { }
}
}
if ((tiduhe[fangbian] >= dTmp1) && (tiduhe[fangbian] >= dTmp2))
{
img_N[fangbian] = tiduhe[fangbian];//梯度图像
}
else
{
img_N[fangbian] = 0;
}
}
}
以上是canny最关键的处理已经结束了。