//用于裁剪视频
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
using namespace std;
using namespace cv;
int main()
{
// 截取小视频
// std::string srcVideoPath = "/data_2/project/video_baoding/data/保定/20190815公交电警视频/ch01_20190815072226_20190815072509.mp4";
//
// std::string startTime = "00:00:24";
// std::string endTime = "00:00:30";
// std::string saveVideoPath = "/data_2/project/video_baoding/data/保定/";
//
// std::string videoName = saveVideoPath + startTime + "_" + endTime + ".mp4";
// std::string commondstr = "ffmpeg -i " + srcVideoPath + " -vcodec copy -ss " + startTime + " -to " + endTime + " " + videoName + " -y";
// system(commondstr.c_str());
//
// return 0;
string videopath = "/media/em/data_1/project/video/data/ch06_20200714_171223_0026_00_147521536_007869384.mp4";
string savePath = "/media/em/data_1/project/video/data_result/ch06_20200714_171223_0026_00_147521536_007869384";
string targetImageSavePath = "/media/em/data_1/project/video/data_crop/ch06_20200714_171223_0026_00_147521536_007869384";
string command1, command2;
command1 = "mkdir -p " + savePath;
command2 = "mkdir -p " + targetImageSavePath;
system(command1.c_str());
system(command2.c_str());
//对视频每隔4帧抽取保存
VideoCapture capture(videopath);
if (!capture.isOpened())
cout << "打开失败!" << endl;
long totalFrameNumber = capture.get(CV_CAP_PROP_FRAME_COUNT);
cout << "整个视频共" << totalFrameNumber << "帧" << endl;
long frameStart = 1; //设置开始帧
capture.set(CV_CAP_PROP_POS_FRAMES, frameStart);
cout << "从第" << frameStart << "帧开始读" << endl;
long frameStop = totalFrameNumber; //设置结束帧
if (frameStop < frameStart){
cout << "结束帧小于开始帧,error!" << endl;
return -1;
}
else{
cout << "结束帧为:第" << frameStop << "帧" << endl;
}
bool stop = false;
Mat frame;
long currentFrame = frameStart;
long skip_frame_num = 4; //跳帧间隔设置(每4帧)
while(!stop){
if (!capture.read(frame)){
cout << "read video frame failed!" << endl;
return -1;
}
cout << "正在写第" << currentFrame << "帧" << endl;
stringstream str1;
str1 << savePath << "/" << currentFrame << ".png";
cout << "图片保存的路径是:" << str1.str( ) << endl;
imwrite(str1.str(), frame);
cv::Rect rect1(10, 10, 20, 20);//框的区域
cv::Rect rect2(8, 8, 24, 24);//截图区域
cv::rectangle(frame, rect1, Scalar(255, 0, 0),1, LINE_8,0); //画框
Mat target_image = frame(rect2);
stringstream str2;
str2 << targetImageSavePath << "/" << currentFrame << ".png";
cout << "裁剪后带框的图片保存的路径是:" << str2.str( ) << endl;
imwrite(str2.str(), target_image);
currentFrame = currentFrame + skip_frame_num;
if (currentFrame > frameStop){
stop = true;
}
}
return 1; //对视频每隔4帧保存为图像
}
C++
最新推荐文章于 2024-07-20 22:56:53 发布