当前位置:网站首页>[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);
}
边栏推荐
猜你喜欢
Torchdrug tutorial
如何提高开发质量
Scala basic tutorial -- 19 -- actor
How to modify icons in VBS or VBE
蓝桥:合根植物
My colleagues quietly told me that flying Book notification can still play like this
Basic tutorial of scala -- 16 -- generics
MySQL common add, delete, modify and query operations (crud)
Unity 制作旋转门 推拉门 柜门 抽屉 点击自动开门效果 开关门自动播放音效 (附带编辑器扩展代码)
能源行业的数字化“新”运维
随机推荐
Microservice architecture debate between radical technologists vs Project conservatives
技术分享 | 接口测试价值与体系
输入的查询SQL语句,是如何执行的?
Neglected problem: test environment configuration management
蓝桥:合根植物
提升复杂场景三维重建精度 | 基于PaddleSeg分割无人机遥感影像
LD_ LIBRARY_ Path environment variable setting
I wrote a learning and practice tutorial for beginners!
Reptile elementary learning
【210】PHP 定界符的用法
能源行业的数字化“新”运维
How to open an account is safe,
[2022 Jiangxi graduate mathematical modeling] curling movement idea analysis and code implementation
基于C语言的菜鸟驿站管理系统
Li Kou brush question diary /day3/2022.6.25
[go ~ 0 to 1] read, write and create files on the sixth day
Li Chi's work and life summary in June 2022
[211] go handles the detailed documents of Excel library
NBA赛事直播超清画质背后:阿里云视频云「窄带高清2.0」技术深度解读
使用SSH