从 openCV 中保存多个帧,并以时间作为其名称

Saving multiple frames from openCV with time as their name

本文关键字:时间 openCV 保存      更新时间:2023-10-16

我正在尝试编写一个代码来保存 openCV 视频流中的一些帧。 保存的图像的名称应该是时间: 这是我的代码:

#include "opencv2/opencv.hpp"
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include<conio.h>
#include <time.h>
using namespace cv;
using namespace std;
 int main(){
int key = 0;
char dateStr[9];
char timeStr[9];
_strdate(dateStr);
_strtime(timeStr);
char buffer[20];
 VideoCapture cap(1); // open the default camera
if(!cap.isOpened())  // check if we succeeded
    return -1;

namedWindow("image",1);
while(key!=27)
{
    Mat frame;
    cap >> frame; // get a new frame from camera
    imshow("image", frame);
    if(key==13){
        sprintf(buffer,"%s%s.tif",dateStr,timeStr);
        imwrite(buffer,frame);
        }
    key = waitKey(1000); 
} 
destroyAllWindows();
// the camera will be deinitialized automatically in VideoCapture destructor
return 0;
}               

当我运行它时,什么也没发生,我不止一次点击了 Enter 按钮,什么也没发生,顺便说一媒体太慢了。 任何想法!!.感谢您的帮助

2件事

流式传输速度很慢,因为您等待的时间太长。 更改 key = waitKey(1000); 中的值以更改程序的帧速率。

此外,如果您按回车键,则只会保存下一帧,而不会保存当前看到的帧。 应将该key = waitKey(1000);语句移到if语句之前。