原文:Win8 Metro(C#)数字图像处理--2.49Zhang二值图像细化算法



[函数名称]

  二值图像细化算法      WriteableBitmap ThinningProcess(WriteableBitmap src)

[算法说明]

  图像细化(Image Thinning),一般指二值图像的骨架化(Image Skeletonization)的一种操作运算。

所谓的细化就是经过一层层的剥离,从原来的图中去掉一些点,但仍要保持原来的形状,直到得到图

像的骨架。骨架,可以理解为图象的中轴。

  细化算法有很多,我们这里介绍一种二值图像的快速细化算法—Zhang 细化算法,该算法是Zhang于

1984年提出。

  算法过程如下:

  1,设二值图像中0为背景,1为目标。目标像素的8邻域如下图所示:

  1. /// <summary>
  2. /// Zhang's fast thinning process for binary image.
  3. /// </summary>
  4. /// <param name="src">The source image.</param>
  5. /// <returns></returns>
  6. public static WriteableBitmap ThinningProcess(WriteableBitmap src)////二值图像细化(Zhang快速细化算法)
  7. {
  8. if (src != null)
  9. {
  10. int w = src.PixelWidth;
  11. int h = src.PixelHeight;
  12. WriteableBitmap srcImage = new WriteableBitmap(w, h);
  13. byte[] temp = src.PixelBuffer.ToArray();
  14. byte[] tempMask = (byte[])temp.Clone();
  15. int[,] srcBytes = new int[w, h];
  16. for (int j = 0; j < h; j++)
  17. {
  18. for (int i = 0; i < w ; i++)
  19. {
  20. srcBytes[i, j] = (tempMask[i * 4 + j * w * 4] * 0.114 + tempMask[i * 4 + 1 + j * w * 4] * 0.587 + tempMask[i * 4 + 2 + j * w * 4] * 0.299 < 128 ? 0 : 1);
  21. }
  22. }
  23. Thinning(ref srcBytes, w, h);
  24. for (int j = 0; j < h; j++)
  25. {
  26. for (int i = 0; i < w; i++)
  27. {
  28. temp[i * 4 + j * w * 4] = temp[i * 4 + 1 + j * w * 4] = temp[i * 4 + 2 + j * w * 4] = (byte)(srcBytes[i, j] * 255);
  29. }
  30. }
  31. Stream sTemp = srcImage.PixelBuffer.AsStream();
  32. sTemp.Seek(0, SeekOrigin.Begin);
  33. sTemp.Write(temp, 0, w * 4 * h);
  34. return srcImage;
  35. }
  36. else
  37. {
  38. return null;
  39. }
  40. }
  41. private static void Thinning(ref int[,] srcBytes,int w,int h)
  42. {
  43. int[] srcTemp;
  44. int countNumber;
  45. do
  46. {
  47. countNumber = 0;
  48. for (int y = 1; y < h - 1; y++)
  49. {
  50. for (int x = 1; x < w - 1; x++)
  51. {
  52. srcTemp = new int[9] { srcBytes[x, y], srcBytes[x - 1, y - 1], srcBytes[x, y - 1], srcBytes[x + 1, y - 1], srcBytes[x + 1, y], srcBytes[x + 1, y + 1], srcBytes[x, y + 1], srcBytes[x - 1, y + 1], srcBytes[x - 1, y] };
  53. if (srcBytes[x, y] != 1)
  54. {
  55. if (CountN(srcTemp) >= 2 && CountN(srcTemp) <= 6)
  56. {
  57. if (CountT(srcTemp) == 1)
  58. {
  59. if (srcBytes[x, y - 1] * srcBytes[x + 1, y] * srcBytes[x, y + 1] == 0)
  60. {
  61. if (srcBytes[x - 1, y] * srcBytes[x + 1, y] * srcBytes[x, y + 1] == 0)
  62. {
  63. srcBytes[x, y] = (byte)1;
  64. countNumber++;
  65. }
  66. }
  67. else
  68. {
  69. if (srcBytes[x, y - 1] * srcBytes[x + 1, y] * srcBytes[x - 1, y] == 0)
  70. {
  71. if (srcBytes[x, y - 1] * srcBytes[x, y + 1] * srcBytes[x - 1, y] == 0)
  72. {
  73. srcBytes[x, y] = (byte)1;
  74. countNumber++;
  75. }
  76. }
  77. }
  78. }
  79. }
  80. }
  81. }
  82. }
  83. } while (countNumber != 0);
  84. }
  85. private static int CountN(params int[] src)
  86. {
  87. int count = 0;
  88. for (int i = 0; i < src.Length; i++)
  89. {
  90. if (src[i] == 0)
  91. {
  92. count++;
  93. }
  94. }
  95. return count;
  96. }
  97. private static int CountT(params int[] src)
  98. {
  99. int count = 0;
  100. for (int i = 1; i < src.Length; i++)
  101. {
  102. if (src[i] == 1 && src[i - 1] == 0)
  103. {
  104. count++;
  105. }
  106. }
  107. if (src[src.Length - 1] == 0 && src[0] == 1)
  108. {
  109. count++;
  110. }
  111. return count;
  112. }


Win8 Metro(C#)数字图像处理--2.49Zhang二值图像细化算法的更多相关文章

  1. Win8 Metro(C#)数字图像处理--2.45图像雾化效果算法

    原文:Win8 Metro(C#)数字图像处理--2.45图像雾化效果算法 [函数名称]   图像雾化         AtomizationProcess(WriteableBitmap src,i ...

  2. Win8 Metro(C#)数字图像处理--2.39二值图像投影

    原文:Win8 Metro(C#)数字图像处理--2.39二值图像投影  [函数名称]   二值图像投影         ImageProjection(WriteableBitmap src) ...

  3. Win8 Metro(C#)数字图像处理--2.40二值图像轮廓提取算法

    原文:Win8 Metro(C#)数字图像处理--2.40二值图像轮廓提取算法  [函数名称]   二值图像轮廓提取         ContourExtraction(WriteableBitm ...

  4. Win8 Metro(C#)数字图像处理--2.41彩色图像密度分割算法

    原文:Win8 Metro(C#)数字图像处理--2.41彩色图像密度分割算法  [函数名称]   彩色图像密度分割函数      DensitySegmentProcess(WriteableB ...

  5. Win8 Metro(C#)数字图像处理--2.42图像光照效果算法

    原文:Win8 Metro(C#)数字图像处理--2.42图像光照效果算法  [函数名称] 图像光照效果  SunlightProcess(WriteableBitmap src,int X,in ...

  6. Win8 Metro(C#)数字图像处理--2.75灰度图像的形态学算法

    原文:Win8 Metro(C#)数字图像处理--2.75灰度图像的形态学算法 前面章节中介绍了二值图像的形态学算法,这里讲一下灰度图的形态学算法,主要是公式,代码略. 1,膨胀算法 2,腐蚀算法 3 ...

  7. Win8 Metro(C#)数字图像处理--2.36角点检测算法

    原文:Win8 Metro(C#)数字图像处理--2.36角点检测算法  [函数名称] Harris角点检测函数    HarrisDetect(WriteableBitmap src, int  ...

  8. Win8 Metro(C#)数字图像处理--4图像颜色空间描述

    原文:Win8 Metro(C#)数字图像处理--4图像颜色空间描述  图像颜色空间是图像颜色集合的数学表示,本小节将针对几种常见颜色空间做个简单介绍. /// <summary> / ...

  9. Win8 Metro(C#)数字图像处理--3.2图像方差计算

    原文:Win8 Metro(C#)数字图像处理--3.2图像方差计算 /// <summary> /// /// </summary>Variance computing. / ...

随机推荐

  1. [tmux] Create collections of panes using tmux windows

    In tmux, a window is a collection of panes. Creating multiple windows is a great way to organize you ...

  2. MVC 设置项目默认起始页和多级目录的路由配置

    我们新建一个MVC的项目 默认的路由是这样的,但是由于一些需求,我们需要对Controllers按照一些规则分类. 比如说我们在Controllers下面建了一个School的文件夹,然后建了一个St ...

  3. ets学习

    http://diaocow.iteye.com/blog/1768647 http://www.cnblogs.com/me-sa/archive/2011/08/11/erlang0007.htm ...

  4. MapReduce 经典案例手机流量排序的分析

    在进行流量排序之前,先要明白排序是发生在map阶段,排序之后(排序结束后map阶段才会显示100%完成)才会到reduce阶段(事实上reduce也会排序),.此外排序之前要已经完成了手机流量的统计工 ...

  5. 静态资源命名的注意点以及document.write与innerHTML的区别

    今天拿出了去年刚开始学前端的那本书来看,发现好多新东西. 使用下划线和混合大小写不利于SEO! document.write与innerHTML的区别 这个问题大概是初学前端的人才会问的吧,业务代码中 ...

  6. STL序列容器之vector

    一,vector容器简介 1.vector容器的原理 vector是将元素置于一个动态数组中加以管理的容器. 2.vector容器的特点 vector容器可以随机存取元素,支持索引存取(即用数组下标的 ...

  7. Mysql主从复制,读写分离(mysql-proxy)

    Mysql主从复制,读写分离(mysql-proxy) 下面介绍MySQL主从复制,读写分离,双主结构完整构建过程,不涉及过多理论,只有实验和配置的过程. Mysql主从复制(转载请注明出处,博文地址 ...

  8. 简明Python3教程 3.介绍

    介绍 Python是少有的几种既强大又简单的编程语言.你将惊喜地发现通过使用Python即可轻松专注于解决问题而非和你所用的语言格式与结构. 下面是Python的官方介绍: Python is an ...

  9. Linux性能测试 free命令

    命 令: free功能说明:显示内存状态.语 法: free [-bkmotV][-s <间隔秒数>]补充说明:free指令会显示内存的使用情况,包括实体内存,虚拟的交换文件内存,共享内存 ...

  10. Metropolis 采样与蒙特卡洛算法

    Metropolis 算法又叫 Metropolis 抽样,是模拟退火算法的基础,在早期的科学计算中蒙特卡洛方法(Monte Carlo)是对大量原子在给定温度下的平衡态的随机模拟,当蒙特卡洛算法计算 ...