■ Bitmap 클래스에서 카툰 필터(Cartoon Filter)를 사용하는 방법을 보여준다.
▶ 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 |
using System; using System.Drawing; using System.Drawing.Imaging; using System.Runtime.InteropServices; namespace TestProject { /// <summary> /// 비트맵 헬퍼 /// </summary> public static class BitmapHelper { //////////////////////////////////////////////////////////////////////////////////////////////////// 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 카툰 필터 적용하기 - ApplyCartoonFilter(sourceBitmap, level, filterSize, threshold) /// <summary> /// 카툰 필터 적용하기 /// </summary> /// <param name="sourceBitmap">소스 비트맵</param> /// <param name="level">레벨</param> /// <param name="filterSize">필터 크기</param> /// <param name="threshold">임계치</param> /// <returns>비트맵</returns> public static Bitmap ApplyCartoonFilter(Bitmap sourceBitmap, int level, int filterSize, byte threshold) { Bitmap oilPaintBitmap = ApplyOilPaintFilter(sourceBitmap, level, filterSize); Bitmap edgeBitmap = ApplyGradientBasedEdgeDetectionFilter(sourceBitmap, threshold); BitmapData oilPaintBitmapData = oilPaintBitmap.LockBits ( new Rectangle (0, 0, oilPaintBitmap.Width, oilPaintBitmap.Height), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb ); byte[] oilPaintByteArray = new byte[oilPaintBitmapData.Stride * oilPaintBitmapData.Height]; Marshal.Copy(oilPaintBitmapData.Scan0, oilPaintByteArray, 0, oilPaintByteArray.Length); oilPaintBitmap.UnlockBits(oilPaintBitmapData); BitmapData edgeBitmapData = edgeBitmap.LockBits ( new Rectangle (0, 0, edgeBitmap.Width, edgeBitmap.Height), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb ); byte[] edgeByteArray = new byte[edgeBitmapData.Stride * edgeBitmapData.Height]; Marshal.Copy(edgeBitmapData.Scan0, edgeByteArray, 0, edgeByteArray.Length); edgeBitmap.UnlockBits(edgeBitmapData); byte[] targetByteArray = new byte [edgeBitmapData.Stride * edgeBitmapData.Height]; for(int k = 0; k + 4 < oilPaintByteArray.Length; k += 4) { if(edgeByteArray[k] == 255 || edgeByteArray[k + 1] == 255 || edgeByteArray[k + 2] == 255) { targetByteArray[k ] = 0; targetByteArray[k + 1] = 0; targetByteArray[k + 2] = 0; targetByteArray[k + 3] = 255; } else { targetByteArray[k ] = oilPaintByteArray[k ]; targetByteArray[k + 1] = oilPaintByteArray[k + 1]; targetByteArray[k + 2] = oilPaintByteArray[k + 2]; targetByteArray[k + 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(targetByteArray, 0, targetBitmapData.Scan0, targetByteArray.Length); targetBitmap.UnlockBits(targetBitmapData); return targetBitmap; } #endregion //////////////////////////////////////////////////////////////////////////////// Private #region 바이트 자르기 - ClipByte(value) /// <summary> /// 바이트 자르기 /// </summary> /// <param name="value">값</param> /// <returns>바이트</returns> private static byte ClipByte(double value) { return (byte)(value > 255 ? 255 : (value < 0 ? 0 : value)); } #endregion #region 임계치 체크하기 - CheckThreshold(sourceByteArray, offset1, offset2, gradientValue, threshold, divideBy) /// <summary> /// 임계치 체크하기 /// </summary> /// <param name="sourceByteArray">소스 바이트 배열</param> /// <param name="offset1">오프셋 1</param> /// <param name="offset2">오프셋 2</param> /// <param name="gradientValue">그라디언트 값</param> /// <param name="threshold">임계치</param> /// <param name="divideBy">젯수</param> /// <returns>임계치 체크 결과</returns> private static bool CheckThreshold ( byte[] sourceByteArray, int offset1, int offset2, ref int gradientValue, byte threshold, int divideBy = 1 ) { gradientValue += Math.Abs(sourceByteArray[offset1 ] - sourceByteArray[offset2 ]) / divideBy; gradientValue += Math.Abs(sourceByteArray[offset1 + 1] - sourceByteArray[offset2 + 1]) / divideBy; gradientValue += Math.Abs(sourceByteArray[offset1 + 2] - sourceByteArray[offset2 + 2]) / divideBy; return (gradientValue >= threshold); } #endregion #region 오일 페인트 필터 적용하기 - ApplyOilPaintFilter(sourceBitmap, level, filterSize) /// <summary> /// 오일 페인트 필터 적용하기 /// </summary> /// <param name="sourceBitmap">소스 비트맵</param> /// <param name="level">레벨</param> /// <param name="filterSize">필터 크기</param> /// <returns>비트맵</returns> private static Bitmap ApplyOilPaintFilter(Bitmap sourceBitmap, int level, int filterSize) { BitmapData sourceBitmapData = sourceBitmap.LockBits ( new Rectangle(0, 0, sourceBitmap.Width, sourceBitmap.Height), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb ); byte[] sourceByteArray = new byte[sourceBitmapData.Stride * sourceBitmapData.Height]; byte[] targetByteArray = new byte[sourceBitmapData.Stride * sourceBitmapData.Height]; Marshal.Copy(sourceBitmapData.Scan0, sourceByteArray, 0, sourceByteArray.Length); sourceBitmap.UnlockBits(sourceBitmapData); int[] intensityArray = new int [level]; int[] blueArray = new int [level]; int[] greenArray = new int [level]; int[] redArray = new int [level]; level = level - 1; int filterOffset = (filterSize - 1) / 2; int targetOffset = 0; int sourceOffset = 0; int currentIntensity = 0; int maximumIntensity = 0; int maximumIndex = 0; double blue = 0; double green = 0; double red = 0; for(int offsetY = filterOffset; offsetY < sourceBitmap.Height - filterOffset; offsetY++) { for(int offsetX = filterOffset; offsetX < sourceBitmap.Width - filterOffset; offsetX++) { blue = green = red = 0; currentIntensity = maximumIntensity = maximumIndex = 0; intensityArray = new int[level + 1]; blueArray = new int[level + 1]; greenArray = new int[level + 1]; redArray = new int[level + 1]; targetOffset = offsetY * sourceBitmapData.Stride + offsetX * 4; for(int filterY = -filterOffset; filterY <= filterOffset; filterY++) { for(int filterX = -filterOffset; filterX <= filterOffset; filterX++) { sourceOffset = targetOffset + (filterX * 4) + (filterY * sourceBitmapData.Stride); currentIntensity = (int )Math.Round ( ((double)(sourceByteArray[sourceOffset ] + sourceByteArray[sourceOffset + 1] + sourceByteArray[sourceOffset + 2]) / 3.0 * (level)) / 255.0 ); intensityArray[currentIntensity] += 1; blueArray [currentIntensity] += sourceByteArray[sourceOffset ]; greenArray[currentIntensity] += sourceByteArray[sourceOffset + 1]; redArray [currentIntensity] += sourceByteArray[sourceOffset + 2]; if(intensityArray[currentIntensity] > maximumIntensity) { maximumIntensity = intensityArray[currentIntensity]; maximumIndex = currentIntensity; } } } blue = blueArray [maximumIndex] / maximumIntensity; green = greenArray[maximumIndex] / maximumIntensity; red = redArray [maximumIndex] / maximumIntensity; targetByteArray[targetOffset ] = ClipByte(blue); targetByteArray[targetOffset + 1] = ClipByte(green); targetByteArray[targetOffset + 2] = ClipByte(red); targetByteArray[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(targetByteArray, 0, targetBitmapData.Scan0, targetByteArray.Length); targetBitmap.UnlockBits(targetBitmapData); return targetBitmap; } #endregion #region 그라디언트 기반 가장자리 탐지 필터 적용하기 - ApplyGradientBasedEdgeDetectionFilter(sourceBitmap, threshold) /// <summary> /// 그라디언트 기반 가장자리 탐지 필터 적용하기 /// </summary> /// <param name="sourceBitmap">소스 비트맵</param> /// <param name="threshold">임계치</param> /// <returns>비트맵</returns> private static Bitmap ApplyGradientBasedEdgeDetectionFilter(Bitmap sourceBitmap, byte threshold = 0) { BitmapData sourceBitmapData = sourceBitmap.LockBits ( new Rectangle(0, 0, sourceBitmap.Width, sourceBitmap.Height), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb ); byte[] sourceByteArray = new byte[sourceBitmapData.Stride * sourceBitmapData.Height]; byte[] targetByteArray = new byte[sourceBitmapData.Stride * sourceBitmapData.Height]; Marshal.Copy(sourceBitmapData.Scan0, sourceByteArray, 0, sourceByteArray.Length); sourceBitmap.UnlockBits(sourceBitmapData); int offset = 0; int gradientValue = 0; bool exceedThreshold = false; for(int offsetY = 1; offsetY < sourceBitmap.Height - 1; offsetY++) { for(int offsetX = 1; offsetX < sourceBitmap.Width - 1; offsetX++) { offset = offsetY * sourceBitmapData.Stride + offsetX * 4; gradientValue = 0; exceedThreshold = true; CheckThreshold ( sourceByteArray, offset - 4, offset + 4, ref gradientValue, threshold, 2 ); exceedThreshold = CheckThreshold ( sourceByteArray, offset - sourceBitmapData.Stride, offset + sourceBitmapData.Stride, ref gradientValue, threshold, 2 ); if(exceedThreshold == false) { gradientValue = 0; exceedThreshold = CheckThreshold ( sourceByteArray, offset - 4, offset + 4, ref gradientValue, threshold ); if(exceedThreshold == false) { gradientValue = 0; exceedThreshold = CheckThreshold ( sourceByteArray, offset - sourceBitmapData.Stride, offset + sourceBitmapData.Stride, ref gradientValue, threshold ); if(exceedThreshold == false) { gradientValue = 0; CheckThreshold ( sourceByteArray, offset - 4 - sourceBitmapData.Stride, offset + 4 + sourceBitmapData.Stride, ref gradientValue, threshold, 2 ); exceedThreshold = CheckThreshold ( sourceByteArray, offset - sourceBitmapData.Stride + 4, offset - 4 + sourceBitmapData.Stride, ref gradientValue, threshold, 2 ); if(exceedThreshold == false) { gradientValue = 0; exceedThreshold = CheckThreshold ( sourceByteArray, offset - 4 - sourceBitmapData.Stride, offset + 4 + sourceBitmapData.Stride, ref gradientValue, threshold ); if(exceedThreshold == false) { gradientValue = 0; exceedThreshold = CheckThreshold ( sourceByteArray, offset - sourceBitmapData.Stride + 4, offset + sourceBitmapData.Stride - 4, ref gradientValue, threshold ); } } } } } targetByteArray[offset ] = (byte)(exceedThreshold ? 255 : 0); targetByteArray[offset + 1] = targetByteArray[offset]; targetByteArray[offset + 2] = targetByteArray[offset]; targetByteArray[offset + 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(targetByteArray, 0, targetBitmapData.Scan0, targetByteArray.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 |
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.ApplyCartoonFilter(sourceBitmap, 20, 5, 250); this.pictureBox.SizeMode = PictureBoxSizeMode.Zoom; this.pictureBox.Image = targetBitmap; } #endregion } } |