路口或某些場所可能並不會把從攝像頭獲取到的視頻全部存儲下來或對所有的視頻幀進行處理,即攝像設備是一直處於打開狀態,可能會根據需要間隔性的抓取其中一幀,或當某事件觸發時纔會抓取當前的一幀數據進行處理。這裏使用兩個線程來模仿此場景。主線程用於開啓視頻設備,另一個線程在間隔指定時間後抓取當前幀並保存。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_VER
std::string path = "E:/GitCode/OpenCV_Test/test_images/";
#else
std::string path = "test_images/";
#endif
for (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;
}
} // namespace
int 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 queue
grab_video_frame = false; // instead of thread lock
}
}
th.join();
return 0;
}
執行結果如下: