Win8 Metro(C#)数字图像处理--2.49Zhang二值图像细化算法
原文:Win8 Metro(C#)数字图像处理--2.49Zhang二值图像细化算法
[函数名称]
二值图像细化算法 WriteableBitmap ThinningProcess(WriteableBitmap src)
[算法说明]
图像细化(Image Thinning),一般指二值图像的骨架化(Image Skeletonization)的一种操作运算。
所谓的细化就是经过一层层的剥离,从原来的图中去掉一些点,但仍要保持原来的形状,直到得到图
像的骨架。骨架,可以理解为图象的中轴。
细化算法有很多,我们这里介绍一种二值图像的快速细化算法—Zhang 细化算法,该算法是Zhang于
1984年提出。
算法过程如下:
1,设二值图像中0为背景,1为目标。目标像素的8邻域如下图所示:
- /// <summary>
- /// Zhang's fast thinning process for binary image.
- /// </summary>
- /// <param name="src">The source image.</param>
- /// <returns></returns>
- public static WriteableBitmap ThinningProcess(WriteableBitmap src)////二值图像细化(Zhang快速细化算法)
- {
- if (src != null)
- {
- int w = src.PixelWidth;
- int h = src.PixelHeight;
- WriteableBitmap srcImage = new WriteableBitmap(w, h);
- byte[] temp = src.PixelBuffer.ToArray();
- byte[] tempMask = (byte[])temp.Clone();
- int[,] srcBytes = new int[w, h];
- for (int j = 0; j < h; j++)
- {
- for (int i = 0; i < w ; i++)
- {
- 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);
- }
- }
- Thinning(ref srcBytes, w, h);
- for (int j = 0; j < h; j++)
- {
- for (int i = 0; i < w; i++)
- {
- 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);
- }
- }
- Stream sTemp = srcImage.PixelBuffer.AsStream();
- sTemp.Seek(0, SeekOrigin.Begin);
- sTemp.Write(temp, 0, w * 4 * h);
- return srcImage;
- }
- else
- {
- return null;
- }
- }
- private static void Thinning(ref int[,] srcBytes,int w,int h)
- {
- int[] srcTemp;
- int countNumber;
- do
- {
- countNumber = 0;
- for (int y = 1; y < h - 1; y++)
- {
- for (int x = 1; x < w - 1; x++)
- {
- 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] };
- if (srcBytes[x, y] != 1)
- {
- if (CountN(srcTemp) >= 2 && CountN(srcTemp) <= 6)
- {
- if (CountT(srcTemp) == 1)
- {
- if (srcBytes[x, y - 1] * srcBytes[x + 1, y] * srcBytes[x, y + 1] == 0)
- {
- if (srcBytes[x - 1, y] * srcBytes[x + 1, y] * srcBytes[x, y + 1] == 0)
- {
- srcBytes[x, y] = (byte)1;
- countNumber++;
- }
- }
- else
- {
- if (srcBytes[x, y - 1] * srcBytes[x + 1, y] * srcBytes[x - 1, y] == 0)
- {
- if (srcBytes[x, y - 1] * srcBytes[x, y + 1] * srcBytes[x - 1, y] == 0)
- {
- srcBytes[x, y] = (byte)1;
- countNumber++;
- }
- }
- }
- }
- }
- }
- }
- }
- } while (countNumber != 0);
- }
- private static int CountN(params int[] src)
- {
- int count = 0;
- for (int i = 0; i < src.Length; i++)
- {
- if (src[i] == 0)
- {
- count++;
- }
- }
- return count;
- }
- private static int CountT(params int[] src)
- {
- int count = 0;
- for (int i = 1; i < src.Length; i++)
- {
- if (src[i] == 1 && src[i - 1] == 0)
- {
- count++;
- }
- }
- if (src[src.Length - 1] == 0 && src[0] == 1)
- {
- count++;
- }
- return count;
- }
Win8 Metro(C#)数字图像处理--2.49Zhang二值图像细化算法的更多相关文章
- Win8 Metro(C#)数字图像处理--2.45图像雾化效果算法
原文:Win8 Metro(C#)数字图像处理--2.45图像雾化效果算法 [函数名称] 图像雾化 AtomizationProcess(WriteableBitmap src,i ...
- Win8 Metro(C#)数字图像处理--2.39二值图像投影
原文:Win8 Metro(C#)数字图像处理--2.39二值图像投影 [函数名称] 二值图像投影 ImageProjection(WriteableBitmap src) ...
- Win8 Metro(C#)数字图像处理--2.40二值图像轮廓提取算法
原文:Win8 Metro(C#)数字图像处理--2.40二值图像轮廓提取算法 [函数名称] 二值图像轮廓提取 ContourExtraction(WriteableBitm ...
- Win8 Metro(C#)数字图像处理--2.41彩色图像密度分割算法
原文:Win8 Metro(C#)数字图像处理--2.41彩色图像密度分割算法 [函数名称] 彩色图像密度分割函数 DensitySegmentProcess(WriteableB ...
- Win8 Metro(C#)数字图像处理--2.42图像光照效果算法
原文:Win8 Metro(C#)数字图像处理--2.42图像光照效果算法 [函数名称] 图像光照效果 SunlightProcess(WriteableBitmap src,int X,in ...
- Win8 Metro(C#)数字图像处理--2.75灰度图像的形态学算法
原文:Win8 Metro(C#)数字图像处理--2.75灰度图像的形态学算法 前面章节中介绍了二值图像的形态学算法,这里讲一下灰度图的形态学算法,主要是公式,代码略. 1,膨胀算法 2,腐蚀算法 3 ...
- Win8 Metro(C#)数字图像处理--2.36角点检测算法
原文:Win8 Metro(C#)数字图像处理--2.36角点检测算法 [函数名称] Harris角点检测函数 HarrisDetect(WriteableBitmap src, int ...
- Win8 Metro(C#)数字图像处理--4图像颜色空间描述
原文:Win8 Metro(C#)数字图像处理--4图像颜色空间描述 图像颜色空间是图像颜色集合的数学表示,本小节将针对几种常见颜色空间做个简单介绍. /// <summary> / ...
- Win8 Metro(C#)数字图像处理--3.2图像方差计算
原文:Win8 Metro(C#)数字图像处理--3.2图像方差计算 /// <summary> /// /// </summary>Variance computing. / ...
随机推荐
- [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 ...
- MVC 设置项目默认起始页和多级目录的路由配置
我们新建一个MVC的项目 默认的路由是这样的,但是由于一些需求,我们需要对Controllers按照一些规则分类. 比如说我们在Controllers下面建了一个School的文件夹,然后建了一个St ...
- ets学习
http://diaocow.iteye.com/blog/1768647 http://www.cnblogs.com/me-sa/archive/2011/08/11/erlang0007.htm ...
- MapReduce 经典案例手机流量排序的分析
在进行流量排序之前,先要明白排序是发生在map阶段,排序之后(排序结束后map阶段才会显示100%完成)才会到reduce阶段(事实上reduce也会排序),.此外排序之前要已经完成了手机流量的统计工 ...
- 静态资源命名的注意点以及document.write与innerHTML的区别
今天拿出了去年刚开始学前端的那本书来看,发现好多新东西. 使用下划线和混合大小写不利于SEO! document.write与innerHTML的区别 这个问题大概是初学前端的人才会问的吧,业务代码中 ...
- STL序列容器之vector
一,vector容器简介 1.vector容器的原理 vector是将元素置于一个动态数组中加以管理的容器. 2.vector容器的特点 vector容器可以随机存取元素,支持索引存取(即用数组下标的 ...
- Mysql主从复制,读写分离(mysql-proxy)
Mysql主从复制,读写分离(mysql-proxy) 下面介绍MySQL主从复制,读写分离,双主结构完整构建过程,不涉及过多理论,只有实验和配置的过程. Mysql主从复制(转载请注明出处,博文地址 ...
- 简明Python3教程 3.介绍
介绍 Python是少有的几种既强大又简单的编程语言.你将惊喜地发现通过使用Python即可轻松专注于解决问题而非和你所用的语言格式与结构. 下面是Python的官方介绍: Python is an ...
- Linux性能测试 free命令
命 令: free功能说明:显示内存状态.语 法: free [-bkmotV][-s <间隔秒数>]补充说明:free指令会显示内存的使用情况,包括实体内存,虚拟的交换文件内存,共享内存 ...
- Metropolis 采样与蒙特卡洛算法
Metropolis 算法又叫 Metropolis 抽样,是模拟退火算法的基础,在早期的科学计算中蒙特卡洛方法(Monte Carlo)是对大量原子在给定温度下的平衡态的随机模拟,当蒙特卡洛算法计算 ...