• opencv之VideoCapture与VideoWriter笔记


    opencv版本:opencv3.4.1 

    1. VideoCapture类 

     作用: 捕获视频文件,图像序列或摄像头;

    1. class VideoCapture
    2. {
    3. public:
    4. //构造
    5. VideoCapture();
    6. VideoCapture(const String& filename);
    7. //apiPreference参数取值:cv::CAP_FFMPEG or cv::CAP_IMAGES or cv::CAP_DSHOW等
    8. VideoCapture(const String& filename, int apiPreference);
    9. VideoCapture(int index);
    10. //析构
    11. virtual ~VideoCapture();
    12. //打开视频文件或捕获设备或IP视频流以进行视频捕获
    13. virtual bool open(const String& filename);
    14. //打开摄像机进行视频捕获
    15. virtual bool open(int index);
    16. bool open(int cameraNum, int apiPreference);
    17. virtual bool open(const String& filename, int apiPreference);
    18. //若视频捕获已经被初始化(打开),返回true;
    19. virtual bool isOpened() const;
    20. //关闭视频文件或捕获设备。
    21. virtual void release();
    22. //从视频文件或捕获设备中捕获下一帧。
    23. virtual bool grab();
    24. /*解码并返回抓取的视频帧。
    25. image为捕获返回的视频帧,没有帧被捕获image为空;
    26. 成功不会返回true;
    27. */
    28. virtual bool retrieve(OutputArray image, int flag = 0);
    29. //抓取、解码并返回下一个视频帧。
    30. virtual bool read(OutputArray image);
    31. //设置VideoCapture中的属性。
    32. virtual bool set(int propId, double value);
    33. //返回指定的VideoCapture属性
    34. virtual double get(int propId) const;
    35. ...
    36. };

    测试代码:

    1. int main()
    2. {
    3. VideoCapture capture;
    4. capture.open("./video/xxx.AVI");
    5. if(!capture.isOpened()){
    6. std::cout << "failed to open video!" << std::endl;
    7. return -1;
    8. }
    9. int totalFrame = capture.get(CV_CAP_PROP_FRAME_COUNT);
    10. int imageW = capture.get(CV_CAP_PROP_FRAME_WIDTH);
    11. int imageH = capture.get(CV_CAP_PROP_FRAME_HEIGHT);
    12. double frameRate = capture.get(CV_CAP_PROP_FPS);
    13. std::cout << "----open ok------" << totalFrame << " w=" << imageW << " h=" << imageH << " frameRate="<< frameRate << std::endl;
    14. //----open ok------1276 w=640 h=480 frameRate=24
    15. cv::Mat frameImg ;
    16. long nCount = 1;
    17. while(true)
    18. {
    19. capture >> frameImg ;
    20. if(!frameImg.empty()){
    21. imwrite("./video/inc835.jpg", frameImg);
    22. break;
    23. }
    24. }
    25. capture.release();
    26. return 0;
    27. }

     2. VideoWriter类

     作用: 写视频文件或图像序列;

    1. class VideoWriter
    2. {
    3. public:
    4. //构造
    5. VideoWriter();
    6. /*filename:输出视频文件的文件名
    7. fourcc:用于压缩帧的编解码器的4个字符代码。
    8. fps:帧率
    9. frameSize:帧宽高大小;
    10. isColor:非0,编码器将期望并编码彩色帧;否则,灰度帧;
    11. */
    12. VideoWriter(const String& filename, int fourcc, double fps,
    13. Size frameSize, bool isColor = true);
    14. //apiPreference参数: cv::CAP_FFMPEG or cv::CAP_GSTREAMER等
    15. VideoWriter(const String& filename, int apiPreference, int fourcc, double fps,
    16. Size frameSize, bool isColor = true);
    17. //析构
    18. virtual ~VideoWriter();
    19. //打开文件
    20. virtual bool open(const String& filename, int fourcc, double fps,
    21. Size frameSize, bool isColor = true);
    22. bool open(const String& filename, int apiPreference, int fourcc, double fps,
    23. Size frameSize, bool isColor = true);
    24. //若视频输出已经被成功初始化(打开),返回true;
    25. virtual bool isOpened() const;
    26. //关闭视频输出
    27. virtual void release();
    28. //写下一帧
    29. virtual void write(const Mat& image);
    30. virtual bool set(int propId, double value);
    31. virtual double get(int propId) const;
    32. //将4个字符连接到一个fourcc代码
    33. static int fourcc(char c1, char c2, char c3, char c4);
    34. protected:
    35. Ptr writer;
    36. Ptr iwriter;
    37. static Ptr create(const String& filename, int fourcc, double fps,
    38. Size frameSize, bool isColor = true);
    39. };

    测试代码:

    1. int main()
    2. {
    3. VideoCapture inputVideo("./video/INC835.AVI");
    4. if(!inputVideo.isOpened()){
    5. std::cout << "----VideoCapture open failed----" << std::endl;
    6. return -1;
    7. }
    8. int imageW = inputVideo.get(CV_CAP_PROP_FRAME_WIDTH);
    9. int imageH = inputVideo.get(CV_CAP_PROP_FRAME_HEIGHT);
    10. double fps = inputVideo.get(CV_CAP_PROP_FPS);
    11. VideoWriter outputVideo;
    12. string fileName = "./video/INC835_Write.avi";
    13. outputVideo.open(fileName, VideoWriter::fourcc('F','M','P','4'),25.0,Size(imageW,imageH),true);
    14. if(!outputVideo.isOpened()){
    15. std::cout << "----VideoWriter open failed----" << std::endl;
    16. inputVideo.release();
    17. return -1;
    18. }
    19. cv::Mat frameImg;
    20. vector rgb;
    21. cv::Mat resultImg;
    22. while(1)
    23. {
    24. inputVideo >> frameImg;
    25. if(!frameImg.empty()){
    26. cv::split(frameImg, rgb); //分离出三通道rgb;
    27. for(int i =0; i < 3; i++){
    28. #if 1
    29. if(i != 0){
    30. //提取B通道分量
    31. rgb[i] = cv::Mat::zeros(Size(imageW,imageH), rgb[0].type());
    32. }
    33. #endif
    34. //通道合并
    35. cv::merge(rgb, resultImg);
    36. }
    37. outputVideo << resultImg;
    38. }else{
    39. break; //读取完
    40. }
    41. }
    42. inputVideo.release();
    43. outputVideo.release();
    44. return 0;
    45. }

     

    outputVideo.open(fileName, -1,25.0,Size(imageW,imageH),false);

    错误: 调用outputVideo.open出现OpenCV: FFMPEG: tag 0xffffffff/'����' is not found (format 'avi / AVI (Audio Video Interleaved)')'

    处理: 将参数2改为使用fourcc生成;具体字段需要根据编码确定;

    如: outputVideo.open(fileName, VideoWriter::fourcc('M','J','P','G'),25.0,Size(imageW,imageH),false);

    或fourcc('F','M','P','4');

    参考: python错误:OpenCV: FFMPEG: tag 0xffffffff/’����‘’is not found (format ’mp4 / MP4 (MPEG-4 Part 14)’)’-python黑洞网

  • 相关阅读:
    死锁和死锁的处理
    ardupilot BMI088加速度陀螺仪学习
    Python与GIS
    switchhosts怎么配置host?
    排序算法之归并排序(递归和非递归版)
    深度学习100例——卷积神经网络(CNN)实现服装图像分类
    osg 操作 NodePathList 节点操作
    NAT常用配置讲解!eNSP实验拓扑!
    day04_java基础
    DoIP——step1:车辆连接
  • 原文地址:https://blog.csdn.net/qq_39048131/article/details/126340322