Read a video and save each frame of the video as a PNG image,
Download the code in this article
The first problem to be solved here is to generate a name for the image by Subscript:
This function counts the image frames in the video and outputs the file name, which is saved in PNG format.
Example: f00005.png
String namegenerate (unsigned int framecounter) {unsigned int num = num2str1 (framecounter ). length (); // <obtain the number of digits assert (Num <= 5); // <The value cannot exceed 5 digits string temp = ". \ image \ f "; // <save in. in the/image folder, the file name prefix is ffor (INT I = 0; I <5-num; I ++) {temp + = "0 "; /// <add zero before} temp + = num2str1 (framecounter) + ". PNG "; // <Add the PNG file name return temp ;}
Main Code for converting a video into an image:
/// <Select to read the video videocapture video (video_file_07); If (! Video. isopened () {cout <"Open failed! "<Endl; Return-1 ;}/// <obtain part of the video's parameter int width = 0, Height = 0, FPS = 0, fourcc = 0; width = static_cast <int> (video. get (cv_cap_prop_frame_width); Height = static_cast <int> (video. get (cv_cap_prop_frame_height); FPS = static_cast <int> (video. get (cv_cap_prop_fps); fourcc = static_cast <int> (video. get (cv_cap_prop_fourcc); cout <"input video: (" <width <"X"