当前位置:网站首页>[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);
}
边栏推荐
- [211] go handles the detailed documents of Excel library
- Digital "new" operation and maintenance of energy industry
- Microservice architecture debate between radical technologists vs Project conservatives
- [go ~ 0 to 1] read, write and create files on the sixth day
- Behind the ultra clear image quality of NBA Live Broadcast: an in-depth interpretation of Alibaba cloud video cloud "narrowband HD 2.0" technology
- How to open an account is safe,
- Angry bird design based on unity
- [cloud native] what is the "grid" of service grid?
- What types of Thawte wildcard SSL certificates provide
- 怎么开户才是安全的,
猜你喜欢

力扣刷题日记/day7/2022.6.29

Nature microbiology | viral genomes in six deep-sea sediments that can infect Archaea asgardii

Scala基础教程--18--集合(二)

Scala基础教程--19--Actor

激进技术派 vs 项目保守派的微服务架构之争

Deleting nodes in binary search tree

How is the entered query SQL statement executed?

Just today, four experts from HSBC gathered to discuss the problems of bank core system transformation, migration and reconstruction

Scala basic tutorial -- 20 -- akka

Scala基础教程--17--集合
随机推荐
[mathematical modeling of graduate students in Jiangxi Province in 2022] analysis and code implementation of haze removal by nucleation of water vapor supersaturation
[go ~ 0 to 1] read, write and create files on the sixth day
基于lex和yacc的词法分析器+语法分析器
一种将Tree-LSTM的强化学习用于连接顺序选择的方法
TCP waves twice, have you seen it? What about four handshakes?
ESP32-C3入门教程 问题篇⑫——undefined reference to rom_temp_to_power, in function phy_get_romfunc_addr
uni-app与uviewUI实现仿小米商城app(附源码)
Li Kou brush question diary /day7/6.30
6.26CF模拟赛B:数组缩减题解
Installation and use of VMware Tools and open VM tools: solve the problems of incomplete screen and unable to transfer files of virtual machines
Li Kou brush question diary /day1/2022.6.23
Esp32-c3 introductory tutorial questions ⑫ - undefined reference to ROM_ temp_ to_ power, in function phy_ get_ romfunc_ addr
技术分享 | 接口测试价值与体系
Scala基础教程--17--集合
LD_ LIBRARY_ Path environment variable setting
Scala basic tutorial -- 14 -- implicit conversion
输入的查询SQL语句,是如何执行的?
【OpenCV入门到精通之九】OpenCV之视频截取、图片与视频互转
6.26cf simulation match B: solution to array reduction problem
Unity 制作旋转门 推拉门 柜门 抽屉 点击自动开门效果 开关门自动播放音效 (附带编辑器扩展代码)