文档介绍:边缘检测类(包括Roberts, Sobel, Prewitt, Kirsch等算子的边缘检测算法) 收藏
public class EdgeDetect : ImageInfo
{
/************************************************************
*
* Roberts, Sobel, Prewitt, Kirsch, GaussLaplacian
* 水平检测、垂直检测、边缘增强、边缘均衡化
*
************************************************************/
/// <summary>
/// 对两幅图像进行梯度运算
/// </summary>
/// <param name="b1">位图 1</param>
/// <param name="b2">位图 2</param>
/// <returns></returns>
private Bitmap Gradient(Bitmap b1, Bitmap b2)
{
int width = ;
int height = ;
BitmapData data1 = (new Rectangle(0, 0, width, height),
, );
BitmapData data2 = (new Rectangle(0, 0, width, height),
, );
unsafe
{
byte* p1 = (byte*);
byte* p2 = (byte*);
int offset = - 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)((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
}
(data1);
(data2);
Bitmap dstImage = (Bitmap)();
();
();
return dstImage;
} // end of Gradient
/// <summary>
/// 按 Roberts 算子进行边缘检测
/// </summary>
/// <param name="b">位图流</param>
/// <returns></returns>
public Bitmap Roberts(Bitmap b)
{
int width = ;
int height = ;
Bitmap dstImage = new Bitmap(width, height);
BitmapData srcData = (new Rectangle(0, 0, width, height),
, );
BitmapData dstData = (new Rectangle(0, 0, width, height),
, );
int stride = ;
int offset = stride - width * BPP;
unsafe