Emgucv視訊處理--進階篇
阿新 • • 發佈:2020-11-24
如果需要檢視更多文章,請微信搜尋公眾號csharp程式設計大全,需要進C#交流群群請加微信z438679770,備註進群, 我邀請你進群! ! !
連結: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:\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; } } }