模仿视频抓帧实现
路口或某些場所可能并不會把從攝像頭獲取到的視頻全部存儲下來或對所有的視頻幀進行處理,即攝像設備是一直處于打開狀態,可能會根據需要間隔性的抓取其中一幀,或當某事件觸發時才會抓取當前的一幀數據進行處理。這里使用兩個線程來模仿此場景。主線程用于開啟視頻設備,另一個線程在間隔指定時間后抓取當前幀并保存。code較簡單,沒有使用隊列和線程鎖。
測試代碼如下:
#include <string>
#include <fstream>
#include <vector>
#include <cmath>
#include <chrono>
#include <thread>#include <opencv2/opencv.hpp>namespace {volatile bool grab_video_frame = false;
volatile bool running = true;
cv::Mat frame;void save_video_frame()
{
#ifdef _MSC_VERstd::string path = "E:/GitCode/OpenCV_Test/test_images/";
#elsestd::string path = "test_images/";
#endiffor (int i = 0; i < 5; ++i) {grab_video_frame = true;auto now = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());std::string name = path + std::to_string(now) + ".png";fprintf(stdout, "start to grab frame: index: %d, name: %s\n", i + 1, name.c_str());while (1) {if (!grab_video_frame) {cv::imwrite(name, frame);break;}}std::this_thread::sleep_for(std::chrono::seconds(5)); // 5 seconds grab a frame}running = false;
}} // namespaceint test_opencv_grab_video_frame()
{cv::VideoCapture cap(0);if (!cap.isOpened()) {fprintf(stderr, "fail to open capture\n");return -1;}std::thread th(save_video_frame);while (running) {if (grab_video_frame) {cap >> frame; // instead of queuegrab_video_frame = false; // instead of thread lock}}th.join();return 0;
}
執行結果如下:
GitHub:https://github.com/fengbingchun/OpenCV_Test
總結
- 上一篇: C和C++安全编码笔记:指针诡计
- 下一篇: Windows下获取视频设备的一种改进实