当前位置:网站首页>[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);
}
边栏推荐
- Scala basic tutorial -- 12 -- Reading and writing data
- 如何提高开发质量
- Scala基础教程--14--隐式转换
- ITSS运维能力成熟度分级详解|一文搞清ITSS证书
- Lua EmmyLua 注解详解
- Basic tutorial of scala -- 16 -- generics
- Unity makes revolving door, sliding door, cabinet door drawer, click the effect of automatic door opening and closing, and automatically play the sound effect (with editor extension code)
- ESP32-C3入门教程 问题篇⑫——undefined reference to rom_temp_to_power, in function phy_get_romfunc_addr
- 力扣刷题日记/day5/2022.6.27
- 输入的查询SQL语句,是如何执行的?
猜你喜欢
TCP waves twice, have you seen it? What about four handshakes?
[HCIA continuous update] WAN technology
I wrote a learning and practice tutorial for beginners!
How to modify icons in VBS or VBE
Numpy 的仿制 2
线上MySQL的自增id用尽怎么办?
Journal des problèmes de brosse à boutons de force / day6 / 6.28
2022 ByteDance daily practice experience (Tiktok)
[mathematical modeling of graduate students in Jiangxi Province in 2022] analysis and code implementation of haze removal by nucleation of water vapor supersaturation
Li Kou brush question diary /day7/6.30
随机推荐
力扣刷题日记/day4/6.26
Esp32-c3 introductory tutorial questions ⑫ - undefined reference to ROM_ temp_ to_ power, in function phy_ get_ romfunc_ addr
如何提高开发质量
Interview summary of large factory Daquan II
How to open an account is safe,
How to modify icons in VBS or VBE
Microservice architecture debate between radical technologists vs Project conservatives
Scala basic tutorial -- 12 -- Reading and writing data
Redis master-slave replication
Scala基础教程--12--读写数据
网上开户安全吗?是真的吗?
ByteDance dev better technology salon was successfully held, and we joined hands with Huatai to share our experience in improving the efficiency of web research and development
Scala basic tutorial -- 19 -- actor
[system disk back to U disk] record the operation of system disk back to U disk
力扣刷题日记/day3/2022.6.25
Crawler (6) - Web page data parsing (2) | the use of beautifulsoup4 in Crawlers
Li Kou brush question diary /day7/6.30
How is the entered query SQL statement executed?
I wrote a learning and practice tutorial for beginners!
Android uses sqliteopenhelper to flash back