当前位置:网站首页>[opencv introduction to mastery 9] opencv video capture, image and video conversion
[opencv introduction to mastery 9] opencv video capture, image and video conversion
2022-07-04 18:57:00 【Life is like a song, and code is like a poem】
One 、 The relationship between pictures and videos
The picture is still , Put many pictures with gradual changes , Organized by time , That is to say, video .
The video is linked , Pause every moment in the video , Then the picture is displayed .
Video frame
Every instant picture in the video pauses, and the picture is the video frameVideo frame rate
Frames per second (Frames per Second, Jane :FPS), This word is often used in film and television production and video games . Because of the special physiological structure of human eyes , If the frame rate of the picture is higher than 16 When , It would be considered coherent , This phenomenon is called visual staying . That's why film is shot one by one , Then quickly play .
For the game , First person shooters pay more attention to FPS High and low , If FPS<30 Words , The game will appear incoherent .30fps Is acceptable , But improve performance to 60fps It can obviously enhance the sense of interaction and realism , But generally speaking, more than 75fps Generally, it is not easy to notice that there is a significant increase in fluency . If the frame rate exceeds the screen refresh rate, it will only waste graphics processing power , Because the monitor can't update so fast , In this way, the frame rate exceeding the refresh rate is wasted .
Two 、 Video and picture conversion
1. Video capture
There are several steps for video capture :
Video on
Read video frames
choice
// Open the video
VideoCapture captrue = VideoCapture("test.mp4");
// Open the failure
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 << " Pixel width of video frame :" << frameWidth << endl;
cout << " Pixel height of video frame :" << framehHeight << endl;
cout << " Frame rate of recorded video ( frame / second ):" << rate << endl;
cout << " Total frames of video files :" << frameCount << endl;
cout << " The format of the image :" << format << endl;
cout << " Video duration :" << video_duration << endl;
cout << "---------------------------------------------" << endl;
namedWindow("Video", WINDOW_NORMAL);
resizeWindow("Video", 480, 270);
// Video write object
cv::VideoWriter write;
// Write the video file name
std::string outFlie = "videoTest.mp4";
// Open video file , Prepare to write
//write.open(outFlie, -1, rate, Size(frameWidth, framehHeight), true);
write.open(outFlie, CV_FOURCC('D', 'I', 'V', 'X'), rate, Size(frameWidth, framehHeight), true);
/* from 6700 Frame start reading */
bool stop = false;
cv::Mat frame;
captrue.set(CV_CAP_PROP_POS_FRAMES, 6700); // Jump to the 6700 frame
// Loop read video frames
while (!stop)
{
// Read frame
if (!captrue.read(frame))
break;
cv::imshow("Video", frame);
// write file
write.write(frame);
if (cv::waitKey(10) > 0)
{
stop = true;
}
}
// Release object
captrue.release();
write.release();
2. Video read pictures
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. Picture synthesis video
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);
}
边栏推荐
- 机器学习概念漂移检测方法(Aporia)
- Is it safe to download the mobile version of Anxin securities and open an account online
- Microservice architecture debate between radical technologists vs Project conservatives
- Wireshark抓包TLS协议栏显示版本不一致问题
- How to download files using WGet and curl
- 6.26CF模拟赛B:数组缩减题解
- Scala basic tutorial -- 13 -- advanced function
- The block:usdd has strong growth momentum
- 谷粒商城(一)
- 力扣刷题日记/day3/2022.6.25
猜你喜欢

Li Kou brush question diary /day6/6.28

输入的查询SQL语句,是如何执行的?

Nature Microbiology | 可感染阿斯加德古菌的六种深海沉积物中的病毒基因组

. Net ORM framework hisql practice - Chapter 2 - using hisql to realize menu management (add, delete, modify and check)

Scala基础教程--17--集合

Journal des problèmes de brosse à boutons de force / day6 / 6.28

Scala basic tutorial -- 20 -- akka

Microservice architecture debate between radical technologists vs Project conservatives

Halcon模板匹配

TCP两次挥手,你见过吗?那四次握手呢?
随机推荐
Angry bird design based on unity
VMware Tools和open-vm-tools的安装与使用:解决虚拟机不全屏和无法传输文件的问题
学习路之PHP--phpstudy创建项目时“hosts文件不存在或被阻止打开”
PB的扩展DLL开发(超级篇)(七)
Scala basic tutorial -- 14 -- implicit conversion
Torchdrug tutorial
[210] usage of PHP delimiter
Li Kou brush question diary /day4/6.26
爬虫初级学习
资料下载 丨首届腾讯技术开放日课程精华!
6.26CF模拟赛B:数组缩减题解
1、 Introduction to C language
6.26cf simulation race e: solution to the problem of price maximization
.NET ORM框架HiSql实战-第二章-使用Hisql实现菜单管理(增删改查)
Crawler (6) - Web page data parsing (2) | the use of beautifulsoup4 in Crawlers
Nature microbiology | viral genomes in six deep-sea sediments that can infect Archaea asgardii
工厂从自动化到数字孪生,图扑能干什么?
How to open an account is safe,
机器学习概念漂移检测方法(Aporia)
Li Kou brush question diary /day6/6.28