四.C#实现图像处理(锐化,灰度,浮雕等效果)
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; namespace ImageProcessDamon { public partial class Form1 : Form { //图像处理主界面 public Form1() { InitializeComponent(); } Image imgOriginal; //打开一张图片 private void btnOpenimage_Click(object sender, EventArgs e) { OpenFileDialog ofd = new OpenFileDialog(); ofd.ShowDialog(); string strFileName = ofd.FileName; imgOriginal = Image.FromFile(strFileName); pictureBox1 .BackgroundImage = imgOriginal; } //底片处理 private void button2_Click(object sender, EventArgs e) { Bitmap bmp = new Bitmap( imgOriginal); for (int i = 0; i < bmp.Width ; i++) { for (int j = 0; j < bmp.Height ; j ++) { //获得原来像素点颜色 Color c = bmp.GetPixel(i , j); //改变 Color cAfter = Color.FromArgb(255 - c.R, 255 - c.G, 255 - c.B); //设置回去 bmp.SetPixel(i, j, cAfter); } } picAfter.BackgroundImage = bmp; } //灰度化 private void button2_Click_1(object sender, EventArgs e) { Bitmap bmp = new Bitmap(imgOriginal); for (int i = 0; i < bmp.Width; i++) { for (int j = 0; j < bmp.Height; j++) { //获得原来像素点颜色 Color c = bmp.GetPixel(i, j); //改变 int gray = (c.R + c.G + c.B) / 3; Color cAfter = Color.FromArgb(gray ,gray ,gray ); //设置回去 bmp.SetPixel(i, j, cAfter); } } picAfter.BackgroundImage = bmp; } private void btnSave_Click(object sender, EventArgs e) { //保存图片 SaveFileDialog sfd = new SaveFileDialog(); sfd.ShowDialog(); picAfter.BackgroundImage.Save (sfd .FileName +".bmp"); } private void button3_Click(object sender, EventArgs e) { //以浮雕效果显示图像 try { int Height = this.pictureBox1.BackgroundImage.Height; int Width = this.pictureBox1.BackgroundImage.Width; Bitmap newBitmap = new Bitmap(Width, Height); Bitmap oldBitmap = (Bitmap)this.pictureBox1.BackgroundImage; Color pixel1, pixel2; for (int x = 0; x < Width - 1; x++) { for (int y = 0; y < Height - 1; y++) { int r = 0, g = 0, b = 0; pixel1 = oldBitmap.GetPixel(x, y); pixel2 = oldBitmap.GetPixel(x + 1, y + 1); r = Math.Abs(pixel1.R - pixel2.R + 128); g = Math.Abs(pixel1.G - pixel2.G + 128); b = Math.Abs(pixel1.B - pixel2.B + 128); if (r > 255) r = 255; if (r < 0) r = 0; if (g > 255) g = 255; if (g < 0) g = 0; if (b > 255) b = 255; if (b < 0) b = 0; newBitmap.SetPixel(x, y, Color.FromArgb(r, g, b)); } } this.picAfter.BackgroundImage = newBitmap; } catch (Exception ex) { MessageBox.Show(ex.Message, "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information); } } private void button4_Click(object sender, EventArgs e) { //以锐化效果显示图像 try { int Height = this.pictureBox1.BackgroundImage.Height; int Width = this.pictureBox1.BackgroundImage.Width; Bitmap newBitmap = new Bitmap(Width, Height); Bitmap oldBitmap = (Bitmap)this.pictureBox1.BackgroundImage; Color pixel; //拉普拉斯模板 int[] Laplacian = { -1, -1, -1, -1, 9, -1, -1, -1, -1 }; for (int x = 1; x < Width - 1; x++) for (int y = 1; y < Height - 1; y++) { int r = 0, g = 0, b = 0; int Index = 0; for (int col = -1; col <= 1; col++) for (int row = -1; row <= 1; row++) { pixel = oldBitmap.GetPixel(x + row, y + col); r += pixel.R * Laplacian[Index]; g += pixel.G * Laplacian[Index]; b += pixel.B * Laplacian[Index]; Index++; } //处理颜色值溢出 r = r > 255 ? 255 : r; r = r < 0 ? 0 : r; g = g > 255 ? 255 : g; g = g < 0 ? 0 : g; b = b > 255 ? 255 : b; b = b < 0 ? 0 : b; newBitmap.SetPixel(x - 1, y - 1, Color.FromArgb(r, g, b)); } this.picAfter.BackgroundImage = newBitmap; } catch (Exception ex) { MessageBox.Show(ex.Message, "信息提示"); } } } }