(VS2017+OPENCV3.3.1)攝像頭小工具,每5分鐘儲存一幀畫面
#include <opencv2/opencv.hpp>
#include <iostream>
#include <stdio.h>
#include <windows.h>
#include <string.h>
#include <time.h>
using namespace cv;
using namespace std;
//設定儲存檔名
string file_name()
{
string suffix = ".png";
char systime[50];
time_t aclock;
time(&aclock);
strftime(systime, 50, "%Y%m%d%H%M%S", localtime(&aclock));
string filename = systime + suffix;
return filename;
}
//延時5min
int delay_time()
{
clock_t start_time;
clock_t end_time;
double duration;
start_time = clock();
while (1)
{
end_time = clock();
duration = (double)(end_time - start_time) / CLOCKS_PER_SEC;
if (duration > 300) break;
}
return 0;
}
int main(int argc, const char** argv)
{
int i;
Mat frame;
string filename;
VideoCapture cap;
for (;;)
{
//開啟攝像頭
cap.open(0);
//顯示攝像頭捕獲的幀
cap >> frame;
namedWindow("Live", 0);
imshow("Live", frame);
//儲存影象
filename = file_name();
imwrite(filename, frame);
waitKey(200);
//關閉攝像頭
cap.release();
//關閉視窗
destroyWindow("Live");
//延時5Min
delay_time();
}
return 0;
}