源码
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dirent.h>
#include <unistd.h>
#include <iostream>
#include <vector>
#include <libgen.h>
#include <opencv2/opencv.hpp>
#include <iostream>
#include <unistd.h>
using namespace cv;
using namespace std;
vector <string> relativePath;
vector <string> fileName;
int get_fileName_fileSuffix(const char *_filePaht, std::string &_fullFileName, std::string &_fileName, std::string &_fileSuffix, bool _dot = true) {
if (!_filePaht) return -1;
std::string str = _filePaht;
if (str.empty()) return -1;
if (str.at(0) == '.') return -1;
if (str.at(str.size() - 1) == '.') return -1;
char c = '\0';
#if defined (_WIN64) || defined (WIN32) || defined (_WIN32)
c = '\\';
#else
c = '\/';
#endif
std::string filename = strrchr(_filePaht, c) == NULL ? "" : strrchr(_filePaht, c) + 1;
if (filename == "") {
filename = _filePaht;
} else {
}
size_t _size = filename.rfind(".");
_fileName = filename.substr(0, _size);
std::string strsuffix = "";
if (_size != -1) {
if (_dot == true) {
strsuffix = filename.substr(_size);
} else {
strsuffix = filename.substr(_size + 1);
}
}
_fileSuffix = strsuffix;
_fullFileName = filename;
return _size;
}
int readFileList(char *basePath)
{
DIR *dir;
struct dirent *ptr;
char base[1000];
if ((dir=opendir(basePath)) == NULL)
{
perror("Open dir error...");
exit(1);
}
while ((ptr=readdir(dir)) != NULL)
{
if(strcmp(ptr->d_name,".")==0 || strcmp(ptr->d_name,"..")==0)
continue;
else if(ptr->d_type == 8){
printf("d_name:%s/%s\n",basePath,ptr->d_name);
string s1 = ptr->d_name;
fileName.push_back(s1);
string s = basePath;
s = s + "/";
s = s + +ptr->d_name;
relativePath.push_back(s);
std::string fullFilename, filename, fileSuffix;
int r = get_fileName_fileSuffix(s1.c_str(), fullFilename, filename, fileSuffix, true);
}else if(ptr->d_type == 10){
printf("d_name:%s/%s\n",basePath,ptr->d_name);
}else if(ptr->d_type == 4)
{
memset(base,'\0',sizeof(base));
strcpy(base,basePath);
strcat(base,"/");
strcat(base,ptr->d_name);
readFileList(base);
}
}
closedir(dir);
return 1;
}
int main(int argc, const char *argv[])
{
DIR *dir;
#if 0
char basePath[1000];
memset(basePath,'\0',sizeof(basePath));
getcwd(basePath, 999);
#else
char *basePath = (char *)argv[1];
#endif
VideoWriter w1;
int encode_type = VideoWriter::fourcc('M', 'J', 'P', 'G');
bool isColor = true;
double fps = 25.0;
string filename = "test_save.avi";
w1.open(filename, encode_type, fps, cv::Size(1280, 720), isColor);
if (!w1.isOpened()) {
cout << "视频打开失败" << endl;
}
int id = 0;
int maxNum = 300;
for (int i = 0; i<maxNum; i++)
{
string filePathName = argv[1] ;
filePathName = filePathName+ "/" + to_string(i) + ".png";
if (! access(filePathName.c_str() ,0) )
{
Mat img;
img = imread(filePathName.c_str());
if (img.empty())
{
cout << "读取失败" << endl;
if (w1.isOpened())
w1.release();
return -1;
}
w1.write(img);
imshow("test", img);
waitKey(0);
}
}
return 0;
}
编译
g++ img2video.cpp -o img2video `pkg-config --cflags --libs opencv`
运行
./img2video ../results
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)