《边缘提取算法》由会员分享,可在线阅读,更多相关《边缘提取算法(11页珍藏版)》请在金锄头文库上搜索。
1、public class EdgeDetect : ImageInfo /* * * Roberts, Sobel, Prewitt, Kirsch, GaussLaplacian * 水平检测、垂直检测、边缘增强、边缘均衡化 * */ / / 对两幅图像进行梯度运算 / / 位图 1 / 位图 2 / private Bitmap Gradient(Bitmap b1, Bitmap b2) int width = b1.Width; int height = b1.Height; BitmapData data1 = b1.LockBits(new Rectangle(0, 0, widt
2、h, height), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb); BitmapData data2 = b2.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb); unsafe byte* p1 = (byte*)data1.Scan0; byte* p2 = (byte*)data2.Scan0; int offset = data1.Stride - width * BPP; fo
3、r (int y = 0; y height; y+) for (int x = 0; x width; x+) for (int i = 0; i 255 ? 255 : power); / i p1 += BPP; p2 += BPP; / x p1 += offset; p2 += offset; / y b1.UnlockBits(data1); b2.UnlockBits(data2); Bitmap dstImage = (Bitmap)b1.Clone(); b1.Dispose(); b2.Dispose(); return dstImage; / end of Gradien
4、t / / 按 Roberts 算子进行边缘检测 / / 位图流 / public Bitmap Roberts(Bitmap b) int width = b.Width; int height = b.Height; Bitmap dstImage = new Bitmap(width, height); BitmapData srcData = b.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb); BitmapData dstData = d
5、stImage.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb); int stride = srcData.Stride; int offset = stride - width * BPP; unsafe byte* src = (byte*)srcData.Scan0; byte* dst = (byte*)dstData.Scan0; int A, B; / A(x-1, y-1) B(x, y-1) int C, D; / C(x-1,
6、y) D(x, y) / 指向第一行 src += stride; dst += stride; / 不处理最上边和最左边 for (int y = 1; y height; y+) / 指向每行第一列 src += BPP; dst += BPP; for (int x = 1; x width; x+) for (int i = 0; i 3; i+) A = srci - stride - BPP; B = srci - stride; C = srci - BPP; D = srci; dsti = (byte)(Math.Sqrt(A - D) * (A - D) + (B - C)
7、 * (B - C); / i dst3 = src3; src += BPP; dst += BPP; / x src += offset; dst += offset; / y b.UnlockBits(srcData); dstImage.UnlockBits(dstData); b.Dispose(); return dstImage; / end of Roberts / / 按 Sobel 算子进行边缘检测 / / 位图流 / public Bitmap Sobel(Bitmap b) Matrix3x3 m = new Matrix3x3(); / -1 -2 -1 / 0 0
8、0 / 1 2 1 m.Init(0); m.TopLeft = m.TopRight = -1; m.BottomLeft = m.BottomRight = 1; m.TopMid = -2; m.BottomMid = 2; Bitmap b1 = m.Convolute(Bitmap)b.Clone(); / -1 0 1 / -2 0 2 / -1 0 1 m.Init(0); m.TopLeft = m.BottomLeft = -1; m.TopRight = m.BottomRight = 1; m.MidLeft = -2; m.MidRight = 2; Bitmap b2
9、 = m.Convolute(Bitmap)b.Clone(); / 0 1 2 / -1 0 1 / -2 -1 0 m.Init(0); m.TopMid = m.MidRight = 1; m.MidLeft = m.BottomMid = -1; m.TopRight = 2; m.BottomLeft = -2; Bitmap b3 = m.Convolute(Bitmap)b.Clone(); / -2 -1 0 / -1 0 1 / 0 1 2 m.Init(0); m.TopMid = m.MidLeft = -1; m.MidRight = m.BottomMid = 1; m.TopLeft = -2; m.BottomRight = 2; Bitmap b4 = m.Convolute(Bitmap)b.Clone(); / 梯度运算 b = Gradient(Gradient(b1, b2), Gradient(b3, b4); b1.Dispose(); b2.Dispose(); b3.Dispose(); b4.Dispose(); return b; / end of Sobel / / 按 Prewitt 算子进行边缘检测 / / 位图流 / public Bitmap Prewitt(Bitmap b)