原文: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. js进阶 11-3 jquery中css属性如何操作

    js进阶 11-3  jquery中css属性如何操作 一.总结 一句话总结:通过css()方法 1.attr和css是有交叉的,比如width,两者中都可以设置,那么他们的区别是什么? 其实通俗一点 ...

  2. Android 自定义View——自定义点击事件

    每个人手机上都有通讯录,这是毫无疑问的,我们通讯录上有一个控件,在通讯录的最左边有一列从”#”到”Z”的字母,我们通过滑动或点击指定的字母来确定联系人的位置,进而找到联系人.我们这一节就通过开发这个控 ...

  3. BigDecimal 舍入模式(Rounding mode)介绍

    BigDecimal 舍入模式(Rounding mode)介绍 什么样的经历,才能领悟成为架构师? >>>   1 RoundingMode介绍 package java.math ...

  4. 囚徒困境、价格大战与 iPhone 的价格

    静态/动态,完全/不完全: 完全信息静态博弈: 不完全信息静态博弈: 完全信息动态博弈: 不完全信息动态博弈: 囚徒困境实际上反映了一个深刻的哲学问题:个人利益与集体利益的矛盾.个人为了自己利益的最大 ...

  5. Lucene + Pinyin4J 提供首字母搜索(——)

    遇到一个集团需求,要求在地址查询时候提供拼音搜索,第一反应应该不难,不过实现过程中却一波三折. 1.第一步是讲字段首字母进行索引,具体可以使用Pinyin4j提供的方法完成. 2.原来系统用的luce ...

  6. 【u219】最长链

    Time Limit: 1 second Memory Limit: 128 MB [问题描述] 现给出一棵N个结点二叉树,问这棵二叉树中最长链的长度为多少,保证了1号结点为二叉树的根. [提示] 关 ...

  7. svn服务配置

    1关闭所有svn服务 nie-xiao-bo-mac-pro:svnproject mac$ killall -9 svnserve 2.开启某文件路径svn服务 nie-xiao-bo-mac-pr ...

  8. 【17.00%】【codeforces 621D】Rat Kwesh and Cheese

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

  9. Spring处理跨域请求

    [nio-8080-exec-8] o.s.web.cors.DefaultCorsProcessor        : Skip CORS processing: request is from s ...

  10. ASP.Net Core 2.2使用SQLite数据库unable to open database file

    原文:ASP.Net Core 2.2使用SQLite数据库unable to open database file 最近把项目更新到了ASP.Net Core 2.2,发布之后发现在IIS下使用SQ ...