边缘提取算法(12页).doc
《边缘提取算法(12页).doc》由会员分享,可在线阅读,更多相关《边缘提取算法(12页).doc(12页珍藏版)》请在得力文库 - 分享文档赚钱的网站上搜索。
1、-边缘提取算法-第 12 页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 Rectan
2、gle(0, 0, width, 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 - w
3、idth * BPP; for (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; /
4、end of Gradient / / 按 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); BitmapDa
5、ta dstData = dstImage.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
6、, D; / C(x-1, 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
7、- D) + (B - C) * (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(); / -
8、1 -2 -1nit(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 = m.C
9、onvolute(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.TopL
10、eft = -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) Matrix3x3 m = new Matrix3x3
11、(); / -1 -1 -1 / 0 0 0 / 1 1 1 m.Init(0); m.TopLeft = m.TopMid = m.TopRight = -1; m.BottomLeft = m.BottomMid = m.BottomRight = 1; Bitmap b1 = m.Convolute(Bitmap)b.Clone(); / -1 0 1 / -1 0 1 / -1 0 1 m.Init(0); m.TopLeft = m.MidLeft = m.BottomLeft = -1; m.TopRight = m.MidRight = m.BottomRight = 1; Bi
12、tmap b2 = m.Convolute(Bitmap)b.Clone(); / -1 -1 0 / -1 0 1 / 0 1 1 m.Init(0); m.TopLeft = m.MidLeft = m.TopMid = -1; m.BottomMid = m.BottomRight = m.MidRight = 1; Bitmap b3 = m.Convolute(Bitmap)b.Clone(); / 0 1 1 / -1 0 1 / -1 -1 0 m.Init(0); m.TopMid = m.TopRight = m.MidRight = 1; m.MidLeft = m.Bot
13、tomLeft = m.BottomMid = -1; 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 Prewitt / / 按 Kirsch 算子进行边缘检测 / / 位图流 / public Bitmap Kirsch(Bitmap b) Matrix3x3 m = new Matrix3x3(
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 边缘 提取 算法 12
限制150内