发布时间:2021-06-17编辑:佚名阅读(1309)
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Drawing.Drawing2D;//对二维图像进行处理需导入该命名空间
1. 浮雕效果原理:对图像像素点的像素值分别与相邻像素点的像素值相减后加上128, 然后将其作为新的像素点的值
处理如下:
private void emboss_Click(object sender, EventArgs e) //点击“浮雕”控件产生浮雕效果 { Bitmap myBitmap = new Bitmap(pictureBox1.Image);//创建Bitmap对象:提取像素信息,转化为二维数组 for (int i = 0; i < myBitmap.Width - 1; i++) { for (int j = 0; j < myBitmap.Height - 1; j++) { Color Color1 = myBitmap.GetPixel(i, j);//调用GetPixel方法获得像素点颜色 Color Color2 = myBitmap.GetPixel(i + 1, j + 1); int red = Math.Abs(Color1.R - Color2.R + 128); //调用绝对值Abs函数 //颜色处理 int green = Math.Abs(Color1.G - Color2.G + 128); int blue = Math.Abs(Color1.B - Color2.B + 128); if (red > 255) red = 255; if (red < 0) red = 0; if (green > 255) green = 255; if (green < 0) green = 0; if (blue > 255) blue = 255; if (blue < 0) blue = 0; //用SetPixel()方法设置像素点颜色 myBitmap.SetPixel(i, j, Color.FromArgb(red, green, blue)); } } pictureBox1.Image = myBitmap; }
2. 锐化:将原图像与拉普拉斯模板相乘,再加上原图像。
private void button13_Click(object sender, EventArgs e) //点击“锐化”控件产生锐化效果 { Bitmap myBitmap = new Bitmap(pictureBox1.Image); //建立拉普拉斯模板 int[] Laplacian = { -1, -1, -1, -1, 9, -1, -1, -1, -1 }; Color pixel; //这里注意边界的像素暂不处理,否则超出数组范围 for (int i = 1; i < myBitmap.Width - 1; i++) { for (int j = 1; j < myBitmap.Height - 1; j++) { int red = 0, green = 0, blue = 0; int index = 0; for (int col = -1; col <= 1; col++) //3*3处理 { for (int row = -1; row <= 1; row++) { pixel = myBitmap.GetPixel(i + row, j + col); red += pixel.R * Laplacian[index]; green += pixel.G * Laplacian[index]; blue += pixel.B * Laplacian[index]; index++; } } if (red > 255) red = 255; if (red < 0) red = 0; if (green > 255) green = 255; if (green < 0) green = 0; if (blue > 255) blue = 255; if (blue < 0) blue = 0; myBitmap.SetPixel(i - 1, j - 1, Color.FromArgb((int)red, (int)green, (int)blue)); //这里注意是i-1,j-1,否则效果很糟糕 } } pictureBox1.Image = myBitmap; }
1人
0人
0人
0人