从opencv2.0开始带有第三方库videoInput支持directshow
在lib中加上inputVideo.lib文件:
// Opencv2.2-videoInput.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include "videoInput.h"
#include "highgui.h"
#include "cv.h"
#pragma comment(linker, "/NODEFAULTLIB:atlthunk.lib")
int _tmain(int argc, _TCHAR* argv[])
{
int width=640;
int height=480;
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);
}
}
return 0;
}