videoInput視訊採集庫
描述
opencv中的VideoCapture在採集USB攝像頭影象時,讀入視訊慢,有可能會崩潰。github上有videoInput庫,可解決上述問題,直接在工程中包含了上面程式碼的其中兩個檔案VideoInput.cpp和VideoInput.h;
部分函式解釋
VideoInput類,相關的類是VideoDevice setVerbose:控制是否在控制檯輸出資訊開關 listDevices:打印出可用視訊裝置文字資訊,其返回值為可用的裝置數(0,1,2,…)
getDeviceName:得到裝置名函式 setUseCallback:視訊捕捉的回撥函式設定函式 setIdealFramerate:調整捕捉幀率的函式(預設30fps,可修改,但不能被保證準確,directshow會嘗試一個鄰近的幀率) setAutoReconnectOnFreeze:防止裝置休眠重新連線的函式 setupDevice:開啟裝置函式 setFormat:設定視訊制式 isFrameNew:檢測是否有新的幀函式 isDeviceSetup:檢測視訊是否開啟 getPixels:獲得資料的函式(注意這裡獲得的資料時uchar型的指標)
showSettingsWindow:顯示視訊設定視窗函式 控制視訊設定的相關函式有setVideoSettingFilter、setVideoSettingFilterPct、getVideoSettingFilter、setVideoSettingCamera、setVideoSettingCameraPct、getVideoSettingCamera 獲得視訊寬高資訊的函式有getWidth、getHeight、getSize
stopDevice:停止裝置函式 restartDevice:重啟裝置函式
函式示例
#include <opencv2/opencv.hpp> #include "videoInput.h" #include <iostream> using namespace std; using namespace cv; int main() { videoInput VI; int DevicesNum = VI.listDevices();//列印可用攝像頭裝置 cout << DevicesNum << endl; string DevicesName[5]; for (int i = 0; i < DevicesNum; i++) { string tmp(VI.getDeviceName(i)); DevicesName[i] = tmp; } int devId = 0; cout << "裝置名稱為:" << DevicesName[devId] << endl; VI.setupDevice(devId,680,480);//開啟你選擇的攝像頭 if (!VI.isDeviceSetup(devId)) return -1; int width = VI.getWidth(devId); int height = VI.getHeight(devId); int imgSize = VI.getSize(devId); unsigned char* dateBuffer = new unsigned char[imgSize]; while (1) { if (VI.isFrameNew(devId))//獲取一幀影象 VI.getPixels(devId,dateBuffer,false,true); Mat imgDate = Mat(height,width,CV_8UC3,dateBuffer); imshow("preview",imgDate); if (waitKey(30)>0) break;
} VI.stopDevice(devId); system("pause"); return 0; }