//将视频转化问AVI格式,参考了网上的部分资料,希望对大家有帮助int VideoToImage(char* videoName, char* outDir, char* imgExt, int maxFrameCount)
{
VideoCapture cap(videoName);
if (!cap.isOpened())
{
cout << "Failed open video file!" << endl;
return1;
}
if (cap.get(CAP_PROP_FRAME_COUNT) < 2)
{
cout << "The video must have at least two frames." << endl;
return1;
}
//保存图片的文件夹路径一定要有,因为OpenCV不会自动创建文件夹if (_access(outDir, 0) == -1)
{
recursive_mkdir(outDir);
std::cout << "the ouput directory does not exist, and the have been created autonomously!" << std::endl;
}
char cur_fn[255];//保存当前帧所得图片的文件名
cv::Mat pImg;
int frame = 0;
double rate = cap.get(CAP_PROP_FPS);
double delay = 1000 / rate;
cap.read(pImg);
bool stop = true;
//while (!pImg.empty() && (frame<maxFrameCount))while (cap.isOpened() && stop)
{
if (frame < maxFrameCount)
{
frame++;
strcpy_s(cur_fn, "");
sprintf_s(cur_fn, "%s%d%s", outDir, frame, imgExt);//这里的设置适合形如 123.jpg 124.jpg的图片序列
imwrite(cur_fn, pImg);
cap.read(pImg);
}
else
stop = false;
}
pImg.release();
cap.release();
return frame;
}
int recursive_mkdir(char *dir)
{
//分解路径名E:\\AA\\BB\\CC\\
//
string str = dir;
intindex = 0;
int i = 0;
while (1)
{
string::size_type pos = str.find("\\", index);
string str1;
str1 = str.substr(0, pos);
if (pos != -1 && i > 0)
{
if (_access(str1.c_str(), 0) == -1)
{
_mkdir(str1.c_str());
}
}
if (pos == -1)
{
break;
}
i++;
index = pos + 1;
}
return0;
}
图片序列转成视频
int ImageToVideo(char* outDir, char* videoName, char* inputDir, int startFrame, int endFrame, int imgW, int imgH, char* imgExt, double fps, int isColor, int fourcc)
{
//判断输入文件夹是否存在if (_access(inputDir, 0) == -1)
{
std::cout << "the input directory does not exist!" << std::endl;
return0;
}
//判断输出文件夹是否创建 若没有则创建;若为NULL则默认当前工作目录char fullVideoName[255];//输出视频的完整文件名:路径+文件名
strcpy_s(fullVideoName, "");
if (outDir == NULL)
{
sprintf_s(fullVideoName, "%s", videoName);//把videoName打印成一个字符串保存在fullVideoName 中
}
else
{
if (_access(outDir, 0) == -1)
{
_mkdir(outDir);
}
sprintf_s(fullVideoName, "%s%s", outDir, videoName);//将字符串outDir和videoName连接起来,打印,保存在fullVideoName中
}
int frameCount = 0;
//CvVideoWriter *pWriter = NULL;
Size size = Size(imgW, imgH);
VideoWriter pWriter(videoName, fourcc, fps, size, isColor);//CREATE WRITER
cv::Mat pImg;
char cur_fn[500];//表示某张图片的路径while (startFrame <= endFrame)
{
strcpy(cur_fn, "");
char name[] = ".jpg";
sprintf(cur_fn, "%s%s%07d%s", inputDir, imgExt, startFrame, name);//注:这里需要根据你的文件名来做相应的更改//cout <<"输入文件夹名为:"<< cur_fn << endl;
pImg = imread(cur_fn, isColor);
if (pImg.empty())
{
cout << "can't open an image file" << endl;
return frameCount;
}
pWriter << pImg;
waitKey(1);
cout << "Write frame " << startFrame << std::endl;
startFrame++;
pImg.release();
frameCount++;
}
//cvReleaseVideoWriter(&pWriter);
pWriter.release();
rename(videoName, fullVideoName);//移动文件到指定文件夹return frameCount;
}
调用测试
char* inputDir = "D:/zhlWorkDocs/运动跟踪/vot2015/pedestrian2/";
char* videoName = "pedestrian.avi";
char* outDir = "D:/zhlWorkDocs/video/";
double t = getTickCount();
int frames = ImageToVideo(outDir, videoName, inputDir, 1, 500, 480, 640, "0", 20, 1, CAP_PROP_FOURCC);
cout << "ImageToVideo resumes: " << (getTickCount() - t) / getTickFrequency() <<"ms"<< endl;
std::cout << "total frames " << frames << " have been write to video." << std::endl;
char out_file[500] = "";
sprintf(out_file,"%s%s",outDir,videoName);
capVideo.open(out_file);
if (!capVideo.isOpened()) { // if unable to open video filecout << "error reading video file!" << endl << endl; // show error message
_getch(); // it may be necessary to change or remove this line if not using Windowsreturn0; // and exit program
}
if (capVideo.get(CAP_PROP_FRAME_COUNT) < 2) {
cout << "error: video file must have at least two frames!";
_getch();
return0;
}
视频的读取
VideoCapture capVideo;
cv::Mat imgFrame1;
capVideo.open("D:/zhlWorkDocs/video/768x576.avi");
if (!capVideo.isOpened()) { //if unable to open video file
cout << "error reading video file!" << endl << endl; // show error message
_getch(); // it may be necessary to change or remove this line ifnot using Windowsreturn0; //and exit program
}
if (capVideo.get(CAP_PROP_FRAME_COUNT) < 2) {
cout << "error: video file must have at least two frames!";
_getch();
return0;
}
capVideo.read(imgFrame1);
图片序列的读取
Mat image;
VideoCapture sequence;
for(int i = 1;i < 200;i ++)
{
char first_file[500];
sprintf(first_file, "D:\\zhlWorkDocs\\image_sequences\\4\\%d.jpg", i);
sequence.open(first_file);
if (!sequence.isOpened())
{
cerr << "Failed to open the image sequence!\n" << endl;
return1;
}
//namedWindow("Image sequence", 1);
sequence.read(image);
if(image.empty())
{
cout << "End of Sequence" << endl;
break;
}
imshow("Image sequence", image);
waitKey();
}