当前位置:网站首页>【OpenCV入门到精通之九】OpenCV之视频截取、图片与视频互转
【OpenCV入门到精通之九】OpenCV之视频截取、图片与视频互转
2022-07-04 17:06:00 【生命如歌,代码如诗】
一、图片与视频的关系
图片是静止的,把众多的有渐变规律的图片,按时间组织起来,即构成视频。
视频是连动的,把视频中每个瞬间的画面停顿下来,则画面显示的就是图片。
视频帧
视频中每个瞬间的画面停顿下来的画面就是视频帧视频帧率
每秒显示帧数(Frames per Second,简:FPS),此词多用于影视制作和电子游戏。由于人类眼睛的特殊生理结构,如果所看画面之帧率高于16的时候,就会认为是连贯的,此现象称之为视觉停留。这也就是为什么电影胶片是一格一格拍摄出来,然后快速播放的。
对于游戏而言,第一人称射击游戏比较注重FPS的高低,如果FPS<30的话,游戏会显得不连贯。30fps就是可以接受的,但是将性能提升至60fps则可以明显提升交互感和逼真感,但是一般来说超过75fps一般就不容易察觉到有明显的流畅度提升了。如果帧率超过屏幕刷新率只会浪费图形处理的能力,因为监视器不能以这么快的速度更新,这样超过刷新率的帧率就浪费掉了。
二、视频与图片的转换
1. 视频截取
视频截取一般下面几个步骤:
视频打开
读取视频帧
选择
//打开视频
VideoCapture captrue = VideoCapture("test.mp4");
//打开失败
if (!captrue.isOpened())
{
return 1;
}
int rate = captrue.get(cv::CAP_PROP_FPS);
int frameCount = captrue.get(cv::CAP_PROP_FRAME_COUNT);
int frameWidth = captrue.get(cv::CAP_PROP_FRAME_WIDTH);
int framehHeight = captrue.get(cv::CAP_PROP_FRAME_HEIGHT);
int format = captrue.get(cv::CAP_PROP_FORMAT);
int video_duration = frameCount / rate;
cout << "---------------------------------------------" << endl;
cout << "视频帧的像素宽度:" << frameWidth << endl;
cout << "视频帧的像素高度:" << framehHeight << endl;
cout << "录制视频的帧速率(帧/秒):" << rate << endl;
cout << "视频文件总帧数:" << frameCount << endl;
cout << "图像的格式:" << format << endl;
cout << "视频时长:" << video_duration << endl;
cout << "---------------------------------------------" << endl;
namedWindow("Video", WINDOW_NORMAL);
resizeWindow("Video", 480, 270);
//视频写入对象
cv::VideoWriter write;
//写入视频文件名
std::string outFlie = "videoTest.mp4";
//打开视频文件,准备写入
//write.open(outFlie, -1, rate, Size(frameWidth, framehHeight), true);
write.open(outFlie, CV_FOURCC('D', 'I', 'V', 'X'), rate, Size(frameWidth, framehHeight), true);
/*从6700帧开始读取*/
bool stop = false;
cv::Mat frame;
captrue.set(CV_CAP_PROP_POS_FRAMES, 6700); //跳到6700帧
//循环读取视频帧
while (!stop)
{
//读取帧
if (!captrue.read(frame))
break;
cv::imshow("Video", frame);
//写入文件
write.write(frame);
if (cv::waitKey(10) > 0)
{
stop = true;
}
}
//释放对象
captrue.release();
write.release();
2. 视频读取到图片
VideoCapture captrue = VideoCapture(filename);
if (!captrue.isOpened()) {
return 1;
}
int picNum = 0;
char picName[32] = "";
cv::Mat frame;
while (1)
{
if (!captrue.read(frame))
break;
picNum++;
cout << "picNum:" << picNum << endl;
memset(picName, 0, sizeof(picName));
sprintf(picName, "pic_%d.jpg", picNum);
imwrite(picName, frame);
}
3. 图片合成视频
int idx = 0;
char picName[64];
for (idx = 1; idx < 100; idx++)
{
sprintf(picName, "pic%d_out.jpg", idx);
cout << "read picture:" << picName << endl;
Mat image = imread(string(picName));
if (image.empty())
{
cout << "read picture failed!" << endl;
continue;
}
static VideoWriter saveVideo("output2.avi", CV_FOURCC('M', 'J', 'P', 'G'), 25, Size(image.cols, image.rows));
saveVideo << image;
waitKey(50);
}
边栏推荐
- Scala基础教程--18--集合(二)
- 一种将Tree-LSTM的强化学习用于连接顺序选择的方法
- 项目通用环境使用说明
- uni-app与uviewUI实现仿小米商城app(附源码)
- Machine learning concept drift detection method (Apria)
- 被忽视的问题:测试环境配置管理
- Halcon模板匹配
- 2022年全国CMMI认证补贴政策|昌旭咨询
- TorchDrug教程
- [go language question brushing chapter] go conclusion chapter | introduction to functions, structures, interfaces, and errors
猜你喜欢
中国农科院基因组所汪鸿儒课题组诚邀加入
[2022 Jiangxi graduate mathematical modeling] curling movement idea analysis and code implementation
爬虫(6) - 网页数据解析(2) | BeautifulSoup4在爬虫中的使用
MXNet对GoogLeNet的实现(并行连结网络)
Once the "king of color TV", he sold pork before delisting
Li Kou brush question diary /day3/2022.6.25
TCP waves twice, have you seen it? What about four handshakes?
Wireshark抓包TLS协议栏显示版本不一致问题
Scala基础教程--13--函数进阶
Thawte通配符SSL证书提供的类型有哪些
随机推荐
基于unity的愤怒的小鸟设计
NBA赛事直播超清画质背后:阿里云视频云「窄带高清2.0」技术深度解读
ThreadLocal原理与使用
vbs或vbe如何修改图标
What if the self incrementing ID of online MySQL is exhausted?
力扣刷题日记/day7/6.30
[211] go handles the detailed documents of Excel library
Is it science or metaphysics to rename a listed company?
File processing examples of fopen, FREAD, fwrite, fseek
力扣刷题日记/day4/6.26
How to download files using WGet and curl
How to modify icons in VBS or VBE
General environmental instructions for the project
Mysql5.7 installation tutorial graphic explanation
Scala基础教程--13--函数进阶
爬虫初级学习
The money circle boss, who is richer than Li Ka Shing, has just bought a building in Saudi Arabia
78 year old professor Huake impacts the IPO, and Fengnian capital is expected to reap dozens of times the return
被忽视的问题:测试环境配置管理
Detailed explanation of the maturity classification of ITSS operation and maintenance capability | one article clarifies the ITSS certificate