边缘提取算法

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

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 < 3; i++)

{

int power = (int)Math.Sqrt((p1[i] * p1[i] + p2[i] * p2[i]));

p1[i] = (byte)(power > 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 = src[i - stride - BPP];

B = src[i - stride];

C = src[i - BPP];

D = src[i];

dst[i] = (byte)(Math.Sqrt((A - D) * (A - D) + (B - C) * (B - C)));

} // i

dst[3] = src[3];

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)

相关文档
最新文档