如何使用imwrite使用日期和时间命名图像

How to name a image using the date and time using the imwrite?

本文关键字:时间 图像 日期 何使用 imwrite      更新时间:2023-10-16

我真的很想保存多个图像,但我想使用日期和时间作为文件名来命名它。有人可以告诉我如何处理下面的代码:

//Saving Image
            Mat save_img; cap >> save_img;
            char buffer[20];
            char dateStr[20];
            char timeStr[20];
            _strdate(dateStr);
            _strtime(timeStr);
            if(save_img.empty())
            {
              std::cerr << "Something is wrong with the webcam, could not get frame." << std::endl;
            }
            // Save the frame into a file
            sprintf(buffer,"Cap %s%s.jpg",dateStr,timeStr);
            imwrite(buffer, save_img); // A JPG FILE IS BEING SAVED
            }

上面的代码不会保存任何文件图像,因此它不起作用,但不知何故它被正确编码。跑步时很慢,不知道为什么。这只是我正在处理的代码的一部分。如果您知道如何改进这一点,请发表评论。

我只是遇到了同样的问题,偶然发现了这个Reddit帖子:http://www.reddit.com/r/learnpython/comments/1e4s39/opencv_cant_use_imwrite_dont_understand_error/

因此,为了在文件名中保存带有日期和时间的文件,必须确保从字符串中删除所有":"符号!

std::string filename;
std::replace(filename.begin(), filename.end(), ':', '_');