当前位置:网站首页>【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);
}
边栏推荐
- Load test practice of pingcode performance test
- LD_ LIBRARY_ Path environment variable setting
- Is it science or metaphysics to rename a listed company?
- Lua EmmyLua 注解详解
- 被忽视的问题:测试环境配置管理
- 力扣刷题日记/day3/2022.6.25
- How to improve development quality
- Grain Mall (I)
- 2022 national CMMI certification subsidy policy | Changxu consulting
- Is it safe to download the mobile version of Anxin securities and open an account online
猜你喜欢
mysql5.7安装教程图文详解
Mysql5.7 installation tutorial graphic explanation
Journal des problèmes de brosse à boutons de force / day6 / 6.28
Scala基础教程--19--Actor
Once the "king of color TV", he sold pork before delisting
中国农科院基因组所汪鸿儒课题组诚邀加入
"In Vietnam, money is like lying on the street"
Wanghongru research group of Institute of genomics, Chinese Academy of Agricultural Sciences is cordially invited to join
Li Kou brush question diary /day7/6.30
Is it science or metaphysics to rename a listed company?
随机推荐
基于NCF的多模块协同实例
android使用SQLiteOpenHelper闪退
Russia arena data releases PostgreSQL based products
ESP32-C3入门教程 问题篇⑫——undefined reference to rom_temp_to_power, in function phy_get_romfunc_addr
【210】PHP 定界符的用法
Is it safe to download the mobile version of Anxin securities and open an account online
2022年字节跳动日常实习面经(抖音)
Load test practice of pingcode performance test
DB-Engines 2022年7月数据库排行榜:Microsoft SQL Server 大涨,Oracle 大跌
.NET ORM框架HiSql实战-第二章-使用Hisql实现菜单管理(增删改查)
The block:usdd has strong growth momentum
基于unity的愤怒的小鸟设计
VMware Tools和open-vm-tools的安装与使用:解决虚拟机不全屏和无法传输文件的问题
2022 national CMMI certification subsidy policy | Changxu consulting
An example of multi module collaboration based on NCF
MXNet对GoogLeNet的实现(并行连结网络)
TCP waves twice, have you seen it? What about four handshakes?
Microservice architecture debate between radical technologists vs Project conservatives
力扣刷题日记/day6/6.28
机器学习概念漂移检测方法(Aporia)