C#程序实现Canny边缘检测算法


标签:
高斯边缘检测梯度杂谈 |
分类: 影像匹配 |
转载自:http://blog.csdn.net/yjz_uestc/article/details/6664937
然后,将每个像素点的梯度的值和方向分别放入两个数组中,程序如下:
-
<span
style= "font-size:16px;">byte[]orients new= byte[width * //height]; 梯度方向数组 -
float[,]
gradients new= float[width, height]; //梯度值数组 -
double
gx, gy; -
for
( inti = 1; i < (height - 1);i++ ) -
{ -
for ( intj = 1; j < (width - 1); j++) -
{ -
//求水平和竖直导数 -
gx = bufdata[(i - 1) * width + j] + bufdata[(i + 1) * width + j] - bufdata[(i -1) * width + j - 1] - bufdata[(i + 1) * width + j - 1]+ 2*(bufdata[i * width + j + 1] - bufdata[i * width + j - 1]); -
gy = bufdata[(i - 1) * width + j - 1] + bufdata[(i + 1) * width + j + 1] - bufdata[(i + 1) * width + j - 1] - bufdata[(i + 1) * width + j + 1]+ 2*(bufdata[(i - 1) * width + j] - bufdata[(i + 1) * width + j - 1]); -
gradients[j, i] = (float)Math.Sqrt(gx * gx + gy * gy); -
if (gx == 0) -
{ -
orientation = (gy == 0) ? 0 : 90; -
} -
else -
{ -
double div double)gy= ( / gx; -
-
if (div < 0) -
{ -
orientation = 180 - Math.Atan(-div) * toAngle; -
} -
else -
{ -
orientation = Math.Atan(div) * toAngle; -
} -
//只保留成4个方向 -
if (orientation < 22.5) -
orientation = 0; -
else if (orientation < 67.5) -
orientation = 45; -
else if (orientation < 112.5) -
orientation = 90; -
else if (orientation < 157.5) -
orientation = 135; -
else orientation = 0; -
} -
orients[i*width+j] = (byte)orientation; -
} -
} </span>
-
<span "font-size:16px;">style= float leftPixel = 0, rightPixel = 0; -
for ( inty = 1; y <height-1; y++) -
{ -
for ( intx = 1; x < width-1; x++) -
{ -
//获得相邻两像素梯度值 -
switch (orients[y * width + x]) -
{ -
case 0: -
leftPixel = gradients[x - 1, y]; -
rightPixel = gradients[x + 1, y]; -
break; -
case 45: -
leftPixel = gradients[x - 1, y + 1]; -
rightPixel = gradients[x + 1, y - 1]; -
break; -
case 90: -
leftPixel = gradients[x, y + 1]; -
rightPixel = gradients[x, y - 1]; -
break; -
case 135: -
leftPixel = gradients[x + 1, y + 1]; -
rightPixel = gradients[x - 1, y - 1]; -
break; -
} -
if ((gradients[x, y] < leftPixel) || (gradients[x, y] < rightPixel)) -
{ -
dis[y * disdata.Stride + x] = 0; -
} -
else -
{ -
dis[y * disdata.Stride + x] = (byte)(gradients[x, y] //maxGradient是最大梯度/maxGradient* 255); -
} -
-
} -
}
</span>
-
<span
style= "font-size:16px;">fmean= //某统计信息fmean / maxGradient * 255; -
highThreshold
= (byte)(fmean);//高阈值 -
lowThreshold
= (byte)(0.4 * //低阈值highThreshold); -
for
( inty = 0; y < height; y++) -
{ -
for ( intx = 0; x < width; x++) -
{ -
if (dis[y * disdata.Stride + x] < highThreshold) -
{ -
if (dis[y * disdata.Stride + x] < lowThreshold) -
{ -
dis[y * disdata.Stride + x] = 0; -
} -
else -
{ -
if ((dis[y * disdata.Stride + x - 1] < highThreshold) && -
(dis[y * disdata.Stride + x + 1] < highThreshold) && -
(dis[(y - 1) * disdata.Stride + x - 1] < highThreshold) && -
(dis[(y - 1) * disdata.Stride + x] < highThreshold) && -
(dis[(y - 1) * disdata.Stride + x + 1] < highThreshold) && -
(dis[(y + 1) * disdata.Stride + x - 1] < highThreshold) && -
(dis[(y + 1) * disdata.Stride + x] < highThreshold) && -
(dis[(y + 1) * disdata.Stride + x + 1] < highThreshold)) -
{ -
dis[y * disdata.Stride + x] = 0; -
} -
} -
} -
} -
}</span>
原图:
http://hi.csdn.net/attachment/201108/16/0_1313504706kSDD.gif
灰度图:
http://hi.csdn.net/attachment/201108/16/0_13135081225866.gif
边缘图:
http://hi.csdn.net/attachment/201108/16/0_1313508170V29N.gif