Form1.cs
上传用户:c6x6r6
上传日期:2022-07-31
资源大小:515k
文件大小:35k
- 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;
- using System.IO;
- using System.Drawing.Imaging;
- using System.Runtime.InteropServices;
- namespace WindowsFormsApplication2
- {
- public partial class Form1 : Form
- {
- #region 本地属性
- string _currentImagePath = string.Empty;
- Bitmap _currentBmp = null;
- Bitmap _currentBmpClone = null;
-
- #endregion
- #region 初始化
- public Form1()
- {
- InitializeComponent();
- this.Text = "ImageManagemer";
- }
- private void Form1_Load(object sender, EventArgs e)
- {
- }
- #endregion
- #region 事件
- private void openToolStripMenuItem_Click(object sender, EventArgs e)
- {
- Open();
- }
- private void saveToolStripMenuItem_Click(object sender, EventArgs e)
- {
- Save();
- }
- private void fileToolStripMenuItem_Click(object sender, EventArgs e)
- {
- }
- private void reverseToolStripMenuItem_Click(object sender, EventArgs e)
- {
- }
- private void 直方图计算ToolStripMenuItem_Click(object sender, EventArgs e)
- {
- }
- private void helpToolStripMenuItem_Click(object sender, EventArgs e)
- {
-
- }
- private void 水平翻转ToolStripMenuItem_Click(object sender, EventArgs e)
- {
- if(_currentBmpClone == null) return;
- _currentBmpClone.RotateFlip(RotateFlipType.RotateNoneFlipX);
- this.pictureBox1.Image.Dispose();
- this.pictureBox1.Image = _currentBmpClone.Clone() as Image;
- //this.pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
-
- }
- private void 垂直翻转ToolStripMenuItem_Click(object sender, EventArgs e)
- {
- if (_currentBmpClone == null) return;
- _currentBmpClone.RotateFlip(RotateFlipType.RotateNoneFlipXY);
- this.pictureBox1.Image.Dispose();
- this.pictureBox1.Image = _currentBmpClone.Clone() as Image;
- //this.pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
- }
- private void saveAsToolStripMenuItem_Click(object sender, EventArgs e)
- {
- SaveAs();
- }
- private void exitToolStripMenuItem_Click(object sender, EventArgs e)
- {
- }
- private void pictureBox1_Click(object sender, EventArgs e)
- {
- }
- private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
- {
- new AboutForm().Show();
- }
- #endregion
- #region 本地方法
- void Open()
- {
- OpenFileDialog of = new OpenFileDialog();
- of.Filter = "所有图像文件 | *.bmp; *.pcx; *.png; *.jpg; *.gif;" +
- "*.tif; *.ico; *.dxf; *.cgm; *.cdr; *.wmf; *.eps; *.emf|" +
- "位图( *.bmp; *.jpg; *.png;...) | *.bmp; *.pcx; *.png; *.jpg; *.gif; *.tif; *.ico|" +
- "矢量图( *.wmf; *.eps; *.emf;...) | *.dxf; *.cgm; *.cdr; *.wmf; *.eps; *.emf";
- of.Title = "打开图像文件";
- of.ShowHelp = true;
- if (of.ShowDialog() == DialogResult.OK && string.IsNullOrEmpty(of.FileName) == false)
- {
- _currentImagePath = of.FileName;
- _currentBmp = Image.FromFile(of.FileName) as Bitmap;
- _currentBmpClone = _currentBmp.Clone() as Bitmap;
- this.pictureBox1.Image = Image.FromFile(of.FileName);
- }
- }
- void Save()
- {
- if (string.IsNullOrEmpty(_currentImagePath)) return;
- this.pictureBox1.Image.Dispose();
- _currentBmp.Dispose();
- _currentBmp = _currentBmpClone.Clone() as Bitmap;
- string temp = _currentImagePath.Substring(0, _currentImagePath.LastIndexOf('.')) +"bmp";
- _currentBmpClone.Save(temp);
- File.Delete(_currentImagePath);
- System.IO.File.Move(temp, _currentImagePath);
-
- this.pictureBox1.Image = _currentBmpClone.Clone() as Image;
-
- }
- void SaveAs()
- {
- SaveFileDialog sf = new SaveFileDialog();
- sf.FileName = "Temp.bmp";
- if (sf.ShowDialog() == DialogResult.OK)
- {
- _currentBmpClone.Save(sf.FileName);
- }
- }
- #endregion
- private void 顺时针旋转ToolStripMenuItem_Click(object sender, EventArgs e)
- {
- if (_currentBmpClone == null) return;
- _currentBmpClone.RotateFlip(RotateFlipType.Rotate90FlipNone);
- this.pictureBox1.Image.Dispose();
- this.pictureBox1.Image = _currentBmpClone.Clone() as Image;
- //this.pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
- }
- private void 逆时针旋转ToolStripMenuItem_Click(object sender, EventArgs e)
- {
- if (_currentBmpClone == null) return;
- _currentBmpClone.RotateFlip(RotateFlipType.Rotate90FlipXY);
- this.pictureBox1.Image.Dispose();
- this.pictureBox1.Image = _currentBmpClone.Clone() as Image;
- //this.pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
- }
- private void 傅里叶变换ToolStripMenuItem_Click(object sender, EventArgs e)
- {
- MyFFT fft = new MyFFT();
- Bitmap result = fft.GetFFTPic((Bitmap)pictureBox1.Image);
- pictureBox1.Image.Dispose();
- pictureBox1.Image = result;
- }
- private Bitmap edgeDetection(Bitmap curBitmap, int thresh)
- {
- Rectangle rect = new Rectangle(0, 0, curBitmap.Width, curBitmap.Height);
- BitmapData bmpData = curBitmap.LockBits(rect, ImageLockMode.ReadWrite, curBitmap.PixelFormat);
- IntPtr ptr = bmpData.Scan0;
- int bytes = curBitmap.Width * curBitmap.Height;
- byte[] rgbValues = new byte[bytes * 3];
- Marshal.Copy(ptr, rgbValues, 0, bytes * 3);
- byte[] grayValues = new byte[bytes];
- double[] tempArray = new double[bytes];
- byte[] rValues = new byte[bytes];
- byte[] gValues = new byte[bytes];
- byte[] bValues = new byte[bytes];
- for (int i = 0; i < bytes; i++)
- {
- rValues[i] = rgbValues[i * 3 + 2];
- gValues[i] = rgbValues[i * 3 + 1];
- bValues[i] = rgbValues[i * 3];
- }
- double maxV = 0.0;
- double minV = 1000.0;
- double grh, ggh, gbh;
- double grv, ggv, gbv;
- double gxx, gyy, gxy;
- double ge1, ge2;
- double theta;
- for (int i = 0; i < curBitmap.Height; i++)
- {
- for (int j = 0; j < curBitmap.Width; j++)
- {
- grh = rValues[((Math.Abs(i - 1)) % curBitmap.Height) * curBitmap.Width + ((j + 1) % curBitmap.Width)] + 2 * rValues[i * curBitmap.Width + ((j + 1) % curBitmap.Width)] + rValues[((i + 1) % curBitmap.Height) * curBitmap.Width + ((j + 1) % curBitmap.Width)] -
- rValues[((Math.Abs(i - 1)) % curBitmap.Height) * curBitmap.Width + ((Math.Abs(j - 1)) % curBitmap.Width)] - 2 * rValues[i * curBitmap.Width + ((Math.Abs(j - 1)) % curBitmap.Width)] - rValues[((i + 1) % curBitmap.Height) * curBitmap.Width + ((Math.Abs(j - 1)) % curBitmap.Width)];
- ggh = gValues[((Math.Abs(i - 1)) % curBitmap.Height) * curBitmap.Width + ((j + 1) % curBitmap.Width)] + 2 * gValues[i * curBitmap.Width + ((j + 1) % curBitmap.Width)] + gValues[((i + 1) % curBitmap.Height) * curBitmap.Width + ((j + 1) % curBitmap.Width)] -
- gValues[((Math.Abs(i - 1)) % curBitmap.Height) * curBitmap.Width + ((Math.Abs(j - 1)) % curBitmap.Width)] - 2 * gValues[i * curBitmap.Width + ((Math.Abs(j - 1)) % curBitmap.Width)] - gValues[((i + 1) % curBitmap.Height) * curBitmap.Width + ((Math.Abs(j - 1)) % curBitmap.Width)];
- gbh = bValues[((Math.Abs(i - 1)) % curBitmap.Height) * curBitmap.Width + ((j + 1) % curBitmap.Width)] + 2 * bValues[i * curBitmap.Width + ((j + 1) % curBitmap.Width)] + bValues[((i + 1) % curBitmap.Height) * curBitmap.Width + ((j + 1) % curBitmap.Width)] -
- bValues[((Math.Abs(i - 1)) % curBitmap.Height) * curBitmap.Width + ((Math.Abs(j - 1)) % curBitmap.Width)] - 2 * bValues[i * curBitmap.Width + ((Math.Abs(j - 1)) % curBitmap.Width)] - bValues[((i + 1) % curBitmap.Height) * curBitmap.Width + ((Math.Abs(j - 1)) % curBitmap.Width)];
- grv = rValues[((Math.Abs(i - 1)) % curBitmap.Height) * curBitmap.Width + ((Math.Abs(j - 1)) % curBitmap.Width)] + 2 * rValues[((Math.Abs(i - 1)) % curBitmap.Height) * curBitmap.Width + j] + rValues[((Math.Abs(i - 1)) % curBitmap.Height) * curBitmap.Width + ((j + 1) % curBitmap.Width)] -
- rValues[((i + 1) % curBitmap.Height) * curBitmap.Width + ((Math.Abs(j - 1)) % curBitmap.Width)] - 2 * rValues[((i + 1) % curBitmap.Height) * curBitmap.Width + j] - rValues[((i + 1) % curBitmap.Height) * curBitmap.Width + ((j + 1) % curBitmap.Width)];
- ggv = gValues[((Math.Abs(i - 1)) % curBitmap.Height) * curBitmap.Width + ((Math.Abs(j - 1)) % curBitmap.Width)] + 2 * gValues[((Math.Abs(i - 1)) % curBitmap.Height) * curBitmap.Width + j] + gValues[((Math.Abs(i - 1)) % curBitmap.Height) * curBitmap.Width + ((j + 1) % curBitmap.Width)] -
- gValues[((i + 1) % curBitmap.Height) * curBitmap.Width + ((Math.Abs(j - 1)) % curBitmap.Width)] - 2 * gValues[((i + 1) % curBitmap.Height) * curBitmap.Width + j] - gValues[((i + 1) % curBitmap.Height) * curBitmap.Width + ((j + 1) % curBitmap.Width)];
- gbv = bValues[((Math.Abs(i - 1)) % curBitmap.Height) * curBitmap.Width + ((Math.Abs(j - 1)) % curBitmap.Width)] + 2 * bValues[((Math.Abs(i - 1)) % curBitmap.Height) * curBitmap.Width + j] + bValues[((Math.Abs(i - 1)) % curBitmap.Height) * curBitmap.Width + ((j + 1) % curBitmap.Width)] -
- bValues[((i + 1) % curBitmap.Height) * curBitmap.Width + ((Math.Abs(j - 1)) % curBitmap.Width)] - 2 * bValues[((i + 1) % curBitmap.Height) * curBitmap.Width + j] - bValues[((i + 1) % curBitmap.Height) * curBitmap.Width + ((j + 1) % curBitmap.Width)];
- gxx = grh * grh + ggh * ggh + gbh * gbh;
- gyy = grv * grv + ggv * ggv + gbv * gbv;
- gxy = grh * grv + ggh * ggv + gbh * gbv;
- theta = Math.Atan(2 * gxy / (gxx - gyy + 0.000000001)) / 2;
- ge1 = ((gxx + gyy) + (gxx - gyy) * Math.Cos(2 * theta) + 2 * gxy * Math.Sin(2 * theta)) / 2;
- theta = theta + Math.PI / 2;
- ge2 = ((gxx + gyy) + (gxx - gyy) * Math.Cos(2 * theta) + 2 * gxy * Math.Sin(2 * theta)) / 2;
- tempArray[i * curBitmap.Width + j] = Math.Max(Math.Sqrt(ge1), Math.Sqrt(ge2));
- if (tempArray[i * curBitmap.Width + j] > maxV)
- maxV = tempArray[i * curBitmap.Width + j];
- else if (tempArray[i * curBitmap.Width + j] < minV)
- minV = tempArray[i * curBitmap.Width + j];
- }
- }
- for (int i = 0; i < bytes; i++)
- {
- grayValues[i] = (byte)((tempArray[i] - minV) * 255 / (maxV - minV));
- }
- if (thresh != 0)
- {
- for (int i = 0; i < bytes; i++)
- {
- if (grayValues[i] > thresh)
- grayValues[i] = 255;
- else
- grayValues[i] = 0;
- }
- }
- curBitmap = new Bitmap(curBitmap.Width, curBitmap.Height, PixelFormat.Format8bppIndexed);
- ColorPalette cp = curBitmap.Palette;
- for (int i = 0; i < 256; i++)
- {
- cp.Entries[i] = Color.FromArgb(i, i, i);
- }
- curBitmap.Palette = cp;
- bmpData = curBitmap.LockBits(rect, ImageLockMode.ReadWrite, curBitmap.PixelFormat);
- ptr = bmpData.Scan0;
- System.Runtime.InteropServices.Marshal.Copy(grayValues, 0, ptr, bytes);
- curBitmap.UnlockBits(bmpData);
- return curBitmap;
- }
- private void 边缘检测ToolStripMenuItem_Click(object sender, EventArgs e)
- {
- }
- private Bitmap LaplEdgeDetection(Bitmap src)
- {
- int w = src.Width;
- int h = src.Height;
- Bitmap newBmp = new Bitmap(w, h);
- Color oColor, gColor;
- float brightness;
- int gRGB;
- //gray
- for (int x = 0; x < w; x++)
- {
- for (int y = 0; y < h; y++)
- {
- oColor = src.GetPixel(x, y);
- brightness = oColor.GetBrightness();
- gRGB = (int)(brightness * 255);
- gColor = Color.FromArgb(gRGB, gRGB, gRGB);
- src.SetPixel(x, y, gColor);
- }
- }
- Color pixel;
- int[] Laplacian = { -1, -1, -1, -1, 8, -1, -1, -1, -1 };
- for (int x = 1; x < w - 1; x++)
- {
- for (int y = 1; y < h - 1; y++)
- {
- int r = 0, g = 0, b = 0;
- int index = 0;
- for (int col = -1; col < 2; col++)
- {
- for (int row = -1; row < 2; row++)
- {
- pixel = src.GetPixel(x + row, y + col);
- r += pixel.R * Laplacian[index];
- g += pixel.G * Laplacian[index];
- b += pixel.B * Laplacian[index];
- index++;
- }
- }
- //pixel scale
- 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;
- newBmp.SetPixel(x, y, Color.FromArgb(r, g, b));
- }
- }
- src.Dispose();
- return newBmp;
- }
- private Bitmap ConvertToGray(Bitmap src)
- {
- int w = src.Width;
- int h = src.Height;
- Rectangle rect = new Rectangle(0, 0, w, h);
- BitmapData srcData = src.LockBits(rect, ImageLockMode.ReadOnly, src.PixelFormat);
- unsafe
- {
- byte* ptr = (byte*)srcData.Scan0;
- for (int y = 0; y < h; y++)
- {
- for (int x = 0; x < w; x++)
- {
- int gray = (int)(ptr[0] * 0.3 + ptr[1] * 0.59 + ptr[2] * 0.11);
- ptr[0] = (byte)gray;
- ptr[1] = (byte)gray;
- ptr[2] = (byte)gray;
- ptr += 3;
- }
- ptr += srcData.Stride - 3 * w;
- }
- }
- src.UnlockBits(srcData);
- return src;
- }
- private void 阈值分割ToolStripMenuItem_Click(object sender, EventArgs e)
- {
-
- Bitmap src = ConvertToGray((Bitmap)pictureBox1.Image);
- int[] histogram = new int[256];
- byte maxGray = 0;
- byte minGray = 255;
- int w = src.Width;
- int h = src.Height;
- Rectangle rect = new Rectangle(0, 0, w, h);
- BitmapData srcData = src.LockBits(rect, ImageLockMode.ReadWrite, src.PixelFormat);
- unsafe
- {
- byte* ptr = (byte*)srcData.Scan0;
- byte temp;
- for (int y = 0; y < h; y++)
- {
- for (int x = 0; x < w; x++)
- {
- temp = ptr[0];
- histogram[temp]++;
- if (temp > maxGray)
- maxGray = temp;
- if (temp < minGray)
- minGray = temp;
- ptr += 3;
- }
- ptr += srcData.Stride - 3 * w;
- }
- double sum = 0;
- double w1 = 0, w2 = 0;
- int numerator = 0;
- for (int i = minGray; i <= maxGray; i ++ )
- {
- sum += i * histogram[i];
- }
- double mu1, mu2;
- double sigma;
- int count = w * h;
- double tempMax = 0;
- byte T = 0 ;
- for (int i = minGray; i <= maxGray; i++ )
- {
- w1 += histogram[i];
- numerator += i * histogram[i];
- mu1 = numerator / w1;
- w2 = count - w1;
- mu2 = (sum - numerator) / w2;
- sigma = w1 * w2 * (mu1 - mu2) * (mu1 - mu2);
- if (sigma > tempMax)
- {
- tempMax = sigma;
- T = Convert.ToByte(i);
- }
- }
- ptr = (byte*)srcData.Scan0;
- for (int y = 0; y < h; y ++ )
- {
- for (int x = 0; x < w; x++ )
- {
- if (ptr[0] < T)
- {
- ptr[0] = 0;
- ptr[1] = 0;
- ptr[2] = 0;
- }
- else
- {
- ptr[0] = 255;
- ptr[1] = 255;
- ptr[2] = 255;
- }
- ptr += 3;
- }
- ptr += srcData.Stride - 3 * w;
- }
- }
- src.UnlockBits(srcData);
- pictureBox1.Image = src;
- }
- private Bitmap ruihua(Bitmap bm)
- {
- Color c1 = new Color();
- Color c2 = new Color();
- Bitmap box1 = bm;
- Bitmap box2 = bm;
- int rr, gg, bb;
- int r1, g1, b1;
- int r2, g2, b2;
- for (int i = 1; i < bm.Width - 1; ++i)
- {
- for (int j = 1; j < bm.Height - 1; ++j)
- {
- rr = gg = bb = 0;
- c1 = box1.GetPixel(i, j);
- r1 = c1.R;
- g1 = c1.G;
- b1 = c1.B;
- c2 = box1.GetPixel(i - 1, j - 1);
- r2 = c2.R;
- g2 = c2.G;
- b2 = c2.B;
- rr = r1 + Math.Abs((r1 - r2) / 4);
- gg = g1 + Math.Abs((g1 - g2) / 4);
- bb = b1 + Math.Abs((b1 - b2) / 4);
- if (rr < 0) rr = 0;
- if (rr > 255) rr = 255;
- if (gg < 0) gg = 0;
- if (gg > 255) gg = 255;
- if (bb < 0) bb = 0;
- if (bb > 255) bb = 255;
- Color c3 = Color.FromArgb(rr, gg, bb); //用formArgb方法由颜色分量值创建color结构
- box2.SetPixel(i, j, c3);
- }
- }
- return box2;
- }
- private void 锐化ToolStripMenuItem_Click(object sender, EventArgs e)
- {
- pictureBox1.Image = ruihua((Bitmap)pictureBox1.Image);
- }
- private void 直方图ToolStripMenuItem1_Click(object sender, EventArgs e)
- {
- if (pictureBox1.Image != null)
- {
- histForm histogram = new histForm((Bitmap)pictureBox1.Image);
- histogram.ShowDialog();
- }
- }
- private void 直方图均衡ToolStripMenuItem1_Click(object sender, EventArgs e)
- {
- Bitmap curBitmap = (Bitmap)pictureBox1.Image;
- if (curBitmap != null)
- {
- Rectangle rect = new Rectangle(0, 0, curBitmap.Width, curBitmap.Height);
- BitmapData bmpData = curBitmap.LockBits(rect, ImageLockMode.ReadWrite, curBitmap.PixelFormat);
- IntPtr ptr = bmpData.Scan0;
- int bytes = curBitmap.Width * curBitmap.Height;
- byte[] rgbValues = new byte[bytes * 3];
- Marshal.Copy(ptr, rgbValues, 0, bytes * 3);
- int[,] countPixel = new int[3, 256];
- int rTemp, gTemp, bTemp;
- for (int i = 0; i < bytes; i++)
- {
- rTemp = ++countPixel[0, rgbValues[i * 3 + 2]];
- gTemp = ++countPixel[1, rgbValues[i * 3 + 1]];
- bTemp = ++countPixel[2, rgbValues[i * 3]];
- }
- int[,] tempArray = new int[3, 256];
- byte[,] pixelMap = new byte[3, 256];
- byte temp;
- for (int i = 0; i < 256; i++)
- {
- if (i != 0)
- {
- tempArray[ 0 , i] = tempArray[0 , i - 1] + countPixel[0 , i];
- tempArray[1, i] = tempArray[1, i - 1] + countPixel[1, i];
- tempArray[2, i] = tempArray[2, i - 1] + countPixel[2, i];
- }
- else
- {
- tempArray[0 , 0] = countPixel[0 , 0];
- tempArray[1, 0] = countPixel[1, 0];
- tempArray[2, 0] = countPixel[2, 0];
- }
- pixelMap[0 ,i] = (byte)(255.0 * tempArray[0 , i] / bytes + 0.5);
- pixelMap[1, i] = (byte)(255.0 * tempArray[1, i] / bytes + 0.5);
- pixelMap[2, i] = (byte)(255.0 * tempArray[2, i] / bytes + 0.5);
- }
- for (int i = 0; i < bytes; i++)
- {
- temp = rgbValues[i * 3 + 2];
- rgbValues[i * 3 + 2] = pixelMap[ 0 , temp];
- temp = rgbValues[i * 3 + 1];
- rgbValues[i * 3 + 1] = pixelMap[ 1 , temp];
- temp = rgbValues[i * 3];
- rgbValues[i * 3] = pixelMap[ 2 , temp];
- }
- Marshal.Copy(rgbValues, 0, ptr, bytes * 3);
- curBitmap.UnlockBits(bmpData);
- Invalidate();
- }
- }
- private void 柔化ToolStripMenuItem_Click(object sender, EventArgs e)
- {
- Bitmap result = rouhua((Bitmap)pictureBox1.Image);
- pictureBox1.Image.Dispose();
- pictureBox1.Image = result;
- }
- private Bitmap rouhua(Bitmap src)
- {
- int w = src.Width;
- int h = src.Height;
- Bitmap newBmp = new Bitmap(w, h);
- Color pixel;
- int[] Guassin = { 1, 2, 1, 2, 4, 2, 1, 2, 1 };
- for (int x = 1; x < w - 1; x++)
- {
- for (int y = 1; y < h - 1; y++)
- {
- int r = 0, g = 0, b = 0;
- int index = 0;
- for (int col = -1; col < 2; col++)
- {
- for (int row = -1; row < 2; row++)
- {
- pixel = src.GetPixel(x + row, y + col);
- r += pixel.R * Guassin[index];
- g += pixel.G * Guassin[index];
- b += pixel.B * Guassin[index];
- index++;
- }
- }
- r = r / 16;
- g = g / 16;
- b = b / 16;
- //pixel scale
- 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;
- newBmp.SetPixel(x, y, Color.FromArgb(r, g, b));
- }
- }
- return newBmp;
- }
- private void 拉普拉斯ToolStripMenuItem_Click(object sender, EventArgs e)
- {
- Bitmap result = LaplEdgeDetection((Bitmap)pictureBox1.Image);
- pictureBox1.Image.Dispose();
- pictureBox1.Image = result;
- }
- private void sobeToolStripMenuItem_Click(object sender, EventArgs e)
- {
- Bitmap result = edgeDetection((Bitmap)pictureBox1.Image , 40);
- pictureBox1.Image.Dispose();
- pictureBox1.Image = result;
- }
- private void 椒盐噪声ToolStripMenuItem_Click(object sender, EventArgs e)
- {
- //加椒盐噪声
- if (pictureBox1.Image == null)
- {
- return;
- }
- int Height = this.pictureBox1.Image.Height;
- int Width = this.pictureBox1.Image.Width;
- Bitmap oldbitmap = (Bitmap)this.pictureBox1.Image;
- Bitmap newbitmap = new Bitmap(Width, Height);
- Color pixel;
- for (int x = 0; x < Width; x++)
- {
- for (int y = 0; y < Height; y++)
- {
- int r, g, b;
- pixel = oldbitmap.GetPixel(x, y);
- r = pixel.R;
- g = pixel.G;
- b = pixel.B;
- newbitmap.SetPixel(x, y, Color.FromArgb(r, g, b));
- }
- }
- for (int z = 0; z < 1000; z++)
- {
- Random random1 = new Random(z);
- int a = random1.Next(Width);
- Random random2 = new Random(a * unchecked((int)DateTime.Now.Ticks));
- int b = random2.Next(Height);
- Random random3 = new Random(b * unchecked((int)DateTime.Now.Ticks));
- int c = random3.Next(2);
- newbitmap.SetPixel(a, b, Color.FromArgb(c * 255, c * 255, c * 255));
- }
- this.pictureBox1.Image = newbitmap;
- }
- private void 高斯噪声ToolStripMenuItem_Click(object sender, EventArgs e)
- {
- //加高斯噪声
- if (pictureBox1.Image == null)
- {
- return;
- }
- int Height = this.pictureBox1.Image.Height;
- int Width = this.pictureBox1.Image.Width;
- Bitmap oldbitmap = (Bitmap)this.pictureBox1.Image;
- Bitmap newbitmap = new Bitmap(Width, Height);
- Color pixel;
- double u, v;
- for (int x = 0; x < Width; x++)
- {
- for (int y = 0; y < Height; y++)
- {
- Random random1 = new Random((x + 1) * (1 + y) * unchecked((int)DateTime.Now.Ticks));
- u = random1.NextDouble();
- Random random2 = new Random((int)u * unchecked((int)DateTime.Now.Ticks));
- v = random2.NextDouble();
- double gs = Math.Pow(-2 * Math.Log(u, Math.E), 0.5) * Math.Sin(2 * Math.PI * v);
- int r, g, b;
- int t = (int)(gs * 50 + 128);
- if (t < 0) t = 0;
- if (t > 255) t = 255;
- pixel = oldbitmap.GetPixel(x, y);
- r = pixel.R;
- g = pixel.G;
- b = pixel.B;
- newbitmap.SetPixel(x, y, Color.FromArgb((int)(r + t) / 2, (int)(g + t) / 2, (int)(b + t) / 2));
- }
- }
- this.pictureBox1.Image = newbitmap;
- }
- public Bitmap getNewBitmap(Bitmap old)
- {
- int Height = old.Height;
- int Width = old.Width;
- Bitmap newbitmap = new Bitmap(Width + 2, Height + 2);
- Color pixel;
- for (int x = 0; x < Width + 2; x++)
- {
- for (int y = 0; y < Height + 2; y++)
- {
- int r, g, b;
- if (x == 0 && y == 0)
- {
- pixel = old.GetPixel(x, y);
- r = pixel.R;
- g = pixel.G;
- b = pixel.B;
- }
- else
- {
- if (x == 0 && y == Height + 1)
- {
- pixel = old.GetPixel(x, y - 2);
- r = pixel.R;
- g = pixel.G;
- b = pixel.B;
- }
- else
- {
- if (x == Width + 1 && y == 0)
- {
- pixel = old.GetPixel(x - 2, y);
- r = pixel.R;
- g = pixel.G;
- b = pixel.B;
- }
- else
- {
- if (x == Width + 1 && y == Height + 1)
- {
- pixel = old.GetPixel(x - 2, y - 2);
- r = pixel.R;
- g = pixel.G;
- b = pixel.B;
- }
- else
- {
- if (x == 0)
- {
- pixel = old.GetPixel(x, y - 1);
- r = pixel.R;
- g = pixel.G;
- b = pixel.B;
- }
- else
- {
- if (x == Width + 1)
- {
- pixel = old.GetPixel(x - 2, y - 1);
- r = pixel.R;
- g = pixel.G;
- b = pixel.B;
- }
- else
- {
- if (y == 0)
- {
- pixel = old.GetPixel(x - 1, y);
- r = pixel.R;
- g = pixel.G;
- b = pixel.B;
- }
- else
- {
- if (y == Height + 1)
- {
- pixel = old.GetPixel(x - 1, y - 2);
- r = pixel.R;
- g = pixel.G;
- b = pixel.B;
- }
- else
- {
- pixel = old.GetPixel(x - 1, y - 1);
- r = pixel.R;
- g = pixel.G;
- b = pixel.B;
- }
- }
- }
- }
- }
- }
- }
- }
- newbitmap.SetPixel(x, y, Color.FromArgb(r, g, b));
- }
- }
- return newbitmap;
- }
- private void 中值滤波ToolStripMenuItem_Click(object sender, EventArgs e)
- {
- //中值滤波
- if (pictureBox1.Image == null)
- {
- return;
- }
- int Height = this.pictureBox1.Image.Height + 2;
- int Width = this.pictureBox1.Image.Width + 2;
- Bitmap newbitmap2 = new Bitmap(Width - 2, Height - 2);
- Bitmap oldbitmap = (Bitmap)this.pictureBox1.Image;
- int[] r = new int[15];
- int[] g = new int[15];
- int[] b = new int[15];
- Bitmap newbitmap = getNewBitmap(oldbitmap);
- Color[] pixel = new Color[9];
- for (int x = 1; x < Width - 1; x++)
- {
- for (int y = 1; y < Height - 1; y++)
- {
- pixel[0] = newbitmap.GetPixel(x - 1, y - 1);
- pixel[1] = newbitmap.GetPixel(x, y - 1);
- pixel[2] = newbitmap.GetPixel(x + 1, y - 1);
- pixel[3] = newbitmap.GetPixel(x - 1, y);
- pixel[4] = newbitmap.GetPixel(x, y);
- pixel[5] = newbitmap.GetPixel(x + 1, y);
- pixel[6] = newbitmap.GetPixel(x - 1, y + 1);
- pixel[7] = newbitmap.GetPixel(x, y + 1);
- pixel[8] = newbitmap.GetPixel(x + 1, y + 1);
- for (int i = 0; i < 9; i++)
- {
- r[i] = pixel[i].R;
- g[i] = pixel[i].G;
- b[i] = pixel[i].B;
- }
- for (int j = 0; j < 4; j++)
- {
- r[9 + j] = pixel[j * 2 + 1].R;
- g[9 + j] = pixel[j * 2 + 1].G;
- b[9 + j] = pixel[j * 2 + 1].B;
- }
- r[14] = pixel[4].R;
- g[14] = pixel[4].G;
- b[14] = pixel[4].B;
- Array.Sort(r);
- Array.Sort(g);
- Array.Sort(b);
- newbitmap2.SetPixel(x - 1, y - 1, Color.FromArgb(r[7], g[7], b[7]));
- }
- }
- this.pictureBox1.Image = newbitmap2;
- }
- private void closeToolStripMenuItem_Click(object sender, EventArgs e)
- {
- pictureBox1.Image.Dispose();
- pictureBox1.Image = null;
- }
- }
- }