Win8 Metro(C#)数字图像处理--2.49Zhang二值图像细化算法
2015-04-17 13:34
671 查看
[函数名称]
二值图像细化算法 WriteableBitmap ThinningProcess(WriteableBitmap src)
[算法说明]
图像细化(Image Thinning),一般指二值图像的骨架化(Image Skeletonization)的一种操作运算。
所谓的细化就是经过一层层的剥离,从原来的图中去掉一些点,但仍要保持原来的形状,直到得到图
像的骨架。骨架,可以理解为图象的中轴。
细化算法有很多,我们这里介绍一种二值图像的快速细化算法—Zhang 细化算法,该算法是Zhang于
1984年提出。
算法过程如下:
1,设二值图像中0为背景,1为目标。目标像素的8邻域如下图所示:
[函数名称]
二值图像细化算法 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.60部分彩色保留算法
- Win8 Metro(C#)数字图像处理--2.36角点检测算法
- Win8 Metro(C#)数字图像处理--2.42图像光照效果算法
- Win8 Metro(C#)数字图像处理--2.44图像油画效果算法
- Win8 Metro(C#)数字图像处理--2.40二值图像轮廓提取算法
- Win8 Metro(C#)数字图像处理--2.35图像肤色检测算法
- Win8 Metro(C#)数字图像处理--2.45图像雾化效果算法
- Win8 Metro(C#)数字图像处理--2.51图像统计滤波算法
- Win8 Metro(C#)数字图像处理--2.65形态学轮廓提取算法
- Win8 Metro(C#)数字图像处理--2.66FloodFill算法
- Win8 Metro(C#)数字图像处理--2.64图像高斯滤波算法
- Win8 Metro(C#) 数字图像处理--1 图像打开,保存
- Win8 Metro(C#)数字图像处理--2.61哈哈镜效果
- Win8 Metro(C#)数字图像处理--2.68图像最小值滤波器
- Win8 Metro(C#)数字图像处理--4图像颜色空间描述
- Win8 Metro(C#)数字图像处理--2.69中点滤波器
- Win8 Metro(C#)数字图像处理--2.55OSTU法图像二值化
- Win8 Metro(C#)数字图像处理--2.56简单统计法图像二值化
- Win8 Metro(C#)数字图像处理--2.46图像RGB分量增强效果
- Win8 Metro(C#)数字图像处理--2.70修正后的阿尔法滤波器