Emgucv视频处理--进阶篇

时间:2022-07-23
本文章向大家介绍Emgucv视频处理--进阶篇,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

链接:https://zhidao.baidu.com/question/559571801.html

C#中的IntPtr类型称为“平台特定的整数类型”,它们用于本机资源,如窗口句柄。资源的大小取决于使用的硬件和操作系统,但其大小总是足以包含系统的指针(因此也可以包含资源的名称)。

所以,在您调用的API函数中一定有类似窗体句柄这样的参数,那么当您声明这个函数时,您应该将它显式地声明为IntPtr类型。

例如,在一个C#程序中调用Win32API mciSendString函数控制光盘驱动器,这个函数的函数原型是:


MCIERROR mciSendString(
LPCTSTR lpszCommand,
LPTSTR lpszReturnString,
UINT cchReturn,
HANDLE hwndCallback
);

首先在C#中声明这个函数:

[DllImport("winmm.dll")]
private static extern long mciSendString(string a,string b,uint c,IntPtr d);

然后用这样的方法调用:

mciSendString("set cdaudio door open", null, 0, this.Handle);

//----------------------------------------------------------------------------
//  Copyright (C) 2004-2019 by EMGU Corporation. All rights reserved.       
//----------------------------------------------------------------------------

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

namespace CameraCapture
{
   public partial class CameraCapture : Form
   {
      private VideoCapture _capture = null;
      private bool _captureInProgress;
      private Mat _frame;
      private Mat _grayFrame;
      private Mat _smallGrayFrame;
      private Mat _smoothedGrayFrame;
      private Mat _cannyFrame;

      public CameraCapture()
      {
         InitializeComponent();
      //使用显卡处理图像数据效率会很多,如果你的设备支持,最好打开,使用CvInvoke.HaveOpenCLCompatibleGpuDevice能返回是否支持.
       // 配置CvInvoke.UseOpenCL能让OpenCV 启用或者停用 GPU运算
      //CvInvoke.UseOpenCL = CvInvoke.HaveOpenCLCompatibleGpuDevice;
            CvInvoke.UseOpenCL = false;
         try
         {
           //构造一个摄像头实例,如果调用本地摄像机则括号里面为空
           _capture = new VideoCapture(@"C:UsersAdministratorDesktopvideovtest.avi");
           _capture.ImageGrabbed += ProcessFrame;//图像捕捉事件
          }
         catch (NullReferenceException excpt)
         {
            MessageBox.Show(excpt.Message);
         }
         _frame = new Mat();
         _grayFrame = new Mat();
         _smallGrayFrame = new Mat();
         _smoothedGrayFrame = new Mat();
         _cannyFrame = new Mat();
           
      }

      private void ProcessFrame(object sender, EventArgs arg)
      {
       
        if (_capture != null && _capture.Ptr != IntPtr.Zero)
           // if (_capture != null)
            {
        _capture.Retrieve(_frame, 0);

        CvInvoke.CvtColor(_frame, _grayFrame, ColorConversion.Bgr2Gray);

        CvInvoke.PyrDown(_grayFrame, _smallGrayFrame);
            //进行高斯向下采样,执行高斯金字塔分解步骤向下采样。固定原图像,
            //删除指定行和列(可以全为奇数行和列,或者偶数行和列...),从而减小图像的宽度和高度。

        CvInvoke.PyrUp(_smallGrayFrame, _smoothedGrayFrame);
      //执行高斯金字塔分解向上采样,首先透过注入固定行和列0像素值,在通过插值算法,对插入行列进行插值,这样用于放大原图像的四倍。
      //参数解析:IInputArraysrc:输入图像,即原图像。IOutputArraydst:输出图像,采样后得到的图像。

        CvInvoke.Canny(_smoothedGrayFrame, _cannyFrame, 100, 60);
        //多级边缘检测算法

         captureImageBox.Image = _frame;
        grayscaleImageBox.Image = _grayFrame;
        smoothedGrayscaleImageBox.Image = _smoothedGrayFrame;
        cannyImageBox.Image = _cannyFrame;
         }
      }

      private void captureButtonClick(object sender, EventArgs e)
      {
         if (_capture != null)
         {
            if (_captureInProgress)
            {  //stop the capture
               captureButton.Text = "Start Capture";
               _capture.Pause();
            }
            else
            {
               //start the capture
               captureButton.Text = "Stop";
               _capture.Start();
            }

            _captureInProgress = !_captureInProgress;
         }
      }
      //Dispose意为释放,释放组件所占用的内存。
      //C#特性,为提高运行效率,自动会释放已使用过且不再需要使用的组件来减少程序的CPU使用率。
      //默认会在程序运行一段时间后自动加载该Dispose方法,或者可以显式的自行调用此方法。
     private void ReleaseData()
      {
            if (_capture != null)
            
                _capture.Dispose();

      }

      private void FlipHorizontalButtonClick(object sender, EventArgs e)
      {
            // 得到和设置Capture类是否进行水平翻转。

         if (_capture != null) _capture.FlipHorizontal = !_capture.FlipHorizontal;
      }

      private void FlipVerticalButtonClick(object sender, EventArgs e)
      {
         if (_capture != null) _capture.FlipVertical = !_capture.FlipVertical;
      }
   }
}