經過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
這兩個文件需要再重新改下名字(因為加進去報錯,始終用不起,谷歌了好半天):
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 下就可以播放本地視頻了
效果如下:
有問題可以站內信