边缘提取算法.doc_第1页
边缘提取算法.doc_第2页
边缘提取算法.doc_第3页
边缘提取算法.doc_第4页
边缘提取算法.doc_第5页
已阅读5页,还剩6页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

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, 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 - width * 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; / 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); BitmapData 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, 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 - 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(); / -1 -2 -1 / 0 0 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 = 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) Matrix3x3 m = new Matrix3x3(); / -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; Bitmap 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.BottomLeft = 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(); / 5 5 5 / -3 0 -3 / -3 -3 -3 m.Init(-3); m.Center = 0; m.TopLeft = m.TopMid = m.TopRight = 5; Bitmap b1 = m.Convolute(Bitmap)b.Clone(); / -3 5 5 / -3 0 5 / -3 -3 -3 m.Init(-3); m.Center = 0; m.TopMid = m.TopRight = m.MidRight = 5; Bitmap b2 = m.Convolute(Bitmap)b.Clone(); / -3 -3 5 / -3 0 5 / -3 -3 5 m.Init(-3); m.Center = 0; m.TopRight = m.MidRight = m.BottomRight = 5; Bitmap b3 = m.Convolute(Bitmap)b.Clone(); / -3 -3 -3 / -3 0 5 / -3 5 5 m.Init(-3); m.Center = 0; m.MidRight = m.BottomRight = m.BottomMid = 5; Bitmap b4 = m.Convolute(Bitmap)b.Clone(); / -3 -3 -3 / -3 0 -3 / 5 5 5 m.Init(-3); m.Center = 0; m.BottomLeft = m.BottomMid = m.BottomRight = 5; Bitmap b5 = m.Convolute(Bitmap)b.Clone(); / -3 -3 -3 / 5 0 -3 / 5 5 -3 m.Init(-3); m.Center = 0; m.MidLeft = m.BottomLeft = m.BottomMid = 5; Bitmap b6 = m.Convolute(Bitmap)b.Clone(); / 5 -3 -3 / 5 0 -3 / 5 -3 -3 m.Init(-3); m.Center = 0; m.TopLeft = m.MidLeft = m.BottomLeft = 5; Bitmap b7 = m.Convolute(Bitmap)b.Clone(); / 5 5 -3 / 5 0 -3 / -3 -3 -3 m.Init(-3); m.Center = 0; m.TopLeft = m.MidLeft = m.TopMid = 5; Bitmap b8 = m.Convolute(Bitmap)b.Clone(); / 梯度运算 Bitmap b9 = Gradient(Gradient(b1, b2), Gradient(b3, b4); Bitmap b10 = Gradient(Gradient(b5, b6), Gradient(b7, b8); b = Gradient(b9, b10); b1.Dispose(); b2.Dispose(); b3.Dispose(); b4.Dispose(); b5.Dispose(); b6.Dispose(); b7.Dispose(); b8.Dispose(); b9.Dispose(); b10.Dispose(); return b; / end of Kirsch / / 按 GaussLaplacian 算子进行边缘检测 / / / public Bitmap GaussLaplacian(Bitmap b) int, kernel = -2, -4, -4, -4, -2, -4, 0, 8, 0, -4, -4, 8, 24, 8, -4, -4, 0, 8, 0, -4, -2, -4, -4, -4, -2 ; MatrixNxN m = new MatrixNxN(); m.Kernel = kernel; return m.Convolute(b); / end of GaussLaplacian / / 按水平边缘检测算子进行边缘检测 / / / public Bitmap EdgeDetectHorizontal(Bitmap b) int, kernel = 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, -1, -1, -1, -1, -1, -1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ; MatrixNxN m = new MatrixNxN(); m.Kernel = kernel; return m.Convolute(b); / end of EdgeDetectHorizontal / / 按垂直边缘检测算子进行边缘检测 / / / public Bitmap EdgeDetectVertical(Bitmap b) int, kernel = 0, 0, -1, 0, 1, 0, 0, 0, 0, -1, 0, 1, 0, 0, 0, 0, -1, 0, 1, 0, 0, 0, 0, -1, 0, 1, 0, 0, 0, 0, -1, 0, 1, 0, 0, 0, 0, -1, 0, 1, 0, 0, 0, 0, -1, 0, 1, 0, 0, ; MatrixNxN m = new MatrixNxN(); m.Kernel = kernel; return m.Convolute(b); / end of EdgeDetectVertical / / 边缘增强 / / 位图流 / 阈值 / public Bitmap EdgeEnhance(Bitmap b, int threshold) int width = b.Width; int height = b.Height; Bitmap dstImage = (Bitmap)b.Clone(); BitmapData srcData = b.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb); BitmapData dstData = dstImage.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb); / 图像实际处理区域 / 不考虑最左 1 列和最右 1 列 / 不考虑最上 1 行和最下 1 行 int rectTop = 1; int rectBottom = height - 1; int rectLeft = 1; int rectRight = width - 1; unsafe byte* src = (byte*)srcData.Scan0; byte* dst = (byte*)dstData.Scan0; int stride = srcData.Stride; int offset = stride - width * BPP; int pixel = 0; int maxPixel = 0; / 指向第 1 行 src += stride; dst += stride; for (int y = rectTop; y rectBottom; y+) / 指向每行

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论