当前位置:   article > 正文

opencv 写入 mp4 文件 C++用VideoWriter写入视频

videowriter

C++用VideoWriter写入视频

warnning:要用resize转换大小.

  1. #include <iostream>
  2. #include <stdio.h>
  3. #include <opencv2/opencv.hpp>
  4. using namespace cv;
  5. using namespace std;
  6. int main() {
  7. string img_name = "/*/*.png";
  8. string save_path = "*.avi";
  9. cv::Mat img = cv::imread(img_name);
  10. cv::VideoWriter track_writer;
  11. track_writer.open (save_path.c_str(), cv::VideoWriter::fourcc('M', 'P', '4', '2'), 30.0, cv::Size(960, 540));
  12. if(!track_writer.isOpened())
  13. {
  14. assert("track writer open failed!\n");
  15. }
  16. for (int i = 0; i < 100; ++i)
  17. {
  18. cv::imshow("test", img);
  19. cv::resize(img, img, cv::Size(960, 540));
  20. track_writer.write(img);
  21. cout << "frame" << i << endl;
  22. }
  23. return 0;
  24. }

opencv读取视频时跳帧处理,grab只缓存帧数据,retrieve做对应的解码

  1. cv::Mat frame;
  2. while (true) {
  3. for (size_t i = 0; i < 100; i++)
  4. {
  5. cap.grab();
  6. }
  7. bool grabbed = cap.grab();
  8. if (!grabbed) {
  9. std::cout << "Error: Video stream ended or unable to grab the frame." << std::endl;
  10. continue;
  11. }
  12. // 解码图像数据并获取图像
  13. int ret = cap.retrieve(frame);
  14. if (ret){
  15. cv::imshow("frame", frame);
  16. cv::waitKey(0);
  17. }
  18. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/2023面试高手/article/detail/484851
推荐阅读
相关标签
  

闽ICP备14008679号