Emgucv视频处理--进阶篇

若是须要查看更多文章,请微信搜索公众号 csharp编程大全,须要进C#交流群群请加微信z438679770,备注进群, 我邀请你进群! ! !html

 

 

连接:https://zhidao.baidu.com/question/559571801.html算法

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

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

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

MCIERROR mciSendString(函数

LPCTSTR lpszCommand,ui

LPTSTR lpszReturnString,this

UINT cchReturn,spa

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:\Users\Administrator\Desktop\video\vtest.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;
      }
   }
}
相关文章
相关标签/搜索