Opencv C++ 读取摄像头并显示
#include <iostream>#include <string>#include <sstream>using namespace std;// OpenCV includes#include "opencv2/core.hpp"#include "opencv2/highgui.hpp"using namespace cv;int ...
·
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
// OpenCV includes
#include "opencv2/core.hpp"
#include "opencv2/highgui.hpp"
using namespace cv;
int main(int argc, const char** argv)
{
// 1.创建视频采集对象;
VideoCapture cap;
// 2.打开默认相机;
cap.open(0);
// 3.判断相机是否打开成功;
if (!cap.isOpened())
return -1;
// 4.显示窗口命名;
namedWindow("Video", 1);
for (;;)
{
// 获取新的一帧;
Mat frame;
cap >> frame;
if (frame.empty())
return 0;
// 显示新的帧;
imshow("Video", frame);
// 按键退出显示;
if (waitKey(30) >= 0) break;
}
// 5.释放视频采集对象;
cap.release();
return 0;
}

可用于 大华USB相机程序
#include <iostream>
#include <opencv2/opencv.hpp>
#include <opencv2/highgui.hpp>
using namespace std;
using namespace cv;
int main()
{
// 1.创建视频采集对象;
VideoCapture cap;
Mat readImage; //读取的图片;
// 2.打开默认相机;
cap.open(0);
// 3.设置采集图片大小; 具体大小根据相机设置;
cap.set(CAP_PROP_FRAME_HEIGHT, 2048);
cap.set(CAP_PROP_FRAME_WIDTH, 2448);
// 4.判断相机是否打开成功;
if (!cap.isOpened())
return -1;
// 5.循环读取图片 并显示;
namedWindow("Output Window");
while (true)
{
// 循环读取图片;
if (!cap.read(readImage)) {
cout << "No frame" << endl;
waitKey(0);
}
// 显示图片;
imshow("Output Window", readImage);
if (waitKey(1) >= 0) break;
}
return 0;
}
更多推荐


所有评论(0)