OpenCV打开摄像头并用窗口显示摄像头的内容
#include <iostream>
#include <opencv2/core/core.hpp>
#include<opencv2/highgui/highgui.hpp>
#include <opencv2/opencv.hpp>
using namespace cv;
using namespace std;
int main()
{
//打开第一个摄像头
VideoCapture cap(0);
//判断摄像头是否打开
if (!cap.isOpened())
{
cout<<"摄像头未成功打开"<<endl;
}
//创建窗口
namedWindow("打开摄像头",1);
for (;;)
{
//创建Mat对象
Mat frame;
//从cap中读取一帧存到frame中
cap>>frame;
//判断是否读取到
if (frame.empty())
{
break;
}
//显示摄像头读取到的图像
imshow("打开摄像头",frame);
//等待300毫秒,如果按键则退出循环
if (waitKey(300)>=0)
{
break;
}
}
}
或者
#include <iostream>
#include <opencv2/core/core.hpp>
#include<opencv2/highgui/highgui.hpp>
#include <opencv2/opencv.hpp>
using namespace cv;
using namespace std;
int main()
{
//打开第一个摄像头
VideoCapture cap(0);
//判断摄像头是否打开
if (!cap.isOpened())
{
cout << "摄像头未成功打开" << endl;
}
//创建窗口
namedWindow("打开摄像头", 1);
while (1)
{
//创建Mat对象
Mat frame;
//从cap中读取一帧存到frame中
bool res = cap.read(frame);
if (!res)
{
break;
}
//判断是否读取到
if (frame.empty())
{
break;
}
//显示摄像头读取到的图像
imshow("打开摄像头", frame);
//等待1毫秒,如果按键则退出循环
if (waitKey(1) >= 0)
{
break;
}
}
cap.release();
cv::destroyAllWindows();
return 0;
}
如果您觉得这篇博文有用,请访问我的个人站:http://www.stubbornhuang.com,更多博文干货等着您。