http://blog.csdn.net/babbxazzg/article/details/6752539
OpenCV使用VideoInput庫從攝像頭捕獲視頻
OpenCV具有強大的圖像處理功能,處理視頻也是毫不遜色。只是其自帶的HighGUI並非是具有工業強度的模塊,不適合最終提供給客戶,僅僅是方便程序開發階段的調試。其中跟視頻捕獲相關的cvCreateCameraCapture或cvCaptureFromCAM函數可以方便的從攝像頭捕獲視頻,但這兩個函數在Windows中都是由較爲低效的VFW機制實現的,不適合在最終產品中使用。
在Windows中使用DirectShow可以高效的從攝像頭捕獲視頻,但使用較爲複雜。OpenCV中文網的YuShiQi提供了一個基於DirectShow實現的攝像頭捕獲類,但試用後效果一般,速度並沒有明顯的提高。
新版的OpenCV(OpenCV 2.0及以後版本)提供了第三方的VideoInput庫,該庫實現了基於DirectShow的視頻捕獲,使用方便、功能強大,速度也很快。另外還有一個意外發現——騰訊QQ的視頻捕獲貌似用的也是這個庫哦~~
好了,下面就說說如何使用這個庫
如果你的OpenCV版本低於2.0,請先去http://muonics.net/school/spring05/videoInput/
下載VideoInput,我這裏需要翻牆才能上去。如果使用2.0及以後版本則自帶該庫。
首先需要在代碼中包含VideoInput的頭文件
#include <videoInput.h>
這個文件的默認路徑是C:/OpenCV2.1/3rdparty/include
靜態鏈接庫的默認路徑是C:/OpenCV2.1/3rdparty/lib
請先設置IDE中頭文件和庫文件的搜索路徑。
下面給出一個讀取並顯示攝像頭畫面的實例
int width=320;
int height=240;
IplImage *pRgb=cvCreateImage(cvSize(width,height), IPL_DEPTH_8U, 3);
videoInput video;//創建視頻捕獲對象
video.setupDevice(0, width, height);//配置設備
video.showSettingsWindow(0);//該語句可以顯示視頻設置窗口,可以去掉
while(1)
{
if(video.isFrameNew(0))
{
video.getPixels(0, (unsigned char *)pRgb->imageData, false, true);//獲取一幀
//cvFlip(pRgb,NULL,1);//加上這句就水平翻轉畫面
char c=cvWaitKey(1);
if(c==27) break;//按ESC退出
cvShowImage(“Video”, pRgb);
}
}
有朋友提出編譯時可能會遇到如下錯誤:
"fatal error LNK1104: cannot open file 'atlthunk.lib'"
如果遇到此錯誤,請在文件的開頭加入如下語句:
#pragma comment(linker, "/NODEFAULTLIB:atlthunk.lib")
這個類庫還提供了非常強大的功能,具體函數可以查看videoInput.h頭文件。下面附上videoInput類的共有成員函數列表和相關說明。
//turns off console messages - default is to print messages
static void setVerbose(bool _verbose);
//Functions in rough order they should be used.
static int listDevices(bool silent = false);
//needs to be called after listDevices - otherwise returns NULL
static char * getDeviceName(int deviceID);
//choose to use callback based capture - or single threaded
void setUseCallback(bool useCallback);
//call before setupDevice
//directshow will try and get the closest possible framerate to what is requested
void setIdealFramerate(int deviceID, int idealFramerate);
//some devices will stop delivering frames after a while - this method gives you the option to try and reconnect
//to a device if videoInput detects that a device has stopped delivering frames.
//you MUST CALL isFrameNew every app loop for this to have any effect
void setAutoReconnectOnFreeze(int deviceNumber, bool doReconnect, int numMissedFramesBeforeReconnect);
//Choose one of these four to setup your device
bool setupDevice(int deviceID);
bool setupDevice(int deviceID, int w, int h);
//These two are only for capture cards
//USB and Firewire cameras souldn't specify connection
bool setupDevice(int deviceID, int connection);
bool setupDevice(int deviceID, int w, int h, int connection);
//If you need to you can set your NTSC/PAL/SECAM
//preference here. if it is available it will be used.
//see #defines above for available formats - eg VI_NTSC_M or VI_PAL_B
//should be called after setupDevice
//can be called multiple times
bool setFormat(int deviceNumber, int format);
//Tells you when a new frame has arrived - you should call this if you have specified setAutoReconnectOnFreeze to true
bool isFrameNew(int deviceID);
bool isDeviceSetup(int deviceID);
//Returns the pixels - flipRedAndBlue toggles RGB/BGR flipping - and you can flip the image too
unsigned char * getPixels(int deviceID, bool flipRedAndBlue = true, bool flipImage = false);
//Or pass in a buffer for getPixels to fill returns true if successful.
bool getPixels(int id, unsigned char * pixels, bool flipRedAndBlue = true, bool flipImage = false);
//Launches a pop up settings window
//For some reason in GLUT you have to call it twice each time.
void showSettingsWindow(int deviceID);
//Manual control over settings thanks.....
//These are experimental for now.
bool setVideoSettingFilter(int deviceID, long Property, long lValue, long Flags = NULL, bool useDefaultValue =false);
bool setVideoSettingFilterPct(int deviceID, long Property, float pctValue, long Flags = NULL);
bool getVideoSettingFilter(int deviceID, long Property, long &min, long &max, long &SteppingDelta, long¤tValue, long &flags, long &defaultValue);
bool setVideoSettingCamera(int deviceID, long Property, long lValue, long Flags = NULL, bool useDefaultValue =false);
bool setVideoSettingCameraPct(int deviceID, long Property, float pctValue, long Flags = NULL);
bool getVideoSettingCamera(int deviceID, long Property, long &min, long &max, long &SteppingDelta, long¤tValue, long &flags, long &defaultValue);
//bool setVideoSettingCam(int deviceID, long Property, long lValue, long Flags = NULL, bool useDefaultValue = false);
//get width, height and number of pixels
int getWidth(int deviceID);
int getHeight(int deviceID);
int getSize(int deviceID);
//completely stops and frees a device
void stopDevice(int deviceID);
//as above but then sets it up with same settings
bool restartDevice(int deviceID);
//number of devices available
int devicesFound;