原文: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二值图像细化算法的更多相关文章

  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. Freemarker宏和函数的用法和区别

    1.宏(macro) 写法:<#macro page url page maskSize=5>    <#if (number == pages)> <li>< ...

  2. Oracle中的游标(转)

    Oracle中的游标有两种:显式游标.隐式游标.显示游标是用cursor...is命令定义的游标,它可以对查询语句(select)返回的多条记录进行处理,而隐式游标是在执行插入 (insert).删除 ...

  3. Java8内存模型

    一.JVM内存模型 内存空间(Runtime Data Area)中可以按照是否线程共享分为两块,线程共享的是方法区(Method Area)和堆(Heap),线程独享的是Java虚拟机栈(Java ...

  4. ios 即时通讯开源IM,LeanCloud、融云、环信

    环信官网链接: http://easemob.com/docs/ios/

  5. NOIP模拟 string - ac自动机

    题目大意: 给n个字符串(100位以内),和一个长串s(100000位以内),求n个字符串在s中出现的次数.然后给出m次修改,每次修改s中的一个字符,对于每次修改,输出更新后的答案(修改会保存). 题 ...

  6. Vue中this的绑定

    之前写过一篇文章 ES6与React中this完全解惑 其实Vue也是相同的道理.在Vue的官方文档中提到: 不要在选项属性或回调上使用箭头函数,比如 created: () => consol ...

  7. FPGA综合优化

    1 速度和面积 在全面优化水平将达到速度和面积RTL要利用逻辑拓扑的优势. 供FPGA由于在后端而言缺乏知识,门级优化.普通情况下更高的速度要求更高的并行性以及更大的面积,可是在某些特殊情况下并非这样 ...

  8. 【21.21%】【codeforces round 382D】Taxes

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  9. uinty3d导入错误问题解决

    导入第一被复制到文件unity3d在相应的文件夹的安装文件夹.回归后,unity3d软体.正确的选择"输入". 版权声明:本文博主原创文章.博客,未经同意不得转载.

  10. 【转】mysql的SQL_NO_CACHE(在查询时不使用缓存)和sql_cache用法

    转自:http://www.169it.com/article/5994930453423417575.html 为了测试sql语句的效率,有时候要不用缓存来查询. 使用 SELECT SQL_NO_ ...