C#简单数字图像处理程序

作者:袖梨 2022-06-25

C#编写的简单数字图像处理程序,数字图像处理的平时成绩和编程作业竟然占50%,那就把最近做的事写个札记吧。

先放个最终做成提交的效果看看:

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 = 255) r = 255;
   if (r = 255) g = 255;
   if (g = 255) b = 255;
   if (b outg[i] 
  for (int i = 0; i  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 = 1; k--)
  for (int l = 1; l  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  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  255) r = 255;
 
   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  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  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 

六、参考书籍

相关文章

精彩推荐