设置首页收藏本站
开启左侧

C# EmguCV图像处理实例

[复制链接]
太极真人 发表于 2019-9-12 11:41:06 | 显示全部楼层 |阅读模式
1. 本例中,我们需要导入:Emgu.CV.UI.dll、Emgu.CV.World.dll
2. 然后在程序中导入命名空间:using Emgu.CV; using System.Diagnostics;
3. 然后拖3个ImageBox到主窗体,拖3个TextBox和4个Button到主窗体,如图所示:
功能说明:点击button1添加图片并显示到ImageBox1中;点击button2将ImageBox1中的图片去色,并将灰度图显示到ImageBox2中;点击button3直接载入新的图片并去色,然后显示到ImageBox3中;点击button4清除所有的图片和文本框中的内容。
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Diagnostics;
  6. using System.Drawing;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Windows.Forms;
  10. using Emgu.CV;

  11. namespace WindowsFormsApplication8
  12. {
  13. public partial class Form1 : Form
  14.     {
  15.         Mat img1 = null;
  16.         Mat img2 = null;
  17.         Mat img3 = null;
  18.         Stopwatch sw = new Stopwatch();
  19. public Form1()
  20.         {
  21.             InitializeComponent();
  22.         }

  23. private void button1_Click(object sender, EventArgs e)
  24.         {
  25.             OpenFileDialog lvse = new OpenFileDialog();
  26.             lvse.Title = "选择图片";
  27.             lvse.InitialDirectory = "";
  28.             lvse.Filter = "图片文件|*.bmp;*.jpg;*.jpeg;*.gif;*.png";
  29.             lvse.FilterIndex = 1;

  30. if (lvse.ShowDialog() == DialogResult.OK)
  31.             {
  32.                 textBox1.Text = null;
  33.                
  34.                 sw.Restart();

  35.                 img1 = CvInvoke.Imread(lvse.FileName, Emgu.CV.CvEnum.LoadImageType.AnyColor);

  36.                 imageBox1.Width = img1.Width / 2;
  37.                 imageBox1.Height = img1.Height / 2;
  38.                 imageBox1.Image = img1;
  39.                
  40.                 sw.Stop();
  41.                 textBox1.Text = sw.ElapsedMilliseconds.ToString();
  42.             }

  43.         }

  44. private void button2_Click(object sender, EventArgs e)
  45.         {
  46. if (img1 != null)
  47.             {
  48.                 img2 = new Mat(img1.Rows, img1.Cols, Emgu.CV.CvEnum.DepthType.Cv8U, 1);
  49.                 textBox2.Text = null;
  50.                 sw.Reset();
  51.                 sw.Start();

  52.                 CvInvoke.CvtColor(img1, img2, Emgu.CV.CvEnum.ColorConversion.Bgr2Gray);
  53.                 imageBox2.Width = img2.Width / 2;
  54.                 imageBox2.Height = img2.Height / 2;
  55.                 imageBox2.Image = img2;

  56.                 sw.Stop();
  57.                 textBox2.Text = sw.ElapsedMilliseconds.ToString();
  58.             }
  59.         }

  60. private void button3_Click(object sender, EventArgs e)
  61.         {
  62.             textBox3.Text = null;
  63.             OpenFileDialog lvse = new OpenFileDialog();
  64.             lvse.Title = "选择图片";
  65.             lvse.InitialDirectory = "";
  66.             lvse.Filter = "图片文件|*.bmp;*.jpg;*.jpeg;*.gif;*.png";
  67.             lvse.FilterIndex = 1;
  68. if (lvse.ShowDialog() == DialogResult.OK)
  69.             {
  70.                 sw.Reset();
  71.                 sw.Start();

  72.                 img3 = CvInvoke.Imread(lvse.FileName, Emgu.CV.CvEnum.LoadImageType.Grayscale);
  73.                 imageBox3.Width = img3.Width / 2;
  74.                 imageBox3.Height = img3.Height / 2;
  75.                 imageBox3.Image = img3;

  76.                 sw.Stop();
  77.                 textBox3.Text = sw.ElapsedMilliseconds.ToString();
  78.             }
  79.         }

  80. private void button4_Click(object sender, EventArgs e)
  81.         {
  82.             img1 = null;
  83.             img2 = null;
  84.             img3 = null;
  85.             imageBox1.Image = null;
  86.             imageBox2.Image = null;
  87.             imageBox3.Image = null;
  88.             textBox1.Text = null;
  89.             textBox2.Text = null;
  90.             textBox3.Text = null;
  91.         }
  92.     }
  93. }
复制代码

运行结果如下:
TIM截图20190912113813.png
其它:

创建图像并显示

  1. Image<Bgr, byte> image = new Image<Bgr, byte>(320, 240, new Bgr(0, 0, 255));

  2. //创建一张320*240尺寸颜色为红色的图像。

  3.             imageBox1.Image = image;//在ImageBox1控件中显示所创建好的图像。
复制代码


加载当前环境目录下图片并显示:
  1. // MessageBox.Show(Environment.CurrentDirectory.ToString());

  2.              Mat imgscr = CvInvoke.Imread(Environment.CurrentDirectory+"\\1.jpg");//读取图像

  3.             // CvInvoke.Imshow("img", imgscr);//显示图像

  4.               imageBox2.Image = imgscr;//在ImageBox2控件中显示所创建好的图像。

  5.              CvInvoke.WaitKey(0);//按键等待
复制代码

奖励计划已经开启,本站鼓励作者发布最擅长的技术内容和资源,流量变现就在现在,[点我]加入吧~~~Go
paul810528 发表于 2020-6-2 11:08:39 | 显示全部楼层
感謝樓主分享!
奖励计划已经开启,本站鼓励作者发布最擅长的技术内容和资源,流量变现就在现在,[点我]加入吧~~~Go
姑蘇城外 发表于 2020-8-23 10:52:44 | 显示全部楼层
相关dll在哪里下载?
奖励计划已经开启,本站鼓励作者发布最擅长的技术内容和资源,流量变现就在现在,[点我]加入吧~~~Go
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表