前言
视频处理过程中,会用到对等长的视频进行处理,此时要对大视频进行分割。
实现步骤
1.批量读取视频集;
2.视频分割;
测试代码
1.批量读取视频集;
/************************************************************************
* Copyright(c) 2017 AMY
* All rights reserved.
*
* File:
* Brief:
* Version: 1.0
* Author:
* Email:
* Date: 2017/10/30
* History:
* 20171030: ; ************************************************************************/
#include <iostream>
#include <string>
#include <vector>
#include "opencv2/opencv.hpp"
#include "opencv2/contrib/contrib.hpp" using namespace std;
using namespace cv; void videoSegmentation(string videoName, string videoSegFolder, int segLen, int k); int main()
{
string videoFolder = ".\\dataset\\p1";
string videoSegFolder = ".\\segVideos\\";
cv::Directory dir;
string videoType = "*.avi";
bool addPath = true;
int segLen = ;
vector<string> videosName = dir.GetListFiles(videoFolder, videoType, addPath);
string videoFile;
cout << videosName.size() << endl;
for (int i = ; i < videosName.size(); i++)
{
videoSegmentation(videosName[i], videoSegFolder, segLen, i+);
}
return ;
}
2.视频分割;
void videoSegmentation(string videoName, string videoSegFolder, int segLen, int k)
{
VideoCapture capture(videoName);
if (!capture.isOpened())
{
cout << "Loading error!" << endl;
abort();
}
int nFrame = (int)capture.get(CV_CAP_PROP_POS_FRAMES);
int fps = (int)capture.get(CV_CAP_PROP_FPS);
int width = (int)capture.get(CV_CAP_PROP_FRAME_WIDTH);
int height = (int)capture.get(CV_CAP_PROP_FRAME_HEIGHT);
Size videoSize(width, height);
VideoWriter writer;
string videoSeg = videoSegFolder + to_string(k) + "_1.avi";
writer.open(videoSeg, CV_FOURCC('D', 'I', 'V', ''), fps, videoSize);
Mat frame;
int videoNum = ;
while ()
{
if (capture.read(frame))
{
videoNum++;
writer << frame;
if (!(videoNum % (fps*segLen)))
{
videoSeg = videoSegFolder + to_string(k) + "_" + to_string(videoNum/(fps*segLen)) + ".avi";
writer.open(videoSeg, CV_FOURCC('D', 'I', 'V', ''), fps, videoSize);
}
else
{ }
}
else
{
break;
}
}
capture.release();
return; }
注意,OpenCV只支持avi的格式,而且生成的视频文件不能大于2GB,而且不能添加音频。
补充:
如果生成视频的颜色不正确,很有可能是中间处理过程中图片的R/G/B通道发生了改变,也有可能是编解码格式问题。
牢记:opencv的颜色通道顺序默认都是BGR!!!
完