C# EmguCV圖像處理實例

若是須要查看更多文章,請微信搜索公衆號 csharp編程大全,須要進C#交流羣羣請加微信z438679770,備註進羣, 我邀請你進羣! ! !編程

1. 本例中,咱們須要導入:Emgu.CV.UI.dll、Emgu.CV.World.dll微信

2. 而後在程序中導入命名空間:using Emgu.CV; using System.Diagnostics;spa

3. 而後拖3個ImageBox到主窗體,拖3個TextBox和4個Button到主窗體,如圖所示:pwa

功能說明:點擊button1添加圖片並顯示到ImageBox1中;點擊button2將ImageBox1中的圖片去色,並將灰度圖顯示到ImageBox2中;點擊button3直接載入新的圖片並去色,而後顯示到ImageBox3中;點擊button4清除全部的圖片和文本框中的內容。orm

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;
        }
    }
}

  運行結果以下:blog

 

 

其它:圖片

建立圖像並顯示v8

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

//建立一張320*240尺寸顏色爲紅色的圖像。io

            imageBox1.Image = image;//在ImageBox1控件中顯示所建立好的圖像。

加載當前環境目錄下圖片並顯示:

 // MessageBox.Show(Environment.CurrentDirectory.ToString());

             Mat imgscr = CvInvoke.Imread(Environment.CurrentDirectory+"\\1.jpg");//讀取圖像

            // CvInvoke.Imshow("img", imgscr);//顯示圖像

              imageBox2.Image = imgscr;//在ImageBox2控件中顯示所建立好的圖像。

             CvInvoke.WaitKey(0);//按鍵等待

相關文章
相關標籤/搜索