实例介绍
【实例简介】实现三个功能: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;
}
好例子网口号:伸出你的我的手 — 分享!
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明


网友评论
我要评论