原文:Win8Metro(C#)数字图像处理--2.21二值图像腐蚀



[函数名称]

二值图像腐蚀函数CorrosionProcess(WriteableBitmap src)

[算法说明]

二值图像腐蚀操作属于图像形态学的范畴,形态学运算是只针对二值图像进行,并依据数学形态学(Mathermatical
Morphogy)集合论方法发展起来的数字图像处理方法,它主要包括腐蚀,膨胀,开,闭,击中,击不中等。

图像形态学运算,要使用结构元素,所谓结构元素是指具有某种确定形状的基本结构,它的选择一般要求其具有旋转不变性或者镜像不变性,即:结构元素的原点在其几何中心处,周围像素关于原点对称。

在这里我们选取如下的结构元素:

其中,F为二值图像原图,X为结构元素原点所在的二值图像中的连通域。

假设F中目标像素为255(白色),非目标为0(黑色),当结构元素S原点移动到点(x,y)时,如果S中所有点均包含在X中(X中对应在S中所有点的位置均为255),则在腐蚀后的二值图像中,对应于S原点的位置为255(白色),否则为0(黑色)。

用通俗的话来说就是:用结构元素作为模板在原始二值图像种平滑一遍,扫描图像的每一个像素,用结构元素中的每一个元素与其覆盖的二值图像做“与”操作(假设结构元素都为1),如果结果都为1,则二值图像中对应结构元素原点位置的像素值为1,否则为0。

[函数代码]

///<summary>

///
Corrosion process.

///</summary>

///<param
name="src">The source image(It should be the binary image).</param>

///<returns></returns>

publicstaticWriteableBitmap
CorrosionProcess(WriteableBitmap src)////21图像腐蚀运算

{

if
(src !=null)

{

int
w = src.PixelWidth;

int
h = src.PixelHeight;

WriteableBitmap
corrosionImage =newWriteableBitmap(w,
h);

byte[]
temp = src.PixelBuffer.ToArray();

byte[]
tempMask = (byte[])temp.Clone();

for
(int j = 0; j < h; j++)

{

for
(int i = 0; i < w ; i ++)

{

if
(i == 0 || i == w - 1 || j == 0 || j == h - 1)

{

temp[i * 4 + j * w * 4] = (byte)255;

temp[i * 4 + 1 + j * w * 4] = (byte)255;

temp[i * 4 + 2 + j * w * 4] = (byte)255;

}

else

{

if
(tempMask[i * 4 - 4 + j * w * 4] == 255 && tempMask[i * 4 + j * w * 4] == 255 && tempMask[i * 4 + 4 + j * w * 4] == 255

&& tempMask[i * 4 + (j - 1) * w * 4] == 255 && tempMask[i
* 4 + (j + 1) * w * 4] == 255)

{

temp[i * 4 + j * w * 4] = (byte)255;

temp[i * 4 + 1 + j * w * 4] = (byte)255;

temp[i * 4 + 2 + j * w * 4] = (byte)255;

}

else

{

temp[i * 4 + j * w * 4] = 0;

temp[i * 4 + 1 + j * w * 4] = 0;

temp[i * 4 + 2 + j * w * 4] = 0;

}

}

}

}

Stream
sTemp = corrosionImage.PixelBuffer.AsStream();

sTemp.Seek(0,
SeekOrigin.Begin);

sTemp.Write(temp, 0, w * 4 * h);

return
corrosionImage;

}

else

{

returnnull;

}

}

[图像效果]

最新文章

  1. Laravel5.0学习--03 Artisan命令
  2. 连接到CentOS(Linux)服务器ssh、mysql缓慢
  3. codeforces 425C
  4. 手工搭建Openvpn
  5. 3-WebPack
  6. CSS--开篇
  7. UNIX环境高级编程——线程与进程区别
  8. Angular6 用户自定义标签开发
  9. ATL右键文件菜单
  10. 前端持久化--evercookie
  11. ios外部链接或者app唤起自己的app
  12. 分布式架构的基石.简单的 RPC 框架实现(JAVA)
  13. debian中完全删除mysql
  14. 用D3.js画的人物关系demo
  15. Unity Mathf/Math数学运算函数说明全集(Chinar总结)
  16. django 表单常用field
  17. Yii实战中8个必备常用的扩展,模块和widget
  18. ini_set的用法介绍
  19. Java多线程(九)之ReentrantLock与Condition
  20. lua的table的删除操作

热门文章

  1. phpStudy的localhost不能访问怎么解决(相关性)
  2. js把其他类型转化成字符串
  3. 找不到头文件xxxxx.h file not found
  4. Graphics processing architecture employing a unified shader
  5. CocoaPods详解之(一)----使用篇
  6. Java 中StringBuffer与StringBuilder区别(转)及String类的一些基本操作代码
  7. cocos2d-x创建第一个项目
  8. Array.prototype.forEach()&amp;&amp;Array.prototype.map()
  9. java中 8进制 10进制 2进制 16进制 相互转换
  10. 抛砖引玉 【镜像控件】 WPF实现毛玻璃控件不要太简单