標籤:
經過N多調研,最終選擇了OpenCV(Emgu CV)
** 至於DirectShow, OpenCV等等其他大家可以百度,在這裡我就不再贅述
環境:vs2010 vs2012 vs2013均可
OpenCV官方網站為:Emgu CV
也可以去我的百度網盤下載安裝包:libemgucv-windows-universal-cuda-2.4.10.1940
然後就可以自己想怎麼玩,怎麼玩了。
安裝好後:
我的一個Demo,用來開啟網路攝影機:
:c#調用網路攝影機
代碼結構:
運行效果:
核心代碼解釋:
namespace CameraCapture{ public partial class CameraCapture : Form { private readonly Capture _capture; private bool _captureInProgress; public CameraCapture()//建構函式 { InitializeComponent(); try { _capture = new Capture();//構造一個網路攝影機執行個體 _capture.ImageGrabbed += ProcessFrame;//映像捕捉事件 } catch (NullReferenceException excpt) { MessageBox.Show(excpt.Message); } } private void ProcessFrame(object sender, EventArgs arg) { Image<Bgr, Byte> frame = _capture.RetrieveBgrFrame();//擷取視訊框架 Image<Gray, Byte> grayFrame = frame.Convert<Gray, Byte>(); Image<Gray, Byte> smallGrayFrame = grayFrame.PyrDown(); Image<Gray, Byte> smoothedGrayFrame = smallGrayFrame.PyrUp(); Image<Gray, Byte> cannyFrame = smoothedGrayFrame.Canny(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; } } private void ReleaseData()//釋放資源 { if (_capture != null) _capture.Dispose(); } private void FlipHorizontalButtonClick(object sender, EventArgs e) { if (_capture != null) _capture.FlipHorizontal = !_capture.FlipHorizontal; } private void FlipVerticalButtonClick(object sender, EventArgs e) { if (_capture != null) _capture.FlipVertical = !_capture.FlipVertical; } }}
擴充
emgucv不僅可以控制網路攝影機,而且可以直接播放本地視頻,但是需要一些配置
public CameraCapture() { InitializeComponent(); try { //_capture = new Capture(); var fileName = "檔案地址"; _capture = new Capture(fileName); _capture.ImageGrabbed += ProcessFrame; } catch (NullReferenceException excpt) { MessageBox.Show(excpt.Message); } }
需要下載兩個第三方檔案:
opencv_ffmpeg.dll
opencv_ffmpeg_64.dll
三方外掛程式可以去 github opencv 下載 三方控制項,我的百度網盤:opencv_ffmpeg
這兩個檔案需要再重新改下名字(因為加進去報錯,始終用不起,Google了好半天):
opencv_ffmpeg.dll opencv_ffmpegVersion.dll -> opencv_ffmpeg2410_64.dll
opencv_ffmpeg_64.dll opencv_ffmpegVersion_64.dll -> opencv_ffmpeg2410_64.dll
最後複製加到bin目錄下的 x86,x64 下就可以播放本地視頻了
效果如下:
有問題可以站內信
c# 遠程監控(2) 網路攝影機調研