前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >C# EmguCV图像处理实例

C# EmguCV图像处理实例

作者头像
zls365
发布2020-08-19 10:34:35
1.7K0
发布2020-08-19 10:34:35
举报
文章被收录于专栏:CSharp编程大全CSharp编程大全

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清除所有的图片和文本框中的内容。

代码语言:javascript
复制
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Emgu.CV;

namespace WindowsFormsApplication8
{
public partial class Form1 : Form
    {
        Mat img1 = null;
        Mat img2 = null;
        Mat img3 = null;
        Stopwatch sw = new Stopwatch();
public Form1()
        {
            InitializeComponent();
        }

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

if (lvse.ShowDialog() == DialogResult.OK)
            {
                textBox1.Text = null;
                
                sw.Restart();

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

                imageBox1.Width = img1.Width / 2;
                imageBox1.Height = img1.Height / 2;
                imageBox1.Image = img1;
                
                sw.Stop();
                textBox1.Text = sw.ElapsedMilliseconds.ToString();
            }

        }

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

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

                sw.Stop();
                textBox2.Text = sw.ElapsedMilliseconds.ToString();
            }
        }

private void button3_Click(object sender, EventArgs e)
        {
            textBox3.Text = null;
            OpenFileDialog lvse = new OpenFileDialog();
            lvse.Title = "选择图片";
            lvse.InitialDirectory = "";
            lvse.Filter = "图片文件|*.bmp;*.jpg;*.jpeg;*.gif;*.png";
            lvse.FilterIndex = 1;
if (lvse.ShowDialog() == DialogResult.OK)
            {
                sw.Reset();
                sw.Start();

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

                sw.Stop();
                textBox3.Text = sw.ElapsedMilliseconds.ToString();
            }
        }

private void button4_Click(object sender, EventArgs e)
        {
            img1 = null;
            img2 = null;
            img3 = null;
            imageBox1.Image = null;
            imageBox2.Image = null;
            imageBox3.Image = null;
            textBox1.Text = null;
            textBox2.Text = null;
            textBox3.Text = null;
        }
    }
}

运行结果如下:

其它:

创建图像并显示

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

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

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

加载当前环境目录下图片并显示:

代码语言:javascript
复制
 // MessageBox.Show(Environment.CurrentDirectory.ToString());
             Mat imgscr = CvInvoke.Imread(Environment.CurrentDirectory+"\\1.jpg");//读取图像
            // CvInvoke.Imshow("img", imgscr);//显示图像
              imageBox2.Image = imgscr;//在ImageBox2控件中显示所创建好的图像。
             CvInvoke.WaitKey(0);//按键等待
本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2019-09-01,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 CSharp编程大全 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档