■ Bitmap 클래스에서 컴파스 가장자리 탐지 필터(Compass Edge Detection Filter)를 사용하는 방법을 보여준다.
▶ CompassEdgeDetectionType.cs
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 |
namespace TestProject { /// <summary> /// 컴파스 가장자리 탐지 타입 /// </summary> public enum CompassEdgeDetectionType { /// <summary> /// Prewitt 3X3X4 /// </summary> Prewitt3X3X4, /// <summary> /// Prewitt 3X3X8 /// </summary> Prewitt3X3X8, /// <summary> /// Prewitt 5X5X4 /// </summary> Prewitt5X5X4, /// <summary> /// Sobel 3X3X4 /// </summary> Sobel3X3X4, /// <summary> /// Sobel 3X3X8 /// </summary> Sobel3X3X8, /// <summary> /// Sobel 5X5X4 /// </summary> Sobel5X5X4, /// <summary> /// Scharr 3X3X4 /// </summary> Scharr3X3X4, /// <summary> /// Scharr 3X3X8 /// </summary> Scharr3X3X8, /// <summary> /// Scharr 5X5X4 /// </summary> Scharr5X5X4, /// <summary> /// Kirsch 3X3X4 /// </summary> Kirsch3X3X4, /// <summary> /// Kirsch 3X3X8 /// </summary> Kirsch3X3X8, /// <summary> /// Isotropic 3X3X4 /// </summary> Isotropic3X3X4, /// <summary> /// Isotropic 3X3X8 /// </summary> Isotropic3X3X8 } } |
▶ BitmapHelper.cs
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 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 |
using System; using System.Drawing; using System.Drawing.Imaging; using System.Runtime.InteropServices; namespace TestProject { /// <summary> /// 비트맵 헬퍼 /// </summary> public static class BitmapHelper { //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Public #region Prewitt 3X3X4 - Prewitt3X3X4 /// <summary> /// Prewitt 3X3X4 /// </summary> public static double[,,] Prewitt3X3X4 { get { double[,] baseKernelArray = new double[,] { { -1, 0, 1 }, { -1, 0, 1 }, { -1, 0, 1 } }; double[,,] kernelArray = RotateArray(baseKernelArray, 90); return kernelArray; } } #endregion #region Prewitt 3X3X8 - Prewitt3X3X8 /// <summary> /// Prewitt 3X3X8 /// </summary> public static double[,,] Prewitt3X3X8 { get { double[,] baseKernelArray = new double[,] { { -1, 0, 1 }, { -1, 0, 1 }, { -1, 0, 1 } }; double[,,] kernelArray = RotateArray(baseKernelArray, 45); return kernelArray; } } #endregion #region Prewitt 5X5X4 - Prewitt5X5X4 /// <summary> /// Prewitt 5X5X4 /// </summary> public static double[,,] Prewitt5X5X4 { get { double[,] baseKernelArray = new double[,] { { -2, -1, 0, 1, 2 }, { -2, -1, 0, 1, 2 }, { -2, -1, 0, 1, 2 }, { -2, -1, 0, 1, 2 }, { -2, -1, 0, 1, 2 } }; double[,,] kernelArray = RotateArray(baseKernelArray, 90); return kernelArray; } } #endregion #region Kirsch 3X3X4 - Kirsch3X3X4 /// <summary> /// Kirsch 3X3X4 /// </summary> public static double[,,] Kirsch3X3X4 { get { double[,] baseKernelArray = new double[,] { { -3, -3, 5 }, { -3, 0, 5 }, { -3, -3, 5 } }; double[,,] kernelArray = RotateArray(baseKernelArray, 90); return kernelArray; } } #endregion #region Kirsch 3X3X8 - Kirsch3X3X8 /// <summary> /// Kirsch 3X3X8 /// </summary> public static double[,,] Kirsch3X3X8 { get { double[,] baseKernelArray = new double[,] { { -3, -3, 5 }, { -3, 0, 5 }, { -3, -3, 5 } }; double[,,] kernelArray = RotateArray(baseKernelArray, 45); return kernelArray; } } #endregion #region Sobel 3X3X4 - Sobel3X3X4 /// <summary> /// Sobel 3X3X4 /// </summary> public static double[,,] Sobel3X3X4 { get { double[,] baseKernelArray = new double[,] { { -1, 0, 1 }, { -2, 0, 2 }, { -1, 0, 1 } }; double[,,] kernelArray = RotateArray(baseKernelArray, 90); return kernelArray; } } #endregion #region Sobel 3X3X8 - Sobel3X3X8 /// <summary> /// Sobel3X3X8 /// </summary> public static double[,,] Sobel3X3X8 { get { double[,] baseKernelArray = new double[,] { { -1, 0, 1 }, { -2, 0, 2 }, { -1, 0, 1 } }; double[,,] kernelArray = RotateArray(baseKernelArray, 45); return kernelArray; } } #endregion #region Sobel 5X5X4 - Sobel5X5X4 /// <summary> /// Sobel 5X5X4 /// </summary> public static double[,,] Sobel5X5X4 { get { double[,] baseKernelArray = new double[,] { { -5, -4, 0, 4, 5 }, { -8, -10, 0, 10, 8 }, { -10, -20, 0, 20, 10 }, { -8, -10, 0, 10, 8 }, { -5, -4, 0, 4, 5 } }; double[,,] kernelArray = RotateArray(baseKernelArray, 90); return kernelArray; } } #endregion #region Scharr 3X3X4 - Scharr3X3X4 /// <summary> /// Scharr 3X3X4 /// </summary> public static double[,,] Scharr3X3X4 { get { double[,] baseKernelArray = new double[,] { { -1, 0, 1 }, { -3, 0, 3 }, { -1, 0, 1 } }; double[, ,] kernelArray = RotateArray(baseKernelArray, 90); return kernelArray; } } #endregion #region Scharr 3X3X8 - Scharr3X3X8 /// <summary> /// Scharr 3X3X8 /// </summary> public static double[,,] Scharr3X3X8 { get { double[,] baseKernelArray = new double[,] { { -1, 0, 1 }, { -3, 0, 3 }, { -1, 0, 1 } }; double[,,] kernelArray = RotateArray(baseKernelArray, 45); return kernelArray; } } #endregion #region Scharr 5X5X4 - Scharr5X5X4 /// <summary> /// Scharr 5X5X4 /// </summary> public static double[, ,] Scharr5X5X4 { get { double[,] baseKernelArray = new double[,] { { -1, -1, 0, 1, 1 }, { -2, -2, 0, 2, 2 }, { -3, -6, 0, 6, 3 }, { -2, -2, 0, 2, 2 }, { -1, -1, 0, 1, 1 } }; double[,,] kernelArray = RotateArray(baseKernelArray, 90); return kernelArray; } } #endregion #region Isotropic 3X3X4 - Isotropic3X3X4 /// <summary> /// Isotropic 3X3X4 /// </summary> public static double[,,] Isotropic3X3X4 { get { double[,] baseKernelArray = new double[,] { { -1, 0, 1 }, { -Math.Sqrt(2), 0, Math.Sqrt(2) }, { -1, 0, 1 } }; double[,,] kernelArray = RotateArray(baseKernelArray, 90); return kernelArray; } } #endregion #region Isotropic 3X3X8 - Isotropic3X3X8 /// <summary> /// Isotropic 3X3X8 /// </summary> public static double[,,] Isotropic3X3X8 { get { double[,] baseKernelArray = new double[,] { { -1, 0, 1 }, { -Math.Sqrt(2), 0, Math.Sqrt(2) }, { -1, 0, 1 } }; double[,,] kernelArray = RotateArray(baseKernelArray, 45); return kernelArray; } } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Public #region 비트맵 로드하기 - LoadBitmap(filePath) /// <summary> /// 비트맵 로드하기 /// </summary> /// <param name="filePath">파일 경로</param> /// <returns>비트맵</returns> public static Bitmap LoadBitmap(string filePath) { using(Bitmap bitmap = new Bitmap(filePath)) { return new Bitmap(bitmap); } } #endregion #region 컴파스 가장자리 탐지 필터 적용하기 - ApplyCompassEdgeDetectionFilter(sourceBitmap, compassEdgeDetectionType) /// <summary> /// 컴파스 가장자리 탐지 필터 적용하기 /// </summary> /// <param name="sourceBitmap">소스 비트맵</param> /// <param name="compassEdgeDetectionType">컴파스 가장자리 탐지 타입</param> /// <returns>비트맵</returns> public static Bitmap ApplyCompassEdgeDetectionFilter(Bitmap sourceBitmap, CompassEdgeDetectionType compassEdgeDetectionType) { Bitmap targetBitmap = null; switch(compassEdgeDetectionType) { case CompassEdgeDetectionType.Sobel3X3X4 : targetBitmap = ApplyConvolutionFilter(sourceBitmap, Sobel3X3X4, 1.0 / 4.0); break; case CompassEdgeDetectionType.Sobel3X3X8 : targetBitmap = ApplyConvolutionFilter(sourceBitmap, Sobel3X3X8, 1.0/ 4.0); break; case CompassEdgeDetectionType.Sobel5X5X4 : targetBitmap = ApplyConvolutionFilter(sourceBitmap, Sobel5X5X4, 1.0/ 84.0); break; case CompassEdgeDetectionType.Prewitt3X3X4 : targetBitmap = ApplyConvolutionFilter(sourceBitmap, Prewitt3X3X4, 1.0 / 3.0); break; case CompassEdgeDetectionType.Prewitt3X3X8 : targetBitmap = ApplyConvolutionFilter(sourceBitmap, Prewitt3X3X8, 1.0/ 3.0); break; case CompassEdgeDetectionType.Prewitt5X5X4 : targetBitmap = ApplyConvolutionFilter(sourceBitmap, Prewitt5X5X4, 1.0 / 15.0); break; case CompassEdgeDetectionType.Scharr3X3X4 : targetBitmap = ApplyConvolutionFilter(sourceBitmap, Scharr3X3X4, 1.0 / 4.0); break; case CompassEdgeDetectionType.Scharr3X3X8 : targetBitmap = ApplyConvolutionFilter(sourceBitmap, Scharr3X3X8, 1.0 / 4.0); break; case CompassEdgeDetectionType .Scharr5X5X4 : targetBitmap = ApplyConvolutionFilter(sourceBitmap, Scharr5X5X4, 1.0 / 21.0); break; case CompassEdgeDetectionType.Kirsch3X3X4 : targetBitmap = ApplyConvolutionFilter(sourceBitmap, Kirsch3X3X4, 1.0 / 15.0); break; case CompassEdgeDetectionType.Kirsch3X3X8 : targetBitmap = ApplyConvolutionFilter(sourceBitmap, Kirsch3X3X8, 1.0 / 15.0); break; case CompassEdgeDetectionType.Isotropic3X3X4 : targetBitmap = ApplyConvolutionFilter(sourceBitmap, Isotropic3X3X4, 1.0 / 3.4); break; case CompassEdgeDetectionType.Isotropic3X3X8 : targetBitmap = ApplyConvolutionFilter(sourceBitmap, Isotropic3X3X8, 1.0 / 3.4); break; } return targetBitmap; } #endregion //////////////////////////////////////////////////////////////////////////////// Private #region 배열 회전하기 - RotateArray(sourceArray, angle) /// <summary> /// 배열 회전하기 /// </summary> /// <param name="sourceArray">소스 배열</param> /// <param name="angle">각도</param> /// <returns>배열</returns> private static double[,,] RotateArray(double[,] sourceArray, double angle) { double[,,] targetArray = new double[(int )(360 / angle), sourceArray.GetLength(0), sourceArray.GetLength(1)]; int xOffset = sourceArray.GetLength(1) / 2; int yOffset = sourceArray.GetLength(0) / 2; for(int y = 0; y < sourceArray.GetLength(0); y++) { for (int x = 0; x < sourceArray.GetLength(1); x++) { for(int compass = 0; compass < targetArray.GetLength(0); compass++) { double radian = compass * angle * Math.PI / 180.0; int targetX = (int)(Math.Round((x - xOffset) * Math.Cos(radian) - (y - yOffset) * Math.Sin(radian)) + xOffset); int targetY = (int)(Math.Round((x - xOffset) * Math.Sin(radian) + (y - yOffset) * Math.Cos(radian)) + yOffset); targetArray[compass, targetY, targetX] = sourceArray[y, x]; } } } return targetArray; } #endregion #region 회선 필터 적용하기 - ApplyConvolutionFilter(sourceBitmap, filterArray, factor, bias) /// <summary> /// 회선 필터 적용하기 /// </summary> /// <param name="sourceBitmap">소스 비트맵</param> /// <param name="filterArray">필터 배열</param> /// <param name="factor">팩터</param> /// <param name="bias">바이어스</param> /// <returns>비트맵</returns> private static Bitmap ApplyConvolutionFilter(Bitmap sourceBitmap, double[,,] filterArray, double factor = 1, int bias = 0) { BitmapData sourceBitmapData = sourceBitmap.LockBits ( new Rectangle(0, 0, sourceBitmap.Width, sourceBitmap.Height), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb ); byte[] sourceArray = new byte [sourceBitmapData.Stride * sourceBitmapData.Height]; byte[] targetArray = new byte [sourceBitmapData.Stride * sourceBitmapData.Height]; Marshal.Copy(sourceBitmapData.Scan0, sourceArray, 0, sourceArray.Length); sourceBitmap.UnlockBits(sourceBitmapData); double blue = 0.0; double green = 0.0; double red = 0.0; double blueCompass = 0.0; double greenCompass = 0.0; double redCompass = 0.0; int filterWidth = filterArray.GetLength(1); int filterHeight = filterArray.GetLength(0); int filterOffset = (filterWidth-1) / 2; int sourceOffset = 0; int targetOffset = 0; for(int offsetY = filterOffset; offsetY < sourceBitmap.Height - filterOffset; offsetY++) { for(int offsetX = filterOffset; offsetX < sourceBitmap.Width - filterOffset; offsetX++) { blue = 0; green = 0; red = 0; targetOffset = offsetY * sourceBitmapData.Stride + offsetX * 4; for(int compass = 0; compass < filterArray.GetLength(0); compass++) { blueCompass = 0.0; greenCompass = 0.0; redCompass = 0.0; for(int filterY = -filterOffset; filterY <= filterOffset; filterY++) { for(int filterX = -filterOffset; filterX <= filterOffset; filterX++) { sourceOffset = targetOffset + (filterX * 4) + (filterY * sourceBitmapData.Stride); blueCompass += (double)(sourceArray[sourceOffset ]) * filterArray[compass, filterY + filterOffset, filterX + filterOffset]; greenCompass += (double)(sourceArray[sourceOffset + 1]) * filterArray[compass, filterY + filterOffset, filterX + filterOffset]; redCompass += (double)(sourceArray[sourceOffset + 2]) * filterArray[compass, filterY + filterOffset, filterX + filterOffset]; } } blue = (blueCompass > blue ? blueCompass : blue ); green = (greenCompass > green ? greenCompass : green); red = (redCompass > red ? redCompass : red ); } blue = factor * blue + bias; green = factor * green + bias; red = factor * red + bias; if(blue > 255) { blue = 255; } else if(blue < 0) { blue = 0; } if(green > 255) { green = 255; } else if(green < 0) { green = 0; } if(red > 255) { red = 255; } else if(red < 0) { red = 0; } targetArray[targetOffset ] = (byte)(blue); targetArray[targetOffset + 1] = (byte)(green); targetArray[targetOffset + 2] = (byte)(red); targetArray[targetOffset + 3] = 255; } } Bitmap targetBitmap = new Bitmap(sourceBitmap.Width, sourceBitmap.Height); BitmapData targetBitmapData = targetBitmap.LockBits ( new Rectangle (0, 0, targetBitmap.Width, targetBitmap.Height), ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb ); Marshal.Copy(targetArray, 0, targetBitmapData.Scan0, targetArray.Length); targetBitmap.UnlockBits(targetBitmapData); return targetBitmap; } #endregion } } |
▶ MainForm.cs
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 |
using System.Drawing; using System.Windows.Forms; namespace TestProject { /// <summary> /// 메인 폼 /// </summary> public partial class MainForm : Form { //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainForm() /// <summary> /// 생성자 /// </summary> public MainForm() { InitializeComponent(); Bitmap sourceBitmap = BitmapHelper.LoadBitmap("IMAGE\\sample.jpg"); Bitmap targetBitmap = BitmapHelper.ApplyCompassEdgeDetectionFilter ( sourceBitmap, CompassEdgeDetectionType.Isotropic3X3X4 ); this.pictureBox.SizeMode = PictureBoxSizeMode.Zoom; this.pictureBox.Image = targetBitmap; } #endregion } } |