日期:2014-05-18 浏览次数:21225 次
private void BlurAndSharpen_Click(object sender, System.EventArgs e)
{
Graphics graphics = this.CreateGraphics();
graphics.Clear(Color.White);
graphics.ScaleTransform(0.8f, 0.8f);
Bitmap image = new Bitmap("snike.bmp");
int Width = image.Width;
int Height = image.Height;
//image2:进行锐化处理
Bitmap image2 = (Bitmap)image.Clone();
Color colorTemp;
Color[,] color = new Color[3, 3];
//绘制原图
graphics.DrawImage(
image, new Rectangle(0, 0, Width, Height));
for (int i = 1; i < Width - 2; i++)
{
for (int j = 1; j < Height - 2; j++)
{
//访问周围9个点的RGB值
color[0, 0] = image.GetPixel(i - 1, j - 1);
color[0, 1] = image.GetPixel(i - 1, j);
color[0, 2] = image.GetPixel(i - 1, j + 1);
color[1, 0] = image.GetPixel(i, j - 1);
color[1, 1] = image.GetPixel(i, j);
color[1, 2] = image.GetPixel(i, j + 1);
color[2, 0] = image.GetPixel(i + 1, j - 1);
color[2, 1] = image.GetPixel(i + 1, j);
color[2, 2] = image.GetPixel(i + 1, j + 1);
int rSum = 0;
int gSum = 0;
int bSum = 0;
//分别求出周围9个点的R、G、B之和
for (int n = 0; n < 3; n++)
for (int nn = 0; nn < 3; nn++)
{
rSum += color[n, nn].R;
gSum += color[n, nn].G;
bSum += color[n, nn].B;
}
//用RGB的平均值做为当前点的RGB值
colorTemp = Color.FromArgb(255,
(int)(rSum / 9), (int)(gSum / 9), (int)(bSum / 9));
//将计算后的RGB值回写到位图
image.SetPixel(i, j, colorTemp);
}
//绘制经过平滑处理的效果图
graphics.DrawImage(
image, new Rectangle(Width, 0, Width, Height));
}
//进行锐化处理
Color colorLeft, colornow;
//常量dep:锐化系数,此值越大,锐化效果越明显
float dep = 0.550f;
for (int i = 1; i < Width - 1; i++)
{
for (int j = 1; j < Height - 1; j++)
{
colornow = image2.GetPixel(i, j);
colorLeft = image2.GetPixel(i - 1, j - 1);
float r = colornow.R + (colornow.R
- colorLeft.R * dep);
r = Math.Min(255, Math.Max(0, r));
float g = colornow.G + (colornow.G
- colorLeft.G * dep);
g = Math.Min(255, Math.Max(0, g));
float b = colornow.B + (colornow.B
- colorLeft.B * dep);
b = Math.Min(25