Win8Metro(C#)数字图像处理--2.40二值图像轮廓提取
2015-11-11 21:47
676 查看
http://dongtingyueh.blog.163.com/blog/static/4619453201271481335630/
[函数名称]
二值图像轮廓提取 ContourExtraction(WriteableBitmap src)
[算法说明]
二值图像的轮廓提取对于图像识别,图像分割有着重要意义。该算法的核心就是将图像目标的内部点消除。所谓内部点,我们要根据当前像素点的邻域来进行判断,假设邻域窗口为3*3窗口,如果当前像素P(x,y)的八个邻域像素满足如下条件,则该点即内部点:
1,P(x,y)为目标像素,假设目标像素为黑色0,背景像素为白色255,那么P(x,y)=0;
2,P(x,y)的八个邻域像素均为目标像素0;
我们把满足条件的内部点删除,换为背景点255,即可得到图像轮廓。
内部点如下图所示:
[函数代码]
/// <summary>
/// Contour Extraction process.
/// </summary>
/// <param name="src">The source image.</param>
/// <returns></returns>
public static WriteableBitmap ContourExtraction(WriteableBitmap src)
{
if (src != null)
{
int w = src.PixelWidth;
int h = src.PixelHeight;
WriteableBitmap dilationImage = new WriteableBitmap(w, h);
byte[] temp = src.PixelBuffer.ToArray();
byte[] tempMask = (byte[])temp.Clone();
for (int j = 1; j < h-1; j++)
{
for (int i = 4; i < w*4-4; i+=4)
{
if ((tempMask[i + j * w * 4] == 0) && (tempMask[i - 4 + j * w * 4] == 0) && (tempMask[i + 4 + j * w * 4] == 0) && (tempMask[i - 4 + (j - 1) * w * 4] == 0)
&& (tempMask[i - 4 + (j + 1) * w * 4] == 0) && (tempMask[i + (j - 1) * w * 4] == 0) && (tempMask[i + (j + 1) * w * 4] == 0)
&& (tempMask[i + 4 + (j - 1) * w * 4] == 0) && (tempMask[i + 4 + (j + 1) * w * 4] == 0))
{
temp[i + j * w * 4] = (byte)255;
temp[i + 1 + j * w * 4] = (byte)255;
temp[i + 2 + j * w * 4] = (byte)255;
}
}
}
Stream sTemp = dilationImage.PixelBuffer.AsStream();
sTemp.Seek(0, SeekOrigin.Begin);
sTemp.Write(temp, 0, w * 4 * h);
return dilationImage;
}
else
{
return null;
}
}
[图像效果]
[函数名称]
二值图像轮廓提取 ContourExtraction(WriteableBitmap src)
[算法说明]
二值图像的轮廓提取对于图像识别,图像分割有着重要意义。该算法的核心就是将图像目标的内部点消除。所谓内部点,我们要根据当前像素点的邻域来进行判断,假设邻域窗口为3*3窗口,如果当前像素P(x,y)的八个邻域像素满足如下条件,则该点即内部点:
1,P(x,y)为目标像素,假设目标像素为黑色0,背景像素为白色255,那么P(x,y)=0;
2,P(x,y)的八个邻域像素均为目标像素0;
我们把满足条件的内部点删除,换为背景点255,即可得到图像轮廓。
内部点如下图所示:
[函数代码]
/// <summary>
/// Contour Extraction process.
/// </summary>
/// <param name="src">The source image.</param>
/// <returns></returns>
public static WriteableBitmap ContourExtraction(WriteableBitmap src)
{
if (src != null)
{
int w = src.PixelWidth;
int h = src.PixelHeight;
WriteableBitmap dilationImage = new WriteableBitmap(w, h);
byte[] temp = src.PixelBuffer.ToArray();
byte[] tempMask = (byte[])temp.Clone();
for (int j = 1; j < h-1; j++)
{
for (int i = 4; i < w*4-4; i+=4)
{
if ((tempMask[i + j * w * 4] == 0) && (tempMask[i - 4 + j * w * 4] == 0) && (tempMask[i + 4 + j * w * 4] == 0) && (tempMask[i - 4 + (j - 1) * w * 4] == 0)
&& (tempMask[i - 4 + (j + 1) * w * 4] == 0) && (tempMask[i + (j - 1) * w * 4] == 0) && (tempMask[i + (j + 1) * w * 4] == 0)
&& (tempMask[i + 4 + (j - 1) * w * 4] == 0) && (tempMask[i + 4 + (j + 1) * w * 4] == 0))
{
temp[i + j * w * 4] = (byte)255;
temp[i + 1 + j * w * 4] = (byte)255;
temp[i + 2 + j * w * 4] = (byte)255;
}
}
}
Stream sTemp = dilationImage.PixelBuffer.AsStream();
sTemp.Seek(0, SeekOrigin.Begin);
sTemp.Write(temp, 0, w * 4 * h);
return dilationImage;
}
else
{
return null;
}
}
[图像效果]
相关文章推荐
- 利用richtextbox做的c#简易编辑软件,
- (原创)c#学习笔记10--定义类成员01--成员定义06--自动属性
- C#GDI+图像处理
- C# 内存法图像处理
- (C#)xml的简单理解创建和读取
- 【C#】RGB,CMYK,HSB各种颜色表示的转换(转)
- C# Socket简单例子(服务器与客户端通信)
- 教你快速在c#中调用C++代码(函数)
- Visual Studio 2010 安装 MSDN Library
- 【C#】正则表达式
- (原创)c#学习笔记10--定义类成员01--成员定义03--定义属性
- C#创建COM组件
- (原创)c#学习笔记10--定义类成员01--成员定义02--建立方法
- (原创)c#学习笔记10--定义类成员01--成员定义01--定义字段
- C#-如何ping IP
- C#操作符??和?:
- C# 文字转换最简单的方法
- c#中MD5的加密解密
- c#在Repeater中的button如何触发onclick事件
- C#中的Action<T>、Func<T>委托和Lambda表达式