■ Bitmap 클래스에서 부울 가장자리 탐지 필터(Boolean Edge Detection Filter)를 사용하는 방법을 보여준다.
▶ BooleanFilterType.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
namespace TestProject { /// <summary> /// 부울 필터 타입 /// </summary> public enum BooleanFilterType { /// <summary> /// 가장자리 탐지 /// </summary> EdgeDetection, /// <summary> /// 선명화 /// </summary> Sharpen } } |
▶ 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 |
using System; using System.Collections.Generic; using System.Drawing; using System.Drawing.Imaging; using System.Runtime.InteropServices; namespace TestProject { /// <summary> /// 비트맵 헬퍼 /// </summary> public static class BitmapHelper { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 가장자리 마스크 리스트 /// </summary> private static List<string> _edgeMaskList; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Static #region 생성자 - BitmapHelper() /// <summary> /// 생성자 /// </summary> static BitmapHelper() { _edgeMaskList = new List<string>(); _edgeMaskList.Add("011011011"); _edgeMaskList.Add("000111111"); _edgeMaskList.Add("110110110"); _edgeMaskList.Add("111111000"); _edgeMaskList.Add("011011001"); _edgeMaskList.Add("100110110"); _edgeMaskList.Add("111011000"); _edgeMaskList.Add("111110000"); _edgeMaskList.Add("111011001"); _edgeMaskList.Add("100110111"); _edgeMaskList.Add("001011111"); _edgeMaskList.Add("111110100"); _edgeMaskList.Add("000011111"); _edgeMaskList.Add("000110111"); _edgeMaskList.Add("001011011"); _edgeMaskList.Add("001011011"); _edgeMaskList.Add("110110100"); } #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 부울 가장자리 탐지 필터 적용하기 - ApplyBooleanEdgeDetectionFilter(sourceBitmap, filterType, redFactor, greenFactor, blueFactor, threshold) /// <summary> /// 부울 가장자리 탐지 필터 적용하기 /// </summary> /// <param name="sourceBitmap">소스 비트맵</param> /// <param name="filterType">필터 타입</param> /// <param name="redFactor">적색 팩터</param> /// <param name="greenFactor">녹색 팩터</param> /// <param name="blueFactor">청색 팩터</param> /// <param name="threshold">임계치</param> /// <returns>비트맵</returns> public static Bitmap ApplyBooleanEdgeDetectionFilter ( Bitmap sourceBitmap, BooleanFilterType filterType, float redFactor = 1.0f, float greenFactor = 1.0f, float blueFactor = 1.0f, 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 filterOffset = 1; int sourceOffset = 0; int targetOffset = 0; int matrixMean = 0; int matrixTotal = 0; double matrixVariance = 0; double blueValue = 0; double greenValue = 0; double redValue = 0; string matrixPatern = string.Empty; for(int offsetY = filterOffset; offsetY < sourceBitmap.Height - filterOffset; offsetY++) { for(int offsetX = filterOffset; offsetX < sourceBitmap.Width - filterOffset; offsetX++) { targetOffset = offsetY * sourceBitmapData.Stride + offsetX * 4; matrixMean = 0; matrixTotal = 0; matrixVariance = 0; matrixPatern = string.Empty; for(int filterY = -filterOffset; filterY <= filterOffset; filterY++) { for(int filterX = -filterOffset; filterX <= filterOffset; filterX++) { sourceOffset = targetOffset + (filterX * 4) + (filterY * sourceBitmapData.Stride); matrixMean += sourceByteArray[sourceOffset ]; matrixMean += sourceByteArray[sourceOffset + 1]; matrixMean += sourceByteArray[sourceOffset + 2]; } } matrixMean = matrixMean / 9; for(int filterY = -filterOffset; filterY <= filterOffset; filterY++) { for(int filterX = -filterOffset; filterX <= filterOffset; filterX++) { sourceOffset = targetOffset + (filterX * 4) + (filterY * sourceBitmapData.Stride); matrixTotal = sourceByteArray[sourceOffset ]; matrixTotal += sourceByteArray[sourceOffset + 1]; matrixTotal += sourceByteArray[sourceOffset + 2]; matrixPatern += (matrixTotal > matrixMean ? "1" : "0" ); matrixVariance += Math.Pow ( matrixMean - (sourceByteArray[sourceOffset ] + sourceByteArray[sourceOffset + 1] + sourceByteArray[sourceOffset + 2]), 2 ); } } matrixVariance = matrixVariance / 9; if(filterType == BooleanFilterType.Sharpen) { blueValue = sourceByteArray[targetOffset ]; greenValue = sourceByteArray[targetOffset + 1]; redValue = sourceByteArray[targetOffset + 2]; if(matrixVariance > threshold) { if(_edgeMaskList.Contains(matrixPatern)) { blueValue = (blueValue * blueFactor ); greenValue = (greenValue * greenFactor); redValue = (redValue * redFactor ); blueValue = (blueValue > 255 ? 255 : (blueValue < 0 ? 0 : blueValue )); greenValue = (greenValue > 255 ? 255 : (greenValue < 0 ? 0 : greenValue)); redValue = (redValue > 255 ? 255 : (redValue < 0 ? 0 : redValue )); } } } else if(matrixVariance > threshold && _edgeMaskList.Contains(matrixPatern)) { blueValue = 255; greenValue = 255; redValue = 255; } else { blueValue = 0; greenValue = 0; redValue = 0; } targetByteArray[targetOffset ] = (byte)blueValue; targetByteArray[targetOffset + 1] = (byte)greenValue; targetByteArray[targetOffset + 2] = (byte)redValue; 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 } } |
▶ 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.ApplyBooleanEdgeDetectionFilter(sourceBitmap, BooleanFilterType.EdgeDetection, 1, 1, 1, 64); this.pictureBox.SizeMode = PictureBoxSizeMode.Zoom; this.pictureBox.Image = targetBitmap; } #endregion } } |