2015年7月4日
《一种改进的并行细化算法》的CSharp实现
《一种改进的并行细化算法》是中科院自动化所文字识别工程中心的一篇文献,它对细化算法有如下描述:
虽然没有文献对细化需求做出明确的定义,但是一个好的细化算法应该保存下面的基本属性:
1)骨架应该是连通的且为一像素宽,接近图像的理想中轴线并保存原图像的形状。
2) 好的细化算法应该对噪声不敏感,尽可能的少产生假肢现象。
3) 好的细化算法应该具有较快的速度,较快的速度可以满足实时性的要求。
怀着对原作者和其所在单位的敬畏与崇拜之情,我尝试用C#来实现了该文献描述的部分算法,该文献的下载地址:链接: http://pan.baidu.com/s/1hqph4u4 密码: mys6。
该文献将改进的并行细化算法分为以下几个部分来阐述:(a)基于8邻域的基本消除规则。 (b) 两像素宽的处理方式。(c) 补偿丢失的信息。我仅仅实现了(a)与(b),在这里分享实现代码,希望可以抛砖引玉。
下图将单独(a),(a)与(b)结合的实验结果与zhang-suen算法进行了比较,两种算法各有千秋,但从识别度来看,zhang-suen算法似乎更胜一筹。
《一种改进的并行细化算法》部分算法的C#实现代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 |
/* -------------------------------------------------------- * 作者:livezingy * * 博客:http://www.livezingy.com * * 开发环境: * Visual Studio V2012 * .NET Framework 4.5 * * * 版本历史: * V1.0 2015年7月4日 * 高效的并行细化算法 * * --------------------------------------------------------- */ using System; using System.Collections.Generic; using System.Collections; using System.Linq; using System.Text; using System.Drawing; using System.Drawing.Imaging; using System.Runtime.InteropServices; using ImageSegmention; namespace parallelThining { public class ThinFunction { private static readonly int[] elimTable = new int[] { 3, 6, 7, 11, 12, 14, 15, 24, 26, 27, 28, 30, 31, 48, 56, 60, 62, 63, 96, 112, 120, 124, 126, 127, 129, 131, 134, 135, 143, 159, 192, 193, 194, 195, 199, 207, 223, 224, 225, 227, 230, 231, 240, 241, 243, 247, 248, 249, 252, 253 }; /* 5 6 7 A 0 B 4 p 0 ---> 1 p 1 3 2 1 1 1 1 10 9 8 D 0 C */ private static readonly int[] template431 = new int[] { 1984,1985,1988,1989,1992,1993,1996,1997,2016,2017,2020,2021,2024,2025,2028,2029 /* // 1,1,1,1,1,A,0,B,C,0,D A,B,C,D//任意像素 {1,1,1,1,1,0,0,0,0,0,0},//0 0 0 0 1984 {1,1,1,1,1,0,0,0,0,0,1},//0 0 0 1 1985 {1,1,1,1,1,0,0,0,1,0,0},//0 0 1 0 1988 {1,1,1,1,1,0,0,0,1,0,1},//0 0 1 1 1989 {1,1,1,1,1,0,0,1,0,0,0},//0 1 0 0 1992 {1,1,1,1,1,0,0,1,0,0,1},//0 1 0 1 1993 {1,1,1,1,1,0,0,1,1,0,0},//0 1 1 0 1996 {1,1,1,1,1,0,0,1,1,0,1},//0 1 1 1 1997 {1,1,1,1,1,1,0,0,0,0,0},//1 0 0 0 2016 {1,1,1,1,1,1,0,0,0,0,1},//1 0 0 1 2017 {1,1,1,1,1,1,0,0,1,0,0},//1 0 1 0 2020 {1,1,1,1,1,1,0,0,1,0,1},//1 0 1 1 2021 {1,1,1,1,1,1,0,1,0,0,0},//1 1 0 0 2024 {1,1,1,1,1,1,0,1,0,0,1},//1 1 0 1 2025 {1,1,1,1,1,1,0,1,1,0,0},//1 1 1 0 2028 {1,1,1,1,1,1,0,1,1,0,1} //1 1 1 1 2029 */ }; /* 10 9 8 B 0 0 5 6 7 ---> 1 1 0 4 p 0 0 p 0 3 2 1 0 0 A */ private static readonly int[] template432 = new int[] { 48,49,560,561 /* // 0,A,0,0,0,1,1,0,0,0,B A,B为任意像素 {0,0,0,0,0,1,1,0,0,0,0},//0 0 48 {0,0,0,0,0,1,1,0,0,0,1},//0 1 49 {0,1,0,0,0,1,1,0,0,0,0},//1 0 560 {0,1,0,0,0,1,1,0,0,0,1}//1 1 561 */ }; /* 5 6 7 A 0 0 4 p 0 ---> 0 p 0 3 2 1 0 1 1 10 9 8 0 0 B */ private static readonly int[] template433 = new int[] { 768,772,800,804 /* // 0,1,1,0,0,A,0,0,B,0,0 A,B为任意像素 {0,1,1,0,0,0,0,0,0,0,0},//0 0 768 {0,1,1,0,0,0,0,0,1,0,0},//0 1 772 {0,1,1,0,0,1,0,0,0,0,0},//1 0 800 {0,1,1,0,0,1,0,0,1,0,0} //1 1 804 */ }; /* 5 6 7 8 0 0 0 0 4 p 0 9 ---> 0 p 1 0 3 2 1 10 0 1 1 0 14 13 12 11 0 0 0 0 */ private static readonly int[] template44 = new int[] { 28672 // 1,1,1,0,0,0,0,0,0,0,0,0,0,0,0 //28672 }; /* 5 6 7 8 A 0 0 0 4 p 0 9 ---> 0 p 1 0 3 2 1 10 0 0 1 B */ private static readonly int[] template341 = new int[] { 1536,1537,1568,1569 /* //1,1,0,0,0,A,0,0,0,0,B A,B为任意像素 {1,1,0,0,0,0,0,0,0,0,0},//0 0 1536 {1,1,0,0,0,0,0,0,0,0,1},//0 1 1537 {1,1,0,0,0,1,0,0,0,0,0},//1 0 1568 {1,1,0,0,0,1,0,0,0,0,1} //1 1 1569 */ }; /* 5 6 7 8 B 1 1 C 4 p 0 9 ---> 0 p 1 0 3 2 1 10 A 1 1 D */ private static readonly int[] template342 = new int[] { 1816,1817,1820,1821,1848,1849,1852,1853,1944,1945,1948,1949,1976,1977,1980,1981 /* // 1,1,1,A,0,B,1,1,C,0,D //A B C D {1,1,1,0,0,0,1,1,0,0,0},//0 0 0 0 1816 {1,1,1,0,0,0,1,1,0,0,1},//0 0 0 1 1817 {1,1,1,0,0,0,1,1,1,0,0},//0 0 1 0 1820 {1,1,1,0,0,0,1,1,1,0,1},//0 0 1 1 1821 {1,1,1,0,0,1,1,1,0,0,0},//0 1 0 0 1848 {1,1,1,0,0,1,1,1,0,0,1},//0 1 0 1 1849 {1,1,1,0,0,1,1,1,1,0,0},//0 1 1 0 1852 {1,1,1,0,0,1,1,1,1,0,1},//0 1 1 1 1853 {1,1,1,1,0,0,1,1,0,0,0},//1 0 0 0 1944 {1,1,1,1,0,0,1,1,0,0,1},//1 0 0 1 1945 {1,1,1,1,0,0,1,1,1,0,0},//1 0 1 0 1948 {1,1,1,1,0,0,1,1,1,0,1},//1 0 1 1 1949 {1,1,1,1,0,1,1,1,0,0,0},//1 1 0 0 1976 {1,1,1,1,0,1,1,1,0,0,1},//1 1 0 1 1977 {1,1,1,1,0,1,1,1,1,0,0},//1 1 1 0 1980 {1,1,1,1,0,1,1,1,1,0,1},//1 1 1 1 1981 */ }; /* 8 5 6 7 0 0 0 A 9 4 p 0 ---> 0 1 p 0 10 3 2 1 B 1 0 0 */ private static readonly int[] template343 = new int[] { 192,193,200,201 /* // 0,0,0,1,1,0,0,A,0,0,B //A B {0,0,0,1,1,0,0,0,0,0,0},//0 0 192 {0,0,0,1,1,0,0,0,0,0,1},//0 1 193 {0,0,0,1,1,0,0,1,0,0,0},//1 0 200 {0,0,0,1,1,0,0,1,0,0,1},//1 1 201 */ }; public static void parallelThin(string imageSrcPath, string imageDestPath) { Bitmap bmp = new Bitmap(imageSrcPath); int Threshold = 0; Byte[,] array = ImageBinarization.ToBinaryArray(bmp, out Threshold); Byte x0,x1,x2,x3,x4,x5,x6,x7; Byte x8_4313,x9_4313,x10_4313; Byte x8_432,x9_432,x10_432; Byte x8_3412,x9_3412,x10_3412; Byte x8_343,x9_343,x10_343; Byte x8_441,x9_441,x10_441,x11_441,x12_441,x13_441,x14_441; int imageHeight = array.GetLength(0); int imageWidth = array.GetLength(1); int pixWidth = 0; int pixHeight = 0; int i,j;//循环用变量 int nValue = 0; int value4313 = 0, value432 = 0; int value441 = 0; int value3412 = 0, value343 = 0; bool endFlg = false; for (i = 0; i < imageHeight; i++) { for (j = 0; j < imageWidth; j++) { if(255 == array[i,j]) { array[i, j] = 0; } else { array[i, j] = 1; } } } do { for (i = 1; i < (imageHeight - 1); i++) { for (j = 1; j < (imageWidth - 1); j++) { endFlg = false; if (1 == array[i, j]) { x0 = array[i, j + 1]; x1 = array[i + 1, j + 1]; x2 = array[i + 1, j]; x3 = array[i + 1, j - 1]; x4 = array[i, j - 1]; x5 = array[i - 1, j - 1]; x6 = array[i - 1, j]; x7 = array[i - 1, j + 1]; nValue = (x0<<7) + (x1<<6) + (x2<<5) + (x3<<4) + (x4<<3) + (x5<<2) + (x6<<1) + x7; if(elimTable.Contains(nValue)) { int tmpVal = nValue<<3; pixWidth = array[i, j] + array[i, j - 1] + array[i, j + 1]; pixHeight = array[i, j] + array[i - 1, j] + array[i + 1, j]; if ((2 == pixWidth) || (2 == pixHeight)) { if(2 == pixWidth) { if (j < (imageWidth - 2)) { x8_3412 = array[i - 1, j + 2]; x9_3412 = array[i, j + 2]; x10_3412 = array[i + 1, j + 2]; value3412 = tmpVal + (x8_3412 << 2) + (x9_3412 << 1) + x10_3412; } if (j > 1) { x8_343 = array[i - 1, j - 2]; x9_343 = array[i, j - 2]; x10_343 = array[i + 1, j - 2]; value343 = tmpVal + (x8_343 << 2) + (x9_343 << 1) + x10_343; } if (!(template341.Contains(value3412) || template342.Contains(value3412) || template343.Contains(value343))) { array[i, j] = 0; endFlg = true; } } else//if (2 == pixHeight) { if (i < (imageHeight - 2)) { x8_4313 = array[i + 2, j + 1]; x9_4313 = array[i + 2, j]; x10_4313 = array[i + 2, j - 1]; value4313 = tmpVal + (x8_4313 << 2) + (x9_4313 << 1) + x10_4313; } if (i > 1) { x8_432 = array[i - 2, j + 1]; x9_432 = array[i - 2, j]; x10_432 = array[i - 2, j - 1]; value432 = tmpVal + (x8_432 << 2) + (x9_432 << 1) + x10_432; } if ((i < (imageHeight - 2)) && (j < (imageWidth - 2))) { x8_441 = array[i - 1, j + 2]; x9_441 = array[i, j + 2]; x10_441 = array[i + 1, j + 2]; x11_441 = array[i + 2, j + 2]; x12_441 = array[i + 2, j + 1]; x13_441 = array[i + 2, j]; x14_441 = array[i + 2, j - 1]; value441 = (tmpVal << 4) + (x8_441 << 6) + (x9_441 << 5) + (x10_441 << 4) + (x11_441 << 3) + (x12_441 << 2) + (x13_441 << 1) + x14_441; } if (!(template431.Contains(value4313) || template432.Contains(value432) || template433.Contains(value4313) || template44.Contains(value441))) { array[i, j] = 0; endFlg = true; } } } else { array[i,j] = 0; endFlg = true; } } } } } } while(endFlg); for (i = 0; i < imageHeight; i++) { for (j = 0; j < imageWidth; j++) { if (1 == array[i, j]) { array[i, j] = 0; } else { array[i, j] = 255; } } } Bitmap GrayBmp = ImageBinarization.BinaryArrayToBinaryBitmap(array); GrayBmp.Save(imageDestPath, System.Drawing.Imaging.ImageFormat.Jpeg); } } } |