Created
April 26, 2023 05:04
-
-
Save asmwarrior/f60c891e76361091f509db1f65c9ddff to your computer and use it in GitHub Desktop.
save image sequence to mp4 file, and read them back, see discussion here: https://stackoverflow.com/questions/76106625/what-is-the-best-way-to-save-an-image-sequence-with-different-time-intervals-in
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <opencv2/opencv.hpp> | |
using namespace cv; | |
int main() { | |
int width = 640; | |
int height = 480; | |
int fps = 30; | |
int num_frames = 100; | |
Scalar black(0, 0, 0); | |
Scalar white(255, 255, 255); | |
int font = FONT_HERSHEY_SIMPLEX; | |
double font_scale = 1.0; | |
int thickness = 2; | |
VideoWriter writer("output.mp4", VideoWriter::fourcc('M', 'P', '4', 'V'), fps, Size(width, height)); | |
for (int i = 0; i < num_frames; i++) { | |
Mat frame = Mat::zeros(height, width, CV_8UC3); | |
putText(frame, std::to_string(i), Point(width / 2 - 50, height / 2), font, font_scale, white, thickness); | |
writer.write(frame); | |
} | |
writer.release(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Here is the code to read back the mp4 file, and save the images with the file name as the index. You can see the for "00.jpg", it shows "0" in the image, so it works correctly.
There is a warning when I save the mp4 file, the warning is below: