您好,登錄后才能下訂單哦!
這期內容當中小編將會給大家帶來有關C#怎么實現一個數字圖像處理程序,文章內容豐富且以專業的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
1.直方圖均衡化
2.算子銳化
3.空域增強
一、要達到的目的和效果
1.打開,保存圖片;
2.獲取圖像灰度值,圖像坐標;
3.進行線性變換,直方圖均衡化處理;
4.直方圖變換增強,以及各種濾波處理;
5.圖像銳化(Kirsch,Laplace,sobel等算子)。
二、編程環境及語言
C#-WindowsForm-VS2015
三、圖標
最近發現了一個完全免費的矢量圖標網站阿里媽媽iconfont,超級好用。
當然也可以自己動手畫一個
四、創建窗體
1.先建一個C#Windows窗體應用程序,設置好保存路徑和項目名稱;
2.打開工具箱,找到menuscript,加到窗體中,依次填寫菜單以及子菜單的名稱,菜單里將完成主要的圖像處理操作;
3.因為要顯示處理前后的圖片,所以再添加兩個picturebox控件,可以設置停靠模式為stretchImage;再加兩個groupbox,每個groupbox里添加label和textbox控件,用來顯示圖像灰度值及坐標,這樣窗體基本搭建完成,還是挺簡單的。
五、主要代碼
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Drawing.Imaging; using System.Linq; using System.Text; using System.Windows.Forms; namespace text1 { public partial class ImageEnhancement : Form { public ImageEnhancement() { InitializeComponent(); } Bitmap bitmap; int iw, ih; //打開文件 private void 打開ToolStripMenuItem_Click(object sender, EventArgs e) { pictureBox1.Image = null;//先設置兩個picturebox為空 pictureBox2.Image = null; //使用 OpenFileDialog類打開圖片 OpenFileDialog open = new OpenFileDialog(); open.Filter = "圖像文件(*.bmp;*.jpg;*gif;*png;*.tif;*.wmf)|" + "*.bmp;*jpg;*gif;*png;*.tif;*.wmf"; if (open.ShowDialog() == DialogResult.OK) { try { bitmap = (Bitmap)Image.FromFile(open.FileName); } catch (Exception exp) { MessageBox.Show(exp.Message); } pictureBox1.Refresh(); pictureBox1.Image = bitmap; label6.Text = "原圖"; iw = bitmap.Width; ih = bitmap.Height; } } //保存文件 private void 保存ToolStripMenuItem_Click(object sender, EventArgs e) { string str; SaveFileDialog saveFileDialog1 = new SaveFileDialog(); saveFileDialog1.Filter = "圖像文件(*.BMP)|*.BMP|All File(*.*)|*.*"; saveFileDialog1.ShowDialog(); str = saveFileDialog1.FileName; pictureBox2.Image.Save(str); } //退出 private void 退出ToolStripMenuItem_Click(object sender, EventArgs e) { this.Close(); } private void label5_Click(object sender, EventArgs e) { } //讀取灰度值及坐標 private void pictureBox1_MouseDown(object sender, MouseEventArgs e) { Color pointRGB = bitmap.GetPixel(e.X, e.Y); textBox1.Text = pointRGB.R.ToString(); textBox2.Text = pointRGB.G.ToString(); textBox3.Text = pointRGB.B.ToString(); textBox4.Text = e.X.ToString(); textBox5.Text = e.Y.ToString(); int a = int.Parse(textBox1.Text); } //線性變換部分 private void linearPO_Click(object sender, EventArgs e) { if (bitmap != null) { linearPOForm linearForm = new linearPOForm(); if (linearForm.ShowDialog() == DialogResult.OK) { Rectangle rect = new Rectangle(0, 0, bitmap.Width, bitmap.Height); System.Drawing.Imaging.BitmapData bmpData = bitmap.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite, bitmap.PixelFormat); IntPtr ptr = bmpData.Scan0; //int bytes = bitmap.Width *; } } } private void textBox4_TextChanged(object sender, EventArgs e) { } private void label3_Click(object sender, EventArgs e) { } //對比度擴展 private void 對比度擴展ToolStripMenuItem_Click(object sender, EventArgs e) { if (bitmap != null) { strechDialog dialog = new strechDialog(); if (dialog.ShowDialog() == DialogResult.OK) { this.Text = " 圖像增強 對比度擴展 "; Bitmap bm = new Bitmap(pictureBox1.Image); int x1 = Convert.ToInt32(dialog.getX01); int y1 = Convert.ToInt32(dialog.getY01); int x2 = Convert.ToInt32(dialog.getX02); int y2 = Convert.ToInt32(dialog.getY02); //計算灰度映射表 int[] pixMap = pixelsMap(x1, y1, x2, y2); //線性拉伸 bm = stretch(bm, pixMap, iw, ih); pictureBox2.Refresh(); pictureBox2.Image = bm; label7.Text = "對比度擴展結果"; } } } //計算灰度映射表 public int[] pixelsMap(int x1, int y1, int x2, int y2) { int[] pMap = new int[256]; //映射表 if (x1 > 0) { double k1 = y1 / x1; //第1段斜率k1 //按第1段斜率k1線性變換 for (int i = 0; i <= x1; i++) pMap[i] = (int)(k1 * i); } double k2 = (y2 - y1) / (x2 - x1); //第2段斜率k2 //按第2段斜率k2線性變換 for (int i = x1 + 1; i <= x2; i++) if (x2 != x1) pMap[i] = y1 + (int)(k2 * (i - x1)); else pMap[i] = y1; if (x2 < 255) { double k3 = (255 - y2) / (255 - x2);//第2段斜率k2 //按第3段斜率k3線性變換 for (int i = x2 + 1; i < 256; i++) pMap[i] = y2 + (int)(k3 * (i - x2)); } return pMap; } //對比度擴展函數 public Bitmap stretch(Bitmap bm, int[] map, int iw, int ih) { Color c = new Color(); int r, g, b; for (int j = 0; j < ih; j++) { for (int i = 0; i < iw; i++) { c = bm.GetPixel(i, j); r = map[c.R]; g = map[c.G]; b = map[c.B]; 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; bm.SetPixel(i, j, Color.FromArgb(r, g, b)); } } return bm; } private void 直方圖均衡化ToolStripMenuItem_Click(object sender, EventArgs e) { if (bitmap != null) { this.Text = " 圖像增強 直方圖均衡化"; Bitmap bm = new Bitmap(pictureBox1.Image); //獲取直方圖 int[] hist = gethist(bm, iw, ih); //直方圖均勻化 bm = histequal(bm, hist, iw, ih); pictureBox2.Refresh(); pictureBox2.Image = bm; label7.Text = "直方圖均衡化結果"; flag = true; } } bool flag = false; //直方圖均衡化標志 //顯示直方圖 private void 顯示直方圖ToolStripMenuItem_Click(object sender, EventArgs e) { if (flag) { Bitmap b1 = new Bitmap(pictureBox1.Image); Bitmap b2 = new Bitmap(pictureBox2.Image); int[] hist1 = gethist(b1, iw, ih); int[] hist2 = gethist(b2, iw, ih); drawHist(hist1, hist2); } } //獲取直方圖 public int[] gethist(Bitmap bm, int iw, int ih) { int[] h = new int[256]; for (int j = 0; j < ih; j++) { for (int i = 0; i < iw; i++) { int grey = (bm.GetPixel(i, j)).R; h[grey]++; } } return h; } //直方圖均衡化 public Bitmap histequal(Bitmap bm, int[] hist, int iw, int ih) { Color c = new Color(); double p = (double)255 / (iw * ih); double[] sum = new double[256]; int[] outg = new int[256]; int r, g, b; sum[0] = hist[0]; for (int i = 1; i < 256; i++) sum[i] = sum[i - 1] + hist[i]; //灰度變換:i-->outg[i] for (int i = 0; i < 256; i++) outg[i] = (int)(p * sum[i]); for (int j = 0; j < ih; j++) { for (int i = 0; i < iw; i++) { r = (bm.GetPixel(i, j)).R; g = (bm.GetPixel(i, j)).G; b = (bm.GetPixel(i, j)).B; c = Color.FromArgb(outg[r], outg[g], outg[b]); bm.SetPixel(i, j, c); } } return bm; } public void drawHist(int[] h2, int[] h3) { //畫原圖直方圖------------------------------------------ Graphics g = pictureBox1.CreateGraphics(); Pen pen1 = new Pen(Color.Blue); g.Clear(this.BackColor); //找出最大的數,進行標準化. int maxn = h2[0]; for (int i = 1; i < 256; i++) if (maxn < h2[i]) maxn = h2[i]; for (int i = 0; i < 256; i++) h2[i] = h2[i] * 250 / maxn; g.FillRectangle(new SolidBrush(Color.White), 0, 0, 255, 255); pen1.Color = Color.Red; for (int i = 0; i < 256; i++) g.DrawLine(pen1, i, 255, i, 255 - h2[i]); g.DrawString("" + maxn, this.Font, new SolidBrush(Color.Blue), 0, 0); label6.Text = "原圖直方圖"; //畫均衡化后直方圖------------------------------------------ g = pictureBox2.CreateGraphics(); pen1 = new Pen(Color.Blue); g.Clear(this.BackColor); //找出最大的數,進行標準化. maxn = h3[0]; for (int i = 1; i < 256; i++) if (maxn < h3[i]) maxn = h3[i]; for (int i = 0; i < 256; i++) h3[i] = h3[i] * 250 / maxn; g.FillRectangle(new SolidBrush(Color.White), 0, 0, 255, 255); pen1.Color = Color.Red; for (int i = 0; i < 256; i++) g.DrawLine(pen1, i, 255, i, 255 - h3[i]); g.DrawString("" + maxn, this.Font, new SolidBrush(Color.Blue), 0, 0); label7.Text = "均衡化后直方圖"; flag = false; } private void 閾值濾波ToolStripMenuItem_Click(object sender, EventArgs e) { if (bitmap != null) { this.Text = "圖像增強 閾值濾波"; Bitmap bm = new Bitmap(pictureBox1.Image); //閾值濾波 bm = threshold(bm, iw, ih); pictureBox2.Refresh(); pictureBox2.Image = bm; label7.Text = "閾值濾波結果"; } } //3×3閾值濾波 public Bitmap threshold(Bitmap bm, int iw, int ih) { Bitmap obm = new Bitmap(pictureBox1.Image); int avr, //灰度平均 sum, //灰度和 num = 0, //計數器 nT = 4, //計數器閾值 T = 50; //閾值 int pij, pkl, //(i,j),(i+k,j+l)處灰度值 err; //誤差 for (int j = 1; j < ih - 1; j++) { for (int i = 1; i < iw - 1; i++) { //取3×3塊的9個象素, 求和 sum = 0; for (int k = -1; k < 2; k++) { for (int l = -1; l < 2; l++) { if ((k != 0) || (l != 0)) { pkl = (bm.GetPixel(i + k, j + l)).R; pij = (bm.GetPixel(i, j)).R; err = Math.Abs(pkl - pij); sum = sum + pkl; if (err > T) num++; } } } avr = (int)(sum / 8.0f); //平均值 if (num > nT) obm.SetPixel(i, j, Color.FromArgb(avr, avr, avr)); } } return obm; } private void 均值濾波ToolStripMenuItem_Click(object sender, EventArgs e) { if (bitmap != null) { this.Text = "數字圖像處理"; Bitmap bm = new Bitmap(pictureBox1.Image); bm = average(bm, iw, ih); pictureBox2.Refresh(); pictureBox2.Image = bm; label7.Text = "均值濾波結果"; } } //均值濾波 public Bitmap average(Bitmap bm, int iw, int ih) { Bitmap obm = new Bitmap(pictureBox1.Image); for (int j = 1; j < ih - 1; j++) { for (int i = 1; i < iw - 1; i++) { int avr; int avr1; int avr2; int sum = 0; int sum1 = 0; int sum2 = 0; for (int k = -1; k <= 1; k++) { for (int l = -1; l <= 1; l++) { sum = sum + (bm.GetPixel(i + k, j + 1).R); sum1 = sum1 + (bm.GetPixel(i + k, j + 1).G); sum2 = sum2 + (bm.GetPixel(i + k, j + 1).B); } } avr = (int)(sum / 9.0f); avr1 = (int)(sum1 / 9.0f); avr2 = (int)(sum2 / 9.0f); obm.SetPixel(i, j, Color.FromArgb(avr, avr1, avr2)); } } return obm; } private void 中值濾波ToolStripMenuItem_Click(object sender, EventArgs e) { if (bitmap != null) { this.Text = "圖像增強 中值濾波"; Bitmap bm = new Bitmap(pictureBox1.Image); int num =3; //中值濾波 bm = median(bm, iw, ih, num); pictureBox2.Refresh(); pictureBox2.Image = bm; label2.Location = new Point(370, 280); if (num == 1) label7.Text = "1X5窗口濾波結果"; else if (num == 2) label7.Text = "5X1窗口濾波結果"; else if (num == 3) label7.Text = "5X5窗口濾波結果"; } } //中值濾波方法 public Bitmap median(Bitmap bm, int iw, int ih, int n) { Bitmap obm = new Bitmap(pictureBox1.Image); for (int j = 2; j < ih - 2; j++) { int[] dt; int[] dt1; int[] dt2; for (int i = 2; i < iw - 2; i++) { int m = 0, r = 0, r1 = 0, r2 = 0, a = 0, b = 0; if (n == 3) { dt = new int[25]; dt1 = new int[25]; dt2 = new int[25]; //取5×5塊的25個象素 for (int k = -2; k < 3; k++) { for (int l = -2; l < 3; l++) { //取(i+k,j+l)處的象素,賦于數組dt dt[m] = (bm.GetPixel(i + k, j + l)).R; dt1[a] = (bm.GetPixel(i + k, j + l)).G; dt2[b] = (bm.GetPixel(i + k, j + l)).B; m++; a++; b++; } } //冒泡排序,輸出中值 r = median_sorter(dt, 25); //中值 r1 = median_sorter(dt1, 25); r2 = median_sorter(dt2, 25); } else if (n == 1) { dt = new int[5]; //取1×5窗口5個像素 dt[0] = (bm.GetPixel(i, j - 2)).R; dt[1] = (bm.GetPixel(i, j - 1)).R; dt[2] = (bm.GetPixel(i, j)).R; dt[3] = (bm.GetPixel(i, j + 1)).R; dt[4] = (bm.GetPixel(i, j + 2)).R; r = median_sorter(dt, 5); //中值 dt1 = new int[5]; //取1×5窗口5個像素 dt1[0] = (bm.GetPixel(i, j - 2)).G; dt1[1] = (bm.GetPixel(i, j - 1)).G; dt1[2] = (bm.GetPixel(i, j)).G; dt1[3] = (bm.GetPixel(i, j + 1)).G; dt1[4] = (bm.GetPixel(i, j + 2)).G; r1 = median_sorter(dt1, 5); //中值 dt2 = new int[5]; //取1×5窗口5個像素 dt2[0] = (bm.GetPixel(i, j - 2)).B; dt2[1] = (bm.GetPixel(i, j - 1)).B; dt2[2] = (bm.GetPixel(i, j)).B; dt2[3] = (bm.GetPixel(i, j + 1)).B; dt2[4] = (bm.GetPixel(i, j + 2)).B; r2 = median_sorter(dt2, 5); //中值 } else if (n == 2) { dt = new int[5]; //取5×1窗口5個像素 dt[0] = (bm.GetPixel(i - 2, j)).R; dt[1] = (bm.GetPixel(i - 1, j)).R; dt[2] = (bm.GetPixel(i, j)).R; dt[3] = (bm.GetPixel(i + 1, j)).R; dt[4] = (bm.GetPixel(i + 2, j)).R; r = median_sorter(dt, 5); //中值 dt = new int[5]; //取5×1窗口5個像素 dt1 = new int[5]; dt1[0] = (bm.GetPixel(i - 2, j)).G; dt1[1] = (bm.GetPixel(i - 1, j)).G; dt1[2] = (bm.GetPixel(i, j)).G; dt1[3] = (bm.GetPixel(i + 1, j)).G; dt1[4] = (bm.GetPixel(i + 2, j)).G; r1 = median_sorter(dt1, 5); //中值 //取5×1窗口5個像素 dt2 = new int[5]; dt2[0] = (bm.GetPixel(i - 2, j)).B; dt2[1] = (bm.GetPixel(i - 1, j)).B; dt2[2] = (bm.GetPixel(i, j)).B; dt2[3] = (bm.GetPixel(i + 1, j)).B; dt2[4] = (bm.GetPixel(i + 2, j)).B; r2 = median_sorter(dt2, 5); //中值 } obm.SetPixel(i, j, Color.FromArgb(r, r1, r2)); //輸出 } } return obm; } //冒泡排序,輸出中值 public int median_sorter(int[] dt, int m) { int tem; for (int k = m - 1; k >= 1; k--) for (int l = 1; l <= k; l++) if (dt[l - 1] > dt[l]) { tem = dt[l]; dt[l] = dt[l - 1]; dt[l - 1] = tem; } return dt[(int)(m / 2)]; } private void pictureBox1_Click(object sender, EventArgs e) { } private void 圖像銳化ToolStripMenuItem_Click(object sender, EventArgs e) { } /* * pix --待檢測圖像數組 * iw, ih --待檢測圖像寬高 * num --算子代號.1:Kirsch算子;2:Laplace算子;3:Prewitt算子;5:Sobel算子 */ public Bitmap detect(Bitmap bm, int iw, int ih, int num) { Bitmap b1 = new Bitmap(pictureBox1.Image); Color c = new Color(); int i, j, r; int[,] inr = new int[iw, ih]; //紅色分量矩陣 int[,] ing = new int[iw, ih]; //綠色分量矩陣 int[,] inb = new int[iw, ih]; //藍色分量矩陣 int[,] gray = new int[iw, ih];//灰度圖像矩陣 //轉變為灰度圖像矩陣 for (j = 0; j < ih; j++) { for (i = 0; i < iw; i++) { c = bm.GetPixel(i, j); inr[i, j] = c.R; ing[i, j] = c.G; inb[i, j] = c.B; gray[i, j] = (int)((c.R + c.G + c.B) / 3.0); } } if (num == 1)//Kirsch { int[,] kir0 = {{ 5, 5, 5}, {-3, 0,-3}, {-3,-3,-3}},//kir0 kir1 = {{-3, 5, 5}, {-3, 0, 5}, {-3,-3,-3}},//kir1 kir2 = {{-3,-3, 5}, {-3, 0, 5}, {-3,-3, 5}},//kir2 kir3 = {{-3,-3,-3}, {-3, 0, 5}, {-3, 5, 5}},//kir3 kir4 = {{-3,-3,-3}, {-3, 0,-3}, { 5, 5, 5}},//kir4 kir5 = {{-3,-3,-3}, { 5, 0,-3}, { 5, 5,-3}},//kir5 kir6 = {{ 5,-3,-3}, { 5, 0,-3}, { 5,-3,-3}},//kir6 kir7 = {{ 5, 5,-3}, { 5, 0,-3}, {-3,-3,-3}};//kir7 //邊緣檢測 int[,] edge0 = new int[iw, ih]; int[,] edge1 = new int[iw, ih]; int[,] edge2 = new int[iw, ih]; int[,] edge3 = new int[iw, ih]; int[,] edge4 = new int[iw, ih]; int[,] edge5 = new int[iw, ih]; int[,] edge6 = new int[iw, ih]; int[,] edge7 = new int[iw, ih]; edge0 = edgeEnhance(gray, kir0, iw, ih); edge1 = edgeEnhance(gray, kir1, iw, ih); edge2 = edgeEnhance(gray, kir2, iw, ih); edge3 = edgeEnhance(gray, kir3, iw, ih); edge4 = edgeEnhance(gray, kir4, iw, ih); edge5 = edgeEnhance(gray, kir5, iw, ih); edge6 = edgeEnhance(gray, kir6, iw, ih); edge7 = edgeEnhance(gray, kir7, iw, ih); int[] tem = new int[8]; int max; for (j = 0; j < ih; j++) { for (i = 0; i < iw; i++) { tem[0] = edge0[i, j]; tem[1] = edge1[i, j]; tem[2] = edge2[i, j]; tem[3] = edge3[i, j]; tem[4] = edge4[i, j]; tem[5] = edge5[i, j]; tem[6] = edge6[i, j]; tem[7] = edge7[i, j]; max = 0; for (int k = 0; k < 8; k++) if (tem[k] > max) max = tem[k]; if (max > 255) max = 255; r = 255 - max; b1.SetPixel(i, j, Color.FromArgb(r, r, r)); } } } else if (num == 2) //Laplace { int[,] lap1 = {{ 1, 1, 1}, { 1,-8, 1}, { 1, 1, 1}}; /*byte[][] lap2 = {{ 0, 1, 0}, { 1,-4, 1}, { 0, 1, 0}}; */ //邊緣增強 int[,] edge = edgeEnhance(gray, lap1, iw, ih); for (j = 0; j < ih; j++) { for (i = 0; i < iw; i++) { r = edge[i, j]; if (r > 255) r = 255; if (r < 0) r = 0; c = Color.FromArgb(r, r, r); b1.SetPixel(i, j, c); } } } else if (num == 3)//Prewitt { //Prewitt算子D_x模板 int[,] pre1 = {{ 1, 0,-1}, { 1, 0,-1}, { 1, 0,-1}}; //Prewitt算子D_y模板 int[,] pre2 = {{ 1, 1, 1}, { 0, 0, 0}, {-1,-1,-1}}; int[,] edge1 = edgeEnhance(gray, pre1, iw, ih); int[,] edge2 = edgeEnhance(gray, pre2, iw, ih); for (j = 0; j < ih; j++) { for (i = 0; i < iw; i++) { r = Math.Max(edge1[i, j], edge2[i, j]); if(r > 255) r = 255; c = Color.FromArgb(r, r, r); b1.SetPixel(i, j, c); } } } else if (num == 5) //Sobel { int[,] sob1 = {{ 1, 0,-1}, { 2, 0,-2}, { 1, 0,-1}}; int[,] sob2 = {{ 1, 2, 1}, { 0, 0, 0}, {-1,-2,-1}}, int[,] edge1 = edgeEnhance(gray, sob1, iw, ih); int[,] edge2 = edgeEnhance(gray, sob2, iw, ih); for (j = 0; j < ih; j++) { for (i = 0; i < iw; i++) { r = Math.Max(edge1[i, j], edge2[i, j]); if(r > 255) r = 255; c = Color.FromArgb(r, r, r); b1.SetPixel(i, j, c); } } } return b1; } private void kirsch算子銳化ToolStripMenuItem_Click(object sender, EventArgs e) { if (bitmap != null) { // this.Text = " 圖像 - 圖像銳化 - Kirsch算子"; Bitmap bm = new Bitmap(pictureBox1.Image); //1: Kirsch銳化 bm = detect(bm, iw, ih, 1) pictureBox2.Refresh(); pictureBox2.Image = bm; label7.Text = " Kirsch算子 銳化結果"; } } public int[,] edgeEnhance(int[,] ing, int[,] tmp, int iw, int ih) { int[,] ed = new int[iw, ih]; for (int j = 1; j < ih - 1; j++) { for (int i = 1; i < iw - 1; i++) { ed[i, j] = Math.Abs(tmp[0, 0] * ing[i - 1, j - 1] + tmp[0, 1] * ing[i - 1, j] + tmp[0, 2] * ing[i - 1, j + 1] + tmp[1, 0] * ing[i, j - 1] + tmp[1, 1] * ing[i, j] + tmp[1, 2] * ing[i, j + 1] + tmp[2, 0] * ing[i + 1, j - 1] + tmp[2, 1] * ing[i + 1, j] + tmp[2, 2] * ing[i + 1, j + 1]); } } return ed; } //Laplace算子 private void laplace算子銳化ToolStripMenuItem_Click(object sender, EventArgs e) { if (bitmap != null) { Bitmap bm = new Bitmap(pictureBox1.Image); //2: Laplace銳化 bm = detect(bm, iw, ih, 2); pictureBox2.Refresh(); pictureBox2.Image = bm; label7.Text = "Laplace算子 銳化結果"; } } //Prewitt算子 private void prewitt算子銳化ToolStripMenuItem_Click(object sender, EventArgs e) { if (bitmap != null) { Bitmap bm = new Bitmap(pictureBox1.Image); //3:Prewitt銳化 bm = detect(bm, iw, ih, 3); pictureBox2.Refresh(); pictureBox2.Image = bm; label2.Location = new Point(390, 280); label7.Text = " Prewitt算子 銳化結果"; } } //Roberts算子 private void roberts算子銳化ToolStripMenuItem_Click(object sender, EventArgs e) { if (bitmap != null) { Bitmap bm = new Bitmap(pictureBox1.Image); //Robert邊緣檢測 bm = robert(bm, iw, ih); pictureBox2.Refresh(); pictureBox2.Image = bm; label2.Location = new Point(390, 280); label7.Text = "Roberts算子 銳化結果"; } } //roberts算法 public Bitmap robert(Bitmap bm, int iw, int ih) { int r, r0, r1, r2, r3, g, g0, g1, g2, g3, b, b0, b1, b2, b3; Bitmap obm = new Bitmap(pictureBox1.Image); int[,] inr = new int[iw, ih];//紅色分量矩陣 int[,] ing = new int[iw, ih];//綠色分量矩陣 int[,] inb = new int[iw, ih];//藍色分量矩陣 int[,] gray = new int[iw, ih];//灰度圖像矩陣 for (int j = 1; j < ih - 1; j++) { for (int i = 1; i < iw - 1; i++) { r0 = (bm.GetPixel(i, j)).R; r1 = (bm.GetPixel(i, j + 1)).R; r2 = (bm.GetPixel(i + 1, j)).R; r3 = (bm.GetPixel(i + 1, j + 1)).R; r = (int)Math.Sqrt((r0 - r3) * (r0 - r3) + (r1 - r2) * (r1 - r2)); g0 = (bm.GetPixel(i, j)).G; g1 = (bm.GetPixel(i, j + 1)).G; g2 = (bm.GetPixel(i + 1, j)).G; g3 = (bm.GetPixel(i + 1, j + 1)).G; g = (int)Math.Sqrt((g0 - g3) * (g0 - g3) + (g1 - g2) * (g1 - g2)); b0 = (bm.GetPixel(i, j)).B; b1 = (bm.GetPixel(i, j + 1)).B; b2 = (bm.GetPixel(i + 1, j)).B; b3 = (bm.GetPixel(i + 1, j + 1)).B; b = (int)Math.Sqrt((b0 - b3) * (b0 - b3) + (b1 - b2) * (b1 - b2)); if (r < 0) r = 0; //黑色,邊緣點 if (r > 255) r = 255; obm.SetPixel(i, j, Color.FromArgb(r, r, r)); } } return obm; } //Sobel算子 private void sobel算子銳化ToolStripMenuItem_Click(object sender, EventArgs e) { if (bitmap != null) { Bitmap bm = new Bitmap(pictureBox1.Image); //5: Sobel銳化 bm = detect(bm, 256, 256, 5); pictureBox2.Refresh(); pictureBox2.Image = bm; label7.Text = " Sobel算子 銳化結果"; } } private void 低通濾波ToolStripMenuItem_Click(object sender, EventArgs e) { if (bitmap != null) { Bitmap bm = new Bitmap(pictureBox1.Image); int num ; for (num = 1; num < 4; num++) { //低通濾波 bm = lowpass(bm, iw, ih, num); pictureBox2.Refresh(); pictureBox2.Image = bm; if (num == 1) label7.Text = "1*5模板低通濾波結果"; else if (num == 2) label7.Text = "5*1模板低通濾波結果"; else if (num == 3) label7.Text = "5*5模板低通濾波結果"; } } } //3×3低通濾波方法 public Bitmap lowpass(Bitmap bm, int iw, int ih, int n) { Bitmap obm = new Bitmap(pictureBox1.Image); int[,] h; //定義擴展輸入圖像矩陣 int[,] ex_inpix = exinpix(bm, iw, ih); //低通濾波 for (int j = 1; j < ih + 1; j++) { for (int i = 1; i < iw + 1; i++) { int r = 0, sum = 0; //低通模板 h = low_matrix(n); //求3×3窗口9個像素加權和 for (int k = -1; k < 2; k++) for (int l = -1; l < 2; l++) sum = sum + h[k + 1, l + 1] * ex_inpix[i + k, j + l]; if (n == 1) r = (int)(sum / 9); //h2平均值 else if (n == 2) r = (int)(sum / 10); //h3 else if (n == 3) r = (int)(sum / 16); //h4 obm.SetPixel(i - 1, j - 1, Color.FromArgb(r, r, r)); //輸出 } } return obm; } //定義擴展輸入圖像矩陣 public int[,] exinpix(Bitmap bm, int iw, int ih) { int[,] ex_inpix = new int[iw + 2, ih + 2]; //獲取非邊界灰度值 for (int j = 0; j < ih; j++) for (int i = 0; i < iw; i++) ex_inpix[i + 1, j + 1] = (bm.GetPixel(i, j)).R; //四角點處理 ex_inpix[0, 0] = ex_inpix[1, 1]; ex_inpix[0, ih + 1] = ex_inpix[1, ih]; ex_inpix[iw + 1, 0] = ex_inpix[iw, 1]; ex_inpix[iw + 1, ih + 1] = ex_inpix[iw, ih]; //上下邊界處理 for (int j = 1; j < ih + 1; j++) { ex_inpix[0, j] = ex_inpix[1, j]; //上邊界 ex_inpix[iw + 1, j] = ex_inpix[iw, j];//下邊界 } //左右邊界處理 for (int i = 1; i < iw + 1; i++) { ex_inpix[i, 0] = ex_inpix[i, 1]; //左邊界 ex_inpix[i, ih + 1] = ex_inpix[i, ih];//右邊界 } return ex_inpix; } //低通濾波模板 public int[,] low_matrix(int n) { int[,] h = new int[3, 3]; if (n == 1) //h2 { h[0, 0] = 1; h[0, 1] = 1; h[0, 2] = 1; h[1, 0] = 1; h[1, 1] = 1; h[1, 2] = 1; h[2, 0] = 1; h[2, 1] = 1; h[2, 2] = 1; } else if (n == 2)//h3 { h[0, 0] = 1; h[0, 1] = 1; h[0, 2] = 1; h[1, 0] = 1; h[1, 1] = 2; h[1, 2] = 1; h[2, 0] = 1; h[2, 1] = 1; h[2, 2] = 1; } else if (n == 3)//h4 { h[0, 0] = 1; h[0, 1] = 2; h[0, 2] = 1; h[1, 0] = 2; h[1, 1] = 4; h[1, 2] = 2; h[2, 0] = 1; h[2, 1] = 2; h[2, 2] = 1; } return h; } } }
上述就是小編為大家分享的C#怎么實現一個數字圖像處理程序了,如果剛好有類似的疑惑,不妨參照上述分析進行理解。如果想知道更多相關知識,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。