2016年9月12日 星期一

如何做視窗上不同視訊處理效果_針對不同按鈕

目標:






首先   新增一個專案





Tools
-->NuGet Package Manager
-->Manage NuGet Packages for Solution








去 sorforge  補下載

https://sourceforge.net/projects/emgucv/files/emgucv/3.1.0/


假壓縮完後




手動添加  UI  部分 組件的   dll










首先介面布局




四個  radioButton  一個  button

一個  emgucv 的  ImageBox


效果示意圖



程式碼

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Emgu.CV;
using Emgu.CV.Structure;
using Emgu.Util;

namespace emgucv_exercise
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private Capture _capture = null;        
        //Image<Bgr, Byte> frame;
        Mat imgMt;
        private void ProcessFrame(object sender, EventArgs arg)
        {            
            imgMt = _capture.QueryFrame();
            imageBox1.Image = imgMt;
        }
        private void btn_webcamOpen_Click(object sender, EventArgs e)
        {
            _capture = new Capture(0);
            _capture.SetCaptureProperty(Emgu.CV.CvEnum.CapProp.Fps, 30);
            _capture.SetCaptureProperty(Emgu.CV.CvEnum.CapProp.FrameWidth, 320);
            _capture.SetCaptureProperty(Emgu.CV.CvEnum.CapProp.FrameHeight, 240);
            Application.Idle += ProcessFrame;
        }

        private void btn_webcamClose_Click(object sender, EventArgs e)
        {
            _capture.Pause();
            _capture.Dispose();
        }
        
        private void rdB_Bchannel_CheckedChanged(object sender, EventArgs e)
        {
            Application.Idle += ProcessBgr_B;            
        }
        private void rdB_Gchannel_CheckedChanged(object sender, EventArgs e)
        {
            Application.Idle += ProcessBgr_G;
        }
        private void rdB_Rchannel_CheckedChanged(object sender, EventArgs e)
        {
            Application.Idle += Process_R;
        }
        //Image<Bgr, byte> imgBgr;
        //Mat capMat;
        private void ProcessBgr_B(object sender, EventArgs e)
        {
            Image<Bgr, Byte> imgBgr = _capture.QueryFrame().ToImage<Bgr, Byte>();

            //capMat = _capture.QueryFrame();
            //imgBgr = capMat.ToImage<Bgr, Byte>();

            for (int i = 0; i < imgBgr.Cols; i++)
                for (int j = 0; j < imgBgr.Rows; j++)
                {
                    imgBgr.Data[j, i, 0] = 255;
                    //imgBgr.Data[j, i, 1] = 255;
                    //imgBgr.Data[j, i, 2] = 255;
                }
            imageBox1.Image = imgBgr;
        }
        private void ProcessBgr_G(object sender, EventArgs e)
        {
            Image<Bgr, Byte> imgBgr = _capture.QueryFrame().ToImage<Bgr, Byte>();

            //capMat = _capture.QueryFrame();
            //imgBgr = capMat.ToImage<Bgr, Byte>();

            for (int i = 0; i < imgBgr.Cols; i++)
                for (int j = 0; j < imgBgr.Rows; j++)
                {
                    //imgBgr.Data[j, i, 0] = 255;
                    imgBgr.Data[j, i, 1] = 255;
                    //imgBgr.Data[j, i, 2] = 255;
                }
            imageBox1.Image = imgBgr;
        }
        private void Process_R(object sender, EventArgs e)
        {
            Image<Bgr, Byte> imgBgr = _capture.QueryFrame().ToImage<Bgr, Byte>();

            //capMat = _capture.QueryFrame();
            //imgBgr = capMat.ToImage<Bgr, Byte>();

            for (int i = 0; i < imgBgr.Cols; i++)
                for (int j = 0; j < imgBgr.Rows; j++)
                {
                    //imgBgr.Data[j, i, 0] = 255;
                    //imgBgr.Data[j, i, 1] = 255;
                    imgBgr.Data[j, i, 2] = 255;
                }
            imageBox1.Image = imgBgr;
        }

        private void rdB_Gray_CheckedChanged(object sender, EventArgs e)
        {
            Application.Idle += Process_Gray;
        }

        private void Process_Gray(object sender, EventArgs e)
        {
            Image<Bgr, Byte> imgBgr = _capture.QueryFrame().ToImage<Bgr, Byte>();
            Image<Gray, Byte> imgGray;

            imgGray = imgBgr.Convert<Gray, Byte>();
            imageBox1.Image = imgGray;
        }

        private void btn_H_Click(object sender, EventArgs e)
        {
            Application.Idle += Process_H;
        }

        private void Process_H(object sender, EventArgs e) //色相(H)、飽和度(S)、明度(V)
        {
            Image<Bgr, Byte> imgBgr = _capture.QueryFrame().ToImage<Bgr, Byte>();
            Image<Hsv, Byte> imgHsv;
            imgHsv = imgBgr.Convert<Hsv, Byte>();
            //.Convert<Hsv, Byte>()
            Hsv lowerLimit = new Hsv(0, 58, 40); //47, 0, 0
            Hsv upperLimit = new Hsv(35, 174, 255); //53, 255, 255
            //imgHsv.InRange(lowerLimit, upperLimit);


            imageBox1.Image = imgHsv.InRange(lowerLimit,upperLimit); 
        }
    }


}










沒有留言:

張貼留言