视频转图像
#include <iostream>
#include <io.h>
#include <fstream>
#include <cstring>
#include <sstream>
#include <opencv2/opencv.hpp>
#include <opencv2/core.hpp>
using namespace cv;
using namespace std;
void video2image(string dir, string name)
{
VideoCapture capture(dir);
int frame_num = capture.get(CAP_PROP_FRAME_COUNT);
for (size_t i = 1; i <= frame_num; i++)
{
Mat frame;
capture >> frame;
stringstream str;
if (!frame.empty())
{
str << setw(5) << setfill('0') << i << ".jpg";
imwrite(name + str.str(), frame);
}
}
}
图像转视频
#include <iostream>
#include <io.h>
#include <fstream>
#include <cstring>
#include <sstream>
#include <opencv2/opencv.hpp>
#include <opencv2/core.hpp>
using namespace cv;
using namespace std;
void getFiles(string path, vector<string>& files)
{
intptr_t hFile = 0;
struct _finddata_t fileinfo;
string p;
if ((hFile = _findfirst(p.assign(path).append("\\*").c_str(), &fileinfo)) != -1)
{
do
{
if ((fileinfo.attrib & _A_SUBDIR))
{
if (strcmp(fileinfo.name, ".") != 0 && strcmp(fileinfo.name, "..") != 0)
getFiles(p.assign(path).append("\\").append(fileinfo.name), files);
}
else
{
files.push_back(p.assign(path).append("\\").append(fileinfo.name));
}
} while (_findnext(hFile, &fileinfo) == 0);
_findclose(hFile);
}
}
void image2video(string path)
{
vector<string> files;
getFiles(path, files);
Mat image = imread(files[0]);
string outputFile = "..//output.avi";
VideoWriter video;
video.open(outputFile, VideoWriter::fourcc('M', 'P', '4', '2'), 10, Size(image.cols, image.rows));
for (int i = 0; i < files.size(); i++)
{
image = imread(files[i]);
video << image;
}
}