■ Bitmap 클래스에서 가우스 차이 필터(Gauss Deference Filter)를 사용하는 방법을 보여준다.
▶ Matrix.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 |
namespace TestProject { /// <summary> /// 매트릭스 /// </summary> public static class Matrix { //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Public #region Gaussian 3X3 - Gaussian3X3 /// <summary> /// Gaussian 3X3 /// </summary> public static double[,] Gaussian3X3 { get { return new double[,] { { 1, 2, 1 }, { 2, 4, 2 }, { 1, 2, 1 } }; } } #endregion #region Gaussian 5X5 Type1 - Gaussian5X5Type1 /// <summary> /// Gaussian 5X5 Type1 /// </summary> public static double[,] Gaussian5X5Type1 { get { return new double[,] { { 2, 4 , 5 , 4 , 2 }, { 4, 9 , 12, 9 , 4 }, { 5, 12, 15, 12, 5 }, { 4, 9 , 12, 9 , 4 }, { 2, 4 , 5 , 4 , 2 } }; } } #endregion #region Gaussian 5X5 Type2 - Gaussian5X5Type2 /// <summary> /// Gaussian 5X5 Type2 /// </summary> public static double[,] Gaussian5X5Type2 { get { return new double[,] { { 1, 4 , 6 , 4 , 1 }, { 4, 16, 24, 16, 4 }, { 6, 24, 36, 24, 6 }, { 4, 16, 24, 16, 4 }, { 1, 4 , 6 , 4 , 1 } }; } } #endregion } } |
▶ 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 |
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 가우스 3X5 타입 1 차이 필터 적용하기 - ApplyGauss3X5Type1DifferenceFilter(sourceBitmap, grayscale, invert, bias) /// <summary> /// 가우스 3X5 타입 1 차이 필터 적용하기 /// </summary> /// <param name="sourceBitmap">소스 비트맵</param> /// <param name="grayscale">회색조 여부</param> /// <param name="invert">반전 여부</param> /// <param name="bias">바이어스</param> /// <returns>비트맵</returns> public static Bitmap ApplyGauss3X5Type1DifferenceFilter ( Bitmap sourceBitmap, bool grayscale = false, bool invert = false, int bias = 0 ) { Bitmap bitmap3X3 = ApplyConvolutionFilter(sourceBitmap, Matrix.Gaussian3X3, 1.0 / 16.0, 0, grayscale); Bitmap bitmap5X5 = ApplyConvolutionFilter(sourceBitmap, Matrix.Gaussian5X5Type1, 1.0 / 159.0, 0, grayscale); SubtractImage(bitmap3X3, bitmap5X5, invert, bias); return bitmap3X3; } #endregion #region 가우스 3X5 타입 2 차이 필터 적용하기 - ApplyGauss3X5Type2DifferenceFilter(sourceBitmap, grayscale, invert, bias) /// <summary> /// 가우스 3X5 타입 2 차이 필터 적용하기 /// </summary> /// <param name="sourceBitmap">소스 비트맵</param> /// <param name="grayscale">회색조 여부</param> /// <param name="invert">반전 여부</param> /// <param name="bias">바이어스</param> /// <returns>비트맵</returns> public static Bitmap ApplyGauss3X5Type2DifferenceFilter ( Bitmap sourceBitmap, bool grayscale = false, bool invert = false, int bias = 0 ) { Bitmap bitmap3X3 = ApplyConvolutionFilter(sourceBitmap, Matrix.Gaussian3X3, 1.0 / 16.0, 0, true ); Bitmap bitmap5X5 = ApplyConvolutionFilter(sourceBitmap, Matrix.Gaussian5X5Type2, 1.0 / 256.0, 0, true ); SubtractImage(bitmap3X3, bitmap5X5, invert, bias); return bitmap3X3; } #endregion //////////////////////////////////////////////////////////////////////////////// Private #region 회선 필터 적용하기 - ApplyConvolutionFilter(sourceBitmap, filterArray, factor, bias, grayscale) /// <summary> /// 회선 필터 적용하기 /// </summary> /// <param name="sourceBitmap">소스 비트맵</param> /// <param name="filterArray">필터 배열</param> /// <param name="factor">팩터</param> /// <param name="bias">바이어스</param> /// <param name="grayscale">회색조 여부</param> /// <returns>비트맵</returns> private static Bitmap ApplyConvolutionFilter ( Bitmap sourceBitmap, double[,] filterArray, double factor = 1, int bias = 0, bool grayscale = false ) { 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); if(grayscale == true) { float rgb = 0; for(int k = 0; k < sourceByteArray.Length; k += 4) { rgb = sourceByteArray[k ] * 0.11f; rgb += sourceByteArray[k + 1] * 0.59f; rgb += sourceByteArray[k + 2] * 0.3f; sourceByteArray[k ] = (byte )rgb; sourceByteArray[k + 1] = sourceByteArray[k]; sourceByteArray[k + 2] = sourceByteArray[k]; sourceByteArray[k + 3] = 255; } } double blue = 0.0; double green = 0.0; double red = 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 filterY = -filterOffset; filterY <= filterOffset; filterY++) { for(int filterX = -filterOffset; filterX <= filterOffset; filterX++) { sourceOffset = targetOffset + (filterX * 4) + (filterY * sourceBitmapData.Stride); blue += (double)(sourceByteArray[sourceOffset ]) * filterArray[filterY + filterOffset, filterX + filterOffset]; green += (double)(sourceByteArray[sourceOffset + 1]) * filterArray[filterY + filterOffset, filterX + filterOffset]; red += (double)(sourceByteArray[sourceOffset + 2]) * filterArray[filterY + filterOffset, filterX + filterOffset]; } } 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; } targetByteArray[targetOffset ] = (byte)blue; targetByteArray[targetOffset + 1] = (byte)green; targetByteArray[targetOffset + 2] = (byte)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 이미지 빼기 - SubtractImage(sourceBitmap1, sourceBitmap2, invert, bias) /// <summary> /// 이미지 빼기 /// </summary> /// <param name="sourceBitmap1">소스 비트맵 1</param> /// <param name="sourceBitmap2">소스 비트맵 2</param> /// <param name="invert">반전 여부</param> /// <param name="bias">바이어스</param> private static void SubtractImage(Bitmap sourceBitmap1, Bitmap sourceBitmap2, bool invert = false, int bias = 0) { BitmapData sourceBitmapData1 = sourceBitmap1.LockBits ( new Rectangle(0, 0, sourceBitmap1.Width, sourceBitmap1.Height), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb ); byte[] sourceByteArray1 = new byte[sourceBitmapData1.Stride * sourceBitmapData1.Height]; Marshal.Copy(sourceBitmapData1.Scan0, sourceByteArray1, 0, sourceByteArray1.Length); BitmapData sourceBitmapData2 = sourceBitmap2.LockBits ( new Rectangle(0, 0, sourceBitmap2.Width, sourceBitmap2.Height), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb ); byte[] sourceByteArray2 = new byte[sourceBitmapData2.Stride * sourceBitmapData2.Height]; Marshal.Copy(sourceBitmapData2.Scan0, sourceByteArray2, 0, sourceByteArray2.Length); sourceBitmap2.UnlockBits(sourceBitmapData2); int blue = 0; int green = 0; int red = 0; for(int k = 0; k < sourceByteArray1.Length && k < sourceByteArray2.Length; k += 4) { if(invert == true ) { blue = 255 - sourceByteArray1[k ] - sourceByteArray2[k ] + bias; green = 255 - sourceByteArray1[k + 1] - sourceByteArray2[k + 1] + bias; red = 255 - sourceByteArray1[k + 2] - sourceByteArray2[k + 2] + bias; } else { blue = sourceByteArray1[k ] - sourceByteArray2[k ] + bias; green = sourceByteArray1[k + 1] - sourceByteArray2[k + 1] + bias; red = sourceByteArray1[k + 2] - sourceByteArray2[k + 2] + bias; } blue = (blue < 0 ? 0 : (blue > 255 ? 255 : blue )); green = (green < 0 ? 0 : (green > 255 ? 255 : green)); red = (red < 0 ? 0 : (red > 255 ? 255 : red )); sourceByteArray1[k ] = (byte)blue; sourceByteArray1[k + 1] = (byte)green; sourceByteArray1[k + 2] = (byte)red; sourceByteArray1[k + 3] = 255; } Marshal.Copy(sourceByteArray1, 0, sourceBitmapData1.Scan0, sourceByteArray1.Length); sourceBitmap1.UnlockBits(sourceBitmapData1); } #endregion } } |
▶ MainForm.cs
—————————————————————————————————-
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.ApplyGauss3X5Type1DifferenceFilter(sourceBitmap, false, false, 64);
this.pictureBox.SizeMode = PictureBoxSizeMode.Zoom;
this.pictureBox.Image = targetBitmap;
}
#endregion
}
}
—————————————————————————————————-