下面代码使用OpenCV来打开手提电脑摄像头,并获取实时彩色(摄像头允许情况下)视频图像显示在屏幕上。
//包含头文件
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/core/core.hpp>
//使用命名空间
using namespace cv;
int main()
{
VideoCapture cap(0);//VideoCapture类用法:1.VideoCapture(const string& filename)打开视频文件;2.VideoCapture(int device)打开设备。
if (!cap.isOpened())//检测VideoCapture类是否打开成功
{
return -1;
}
Mat mFrame; //定义变量类
bool bStop = false; //定义控制变量
while (!bStop)
{
cap >> mFrame;
imshow("camera", mFrame);//显示
if (waitKey(30) >= 0)//判断用户按键
{
bStop = true;
}
}
return 0;//结束返回
}