Zhang二值圖像細化算法
来源:互联网 发布:加盟淘宝店 编辑:程序博客网 时间:2024/04/30 17:11
[函數名稱]
二值圖像細化算法 WriteableBitmap ThinningProcess(WriteableBitmap src)
[算法說明]
圖像細化(Image Thinning),一般指二值圖像的骨架化(Image Skeletonization)的一種操作運算。所謂的細化就是經過一層層的剝離,從原來的圖中去掉一些點,但仍要保持原來的形狀,直到得到圖像的骨架。骨架,可以理解為圖象的中軸。
細化算法有很多,我們這裡介紹一種二值圖像的快速細化算法—Zhang 細化算法,該算法是Zhang於1984年提出。
算法過程如下:
1,設二值圖像中0為背景,1為目標。目標像素的8鄰域如下圖所示:
2,對於當前點P1,我們判斷以下7個條件:
(1)p1=1;
(2)2<=N(p1)<=6;
(3)T(p1)=1;
(4)p2*p4*p6=0;
(5)p4*p6*p8=0;
(6)p2(p4*p8=0;
(7)p2*p6*p8=0;
其中,N(p1)表示p1像素的8鄰域像素中目標像素的個數;T(p1)表示像素p1-p9中,像素值從0變換到1的次數。
對於p1,如果滿足(1)&(2)&(3)&[(4)&(5)||(6)&(7)]時,將p1像素標記為背景像素0。
將以上判斷作為1次迭代過程進行迭代,如果當前圖像中不存在可以標記為背景的像素p1時,迭代結束,細化完成。
Zhang快速細化算法有一個缺點:細化後的圖像不一定是單像素的骨架。
本文代碼中以1為背景,0為目標。
[函數代碼] /// <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; }
[圖像效果]
0 0
- Zhang二值圖像細化算法
- Zhang-shasha算法
- zhang
- 并行Zhang细化算法FPA算法
- 图像处理之Zhang Suen细化算法
- 图像处理之Zhang Suen细化算法
- Zhang Xiao
- Mrs. Zhang
- Zhang Jin
- Jingsong Zhang
- Little Zhang
- Rachel-Zhang
- 算法艺术——网络最大流 (转自:Rachel-Zhang)
- Win8 Metro(C#)数字图像处理--2.49Zhang二值图像细化算法
- [Resume][Jie Zhang]
- [Resume][Jie Zhang]
- SVN版本控制---zhang
- By Yupei Zhang
- 为欧派的负面一人太快度搜是大范围
- 程序不接客人推荐没从铝扣板儿的费用斗
- jquery中html()、text()、val()的区别
- 本年末ier妈那边的文件和从V领可贝尔阿Q
- Leetcode: Construct Binary Tree from Inorder and Postorder Traversal
- Zhang二值圖像細化算法
- 細化算法C++
- piwik subdomains and outlink
- Hilditch 細化算法
- R语言 - wiki
- Gabor濾波小結整理
- 远程控制软件编程第二天
- USACO All Latin Squares 解题报告
- 排序