基本信息
源码名称:opencv:视频图片相互转换程序
源码大小:5.50KB
文件格式:.cpp
开发语言:C/C++
更新时间:2019-06-11
友情提示:(无需注册或充值,赞助后即可获取资源下载链接)
嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300
本次赞助数额为: 2 元×
微信扫码支付:2 元
×
请留下您的邮箱,我们将在2小时内将文件发到您的邮箱
源码介绍
实现三个功能:Video2Img:视频转图片序列;Img2Video:序列图片转视频;Img2VideoRect:图片转视频,并指定区域。
实现三个功能:Video2Img:视频转图片序列;Img2Video:序列图片转视频;Img2VideoRect:图片转视频,并指定区域。
#include <iostream> #include <io.h> #include <string> #include <fstream> #include <stdio.h> #include <direct.h> #include <opencv2/core/core.hpp> #include <opencv2/imgproc/imgproc.hpp> #include <opencv2/contrib/contrib.hpp> #include <opencv2/highgui/highgui.hpp> #include <opencv2/video/video.hpp> using namespace std; vector<string> split(const string& str, const string& delim); int Video2Img(const std::string videoName, const std::string imgPath, int stepFrameNum); int Img2Video(const std::string imgPath, const std::string videoName); int Img2VideoRect(const std::string imgPath, const std::string filePath, const std::string videoName); int main(int argc, char *argv[]) { /*int model = stoi(argv[1]); if(model == 0) { string videoName = argv[2]; string imgPath = argv[3]; int stepFrameNum = stoi(argv[4]); Video2Img(videoName, imgPath, stepFrameNum); } if(model == 1) { string imgPath = argv[2]; string filePath = argv[3]; string videoName = argv[4]; Img2Video(imgPath, filePath, videoName); } */ string videoName = "E:/Data/VID_20190111.avi"; string imgPath = "E:/Data/image"; int stepFrameNum = 1; Img2Video(imgPath, videoName); system("pause"); return 0; } int Video2Img(const std::string videoName, const std::string imgPath, int stepFrameNum) { cv::VideoCapture video(videoName); if (!video.isOpened()) { std::cout << "Read video Failed !" << std::endl; return 0; } double frame_num = video.get(CV_CAP_PROP_FRAME_COUNT); std::cout << "total frame number is: " << frame_num << std::endl; if (_access(imgPath.c_str(), 0) == -1) { std::cout << imgPath <<" not existence!" << std::endl; std::cout << "Create folder" << imgPath << " !" << std::endl; if (_mkdir(imgPath.c_str()) == -1) { return 0; } } cv::Mat img; int index = 0; int count = 0; int step = stepFrameNum * video.get(CV_CAP_PROP_FPS); char buffer[50]; while (1) { video >> img; if (count > frame_num && img.empty()) { return 0; } cv::Mat cutImg(img, cv::Range(120,720), cv::Range(90,730)); cv::transpose(cutImg, cutImg); cv::flip(cutImg, cutImg, 1); //if (count % step == 0) { sprintf(buffer, "%05d", index); cv::imwrite(imgPath "/0" buffer ".jpg", cutImg); std::cout << "*****************" << index << std::endl; } std::cout << frame_num << "/" << count << std::endl; } video.release(); return 0; } int Img2Video(const std::string imgPath, const std::string videoName) { cv::Directory dir; std::vector< std::string > imgs = dir.GetListFiles(imgPath, "*.jpg", true); cv::Mat img; img = cv::imread(imgs[0]); if (img.empty()) { return 0; } cv::VideoWriter outputVideo(videoName, CV_FOURCC('M', 'J', 'P', 'G'), 30, cv::Size(img.cols, img.rows)); for (int i = 0; i < imgs.size(); i ) { img = cv::imread(imgs[i]); if (img.empty()) { return 0; } outputVideo << img; std::cout << "******" << i << std::endl; } std::cout << "****** End! " << std::endl; outputVideo.release(); return 0; } int Img2VideoRect(const std::string imgPath, const std::string filePath, const std::string videoName) { cv::Directory dir; std::vector< std::string > imgs = dir.GetListFiles(imgPath,"*.jpg",true); cv::Mat img; img = cv::imread(imgs[0]); if (img.empty()) { return 0; } cv::VideoWriter outputVideo(videoName, CV_FOURCC('M', 'J', 'P', 'G'), 2, cv::Size(img.cols, img.rows)); for (int i = 0; i < imgs.size(); i ) { img = cv::imread(imgs[i]); if (img.empty()) { return 0; } string txtPath = filePath "/" split(split(imgs[i], "/")[split(imgs[i], "/").size() - 1], ".")[0] ".txt"; fstream txt(txtPath); std::cout << txtPath << std::endl; if (txt.is_open()) { char line[256]; while (txt.getline(line, 256)) { string iclass = split(line, " ")[0]; float center_x = stof(split(line, " ")[1])*img.cols; float center_y = stof(split(line, " ")[2])*img.rows; float w = stof(split(line, " ")[3])*img.cols; float h = stof(split(line, " ")[4])*img.rows; float x = center_x - w / 2.0; float y = center_y - h / 2.0; if (iclass == "0") cv::rectangle(img, cv::Point(int(x), int(y)), cv::Point(int(x w), int(y h)), cv::Scalar(0, 0, 255), 2); if (iclass == "1") cv::rectangle(img, cv::Point(int(x), int(y)), cv::Point(int(x w), int(y h)), cv::Scalar(0, 255, 0), 2); if (iclass == "2") cv::rectangle(img, cv::Point(int(x), int(y)), cv::Point(int(x w), int(y h)), cv::Scalar(255, 0, 255), 2); if (iclass == "3") cv::rectangle(img, cv::Point(int(x), int(y)), cv::Point(int(x w), int(y h)), cv::Scalar(255, 0, 0), 2); if (iclass == "4") cv::rectangle(img, cv::Point(int(x), int(y)), cv::Point(int(x w), int(y h)), cv::Scalar(100, 0, 50), 2); } } outputVideo << img; std::cout << "******" << i<< std::endl; } std::cout << "****** End! " << std::endl; outputVideo.release(); return 0; } vector<string> split(const string& str, const string& delim) { vector<string> res; if ("" == str) return res; //先将要切割的字符串从string类型转换为char*类型 char * strs = new char[str.length() 1]; //不要忘了 strcpy(strs, str.c_str()); char * d = new char[delim.length() 1]; strcpy(d, delim.c_str()); char *p = strtok(strs, d); while (p) { string s = p; //分割得到的字符串转换为string类型 res.push_back(s); //存入结果数组 p = strtok(NULL, d); } return res; }