若是須要查看更多文章,請微信搜索公衆號 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; } } }