728x90
반응형
728x170
▶ BitmapHelper.cs
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 가우시안 커널 계산하기 - CalculateGaussianKernel(length, weight)
/// <summary>
/// 가우시안 커널 계산하기
/// </summary>
/// <param name="length">길이</param>
/// <param name="weight">가중치</param>
/// <returns>가우시안 커널 배열</returns>
public static double[,] CalculateGaussianKernel(int length, double weight)
{
double[,] filterArray = new double [length, length];
double totalSummary = 0;
int kernelRadius = length / 2;
double distance = 0;
double eulerCalculated = 1.0 / (2.0 * Math.PI * Math.Pow(weight, 2));
for(int filterY = -kernelRadius; filterY <= kernelRadius; filterY++)
{
for(int filterX = -kernelRadius; filterX <= kernelRadius; filterX++)
{
distance = ((filterX * filterX) + (filterY * filterY)) / (2 * (weight * weight));
filterArray[filterY + kernelRadius, filterX + kernelRadius] = eulerCalculated * Math.Exp(-distance);
totalSummary += filterArray[filterY + kernelRadius, filterX + kernelRadius];
}
}
for(int y = 0; y < length; y++)
{
for(int x = 0; x < length; x++)
{
filterArray[y, x] = filterArray[y, x] * (1.0 / totalSummary);
}
}
return filterArray;
}
#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>
public 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[] 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);
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;
blue = (blue > 255 ? 255 : (blue < 0 ? 0 : blue ));
green = (green > 255 ? 255 : (green < 0 ? 0 : green));
red = (red > 255 ? 255 : (red < 0 ? 0 : red ));
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
}
}
728x90
▶ 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();
double[,] kernelArray = BitmapHelper.CalculateGaussianKernel(5, 5.5);
Bitmap sourceBitmap = BitmapHelper.LoadBitmap("IMAGE\\sample.jpg");
Bitmap targetBitmap = BitmapHelper.ApplyConvolutionFilter(sourceBitmap, kernelArray, 1, 0);
this.pictureBox.SizeMode = PictureBoxSizeMode.Zoom;
this.pictureBox.Image = targetBitmap;
}
#endregion
}
}
728x90
반응형
그리드형(광고전용)
'C# > WinForm' 카테고리의 다른 글
[C#/WINFORM] Bitmap 클래스 : 2개의 비트맵에서 차이점 찾기 (0) | 2021.01.10 |
---|---|
[C#/WINFORM] Bitmap 클래스 : 그라디언트 기반 가장자리 탐지 필터(Gradient Based Edge Detection Filter) 사용하기 (0) | 2021.01.10 |
[C#/WINFORM] Bitmap 클래스 : 카툰 필터(Cartoon Filter) 사용하기 (0) | 2021.01.09 |
[C#/WINFORM] Bitmap 클래스 : 스무딩 필터(Smoothing Filter) 사용하기 (0) | 2021.01.09 |
[C#/WINFORM] Bitmap 클래스 : 선명 가장자리 탐지 필터(Sharpen Edge Detection Filter) 사용하기 (0) | 2021.01.09 |
[C#/WINFORM] Bitmap 클래스 : 회색조 비트맵 구하기 (0) | 2021.01.08 |
[C#/WINFORM] Bitmap 클래스 : 투명 비트맵 구하기 (0) | 2021.01.08 |
[C#/WINFORM] Bitmap 클래스 : 불선명 필터(Blur Filter) 사용하기 (0) | 2021.01.08 |
[C#/WINFORM] Bitmap 클래스 : 비트맵 회전하기 (0) | 2021.01.08 |
[C#/WINFORM] Bitmap 클래스 : 비트맵 자르기(Shear) (0) | 2021.01.08 |
댓글을 달아 주세요