查了,想了,写了几天,网上都没有相关的,特意贴出来,分享一下。
版权所有。
主要涉及异常捕捉,opencv。
new,指针可以换成常规操作;
#include <iostream>
#include <opencv2/opencv.hpp>
int main()
{
//打开一个默认的相机
cv::VideoCapture *capture_ptr = new cv::VideoCapture;
capture_ptr->open(0);
// //检查是否成功打开
// if (!capture_ptr->isOpened())
// return -1;
cv::Mat frame;
int count = 0;
while (1)
{
try
{
// std::cout << "c1.isOpened: " << capture_ptr->isOpened() << "; frame.rows: " << frame.cols << std::endl;
*capture_ptr >> frame;
cv::imshow("视频", frame); //显示
cv::waitKey(30);
count++;
std::cout << "read the " << count << " th frame successfully!" << std::endl;
}
catch (cv::Exception &e)
{
// delete capture_ptr;
// capture_ptr = new cv::VideoCapture;
capture_ptr->release();
capture_ptr->open(0);
// std::cout << "c2.isOpened: " << capture_ptr->isOpened() << std::endl;
const char *msg_e = e.what();
std::cout << "exception caught: " << msg_e << std::endl;
}
}
}