当前位置:网站首页>【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);
}
边栏推荐
- MySQL common add, delete, modify and query operations (crud)
- Wireshark抓包TLS协议栏显示版本不一致问题
- General environmental instructions for the project
- Redis master-slave replication
- MXNet对GoogLeNet的实现(并行连结网络)
- 网上开户安全吗?是真的吗?
- C语言打印练习
- I wrote a learning and practice tutorial for beginners!
- Scala基础教程--13--函数进阶
- 删除二叉搜索树中的节点附图详解
猜你喜欢
【Go语言刷题篇】Go完结篇|函数、结构体、接口、错误入门学习
ThreadLocal原理与使用
NBA赛事直播超清画质背后:阿里云视频云「窄带高清2.0」技术深度解读
基于unity的愤怒的小鸟设计
VMware Tools和open-vm-tools的安装与使用:解决虚拟机不全屏和无法传输文件的问题
Five thousand words to clarify team self-organization construction | Liga wonderful talk
输入的查询SQL语句,是如何执行的?
【2022年江西省研究生数学建模】冰壶运动 思路分析及代码实现
Crawler (6) - Web page data parsing (2) | the use of beautifulsoup4 in Crawlers
Li Kou brush question diary /day7/2022.6.29
随机推荐
输入的查询SQL语句,是如何执行的?
力扣刷題日記/day6/6.28
File processing examples of fopen, FREAD, fwrite, fseek
Redis master-slave replication
SIGMOD’22 HiEngine论文解读
"In Vietnam, money is like lying on the street"
Microservice architecture debate between radical technologists vs Project conservatives
蓝桥:合根植物
学习路之PHP--phpstudy创建项目时“hosts文件不存在或被阻止打开”
Mysql5.7 installation tutorial graphic explanation
被忽视的问题:测试环境配置管理
【云端心声 建议征集】云商店焕新升级:提有效建议,赢海量码豆、华为AI音箱2!
力扣刷题日记/day7/6.30
Scala基础教程--15--递归
基于unity的愤怒的小鸟设计
PB的扩展DLL开发(超级篇)(七)
未来几年中,软件测试的几大趋势是什么?
Tutorial on the use of Huawei cloud modelarts (with detailed illustrations)
Behind the ultra clear image quality of NBA Live Broadcast: an in-depth interpretation of Alibaba cloud video cloud "narrowband HD 2.0" technology
Li Kou brush question diary /day8/7.1