OpenCV:打开摄像头获取视频流
代码:
#include
#include
using namespace cv;
using namespace std;
int main()
{
//【1】从摄像头读入视频
VideoCapture capture(1);
if (!capture.isOpened())
{
cout<< "open camera fail ..." << endl;
return -1;
}
capture.set(CAP_PROP_FRAME_WIDTH, 640);
capture.set(CAP_PROP_FRAME_HEIGHT, 480);
char filename[200];
int count =0;
//【2】循环显示每一帧
Mat frame; //定义一个Mat变量,用于存储每一帧的图像
char key;
while (true)
{
//读入图像
capture>> frame; //读取当前帧
key = waitKey(20);
if(key ==27)//esc键退出
break;
if(key ==32)//空格键保存图像
{
sprintf(filename, "Picture_%d.png", ++count);
imwrite(filename, frame);//
namedWindow("[frame]", WINDOW_NORMAL);
imshow("[frame]",frame);
}
imshow("image", frame); //显示当前帧
}
return 0;
}
更多推荐
所有评论(0)