【问题标题】:Video capture and save using OpenCV使用 OpenCV 捕获和保存视频
【发布时间】:2014-01-22 11:51:35
【问题描述】:

下面是我用于从网络摄像头获取视频并将其保存在硬盘上的代码。在运行程序时,它会显示“视频编写器未打开”。我哪里错了?

#include <opencv\cv.h>
#include <opencv2\highgui\highgui.hpp>
#include <opencv2\imgproc\imgproc.hpp>
#include <WinSock2.h>
#include <WS2tcpip.h>
#include <stdio.h>
#include <iostream>

#pragma comment(lib, "Ws2_32.lib")
#define default_buflen 1024

using namespace std;
using namespace cv;

#define default_port "1234"

int main(int argc, char** argv)
{
    Mat capture;
    VideoCapture cap(0);
    if(!cap.isOpened())
    {
        cout<<"Cannot connect to camera"<<endl;
        getchar();
        return -1;
    }
    double fps=30;
    Size s=Size((int)cap.get(CV_CAP_PROP_FRAME_WIDTH),(int)cap.get(CV_CAP_PROP_FRAME_WIDTH));
    VideoWriter vidcapt;
    vidcapt.open("c:\\out.avi",CV_FOURCC('D','I','V','X'),cap.get(CV_CAP_PROP_FPS),s,true);

    if(!vidcapt.isOpened())
    {
        cout<<"Video writer not opening"<<endl;
        getchar();
        return -1;
    }

    while(true)
    {
        cap>>capture;
        namedWindow("Display",1);
        imshow("Display",capture);

        vidcapt<<capture;

        int ch=waitKey(5);
        if(char(ch)==27)
        {
            break;
        }
    }
}

我已经阅读了herehere 给出的答案,但我不明白我哪里出错了。

【问题讨论】:

    标签: c++ opencv video video-capture


    【解决方案1】:

    尝试其他编解码器

    CV_FOURCC('P','I','M','1') = MPEG-1 编解码器

    CV_FOURCC('M','J','P','G') = motion-jpeg 编解码器(效果不佳)

    CV_FOURCC('M', 'P', '4', '2') = MPEG-4.2 编解码器

    CV_FOURCC('D', 'I', 'V', '3') = MPEG-4.3 编解码器

    CV_FOURCC('D', 'I', 'V', 'X') = MPEG-4 编解码器

    CV_FOURCC('U', '2', '6', '3') = H263 编解码器

    CV_FOURCC('I', '2', '6', '3') = H263I 编解码器

    CV_FOURCC('F', 'L', 'V', '1') = FLV1 编解码器

    here 复制粘贴。我设法用 CV_FOURCC('F', 'L', 'V', '1') 编写视频。

    顺便说一句,编解码器当然应该安装在你的机器上。

    【讨论】:

    • 都试过了。还是行不通。接下来做什么?另外,如何知道安装了哪些编解码器?
    • @PrakharMohanSrivastava 检查您的编解码器,重新安装它们,如果您使用调试选项编译它,则调试 opencv
    【解决方案2】:

    根据您的代码,我无法理解为什么您每次都在 while 循环中创建窗口 Display,而且您每次都初始化 VideoWriter 对象可能是错误的。我对您的代码稍作修改,请尝试,它可能对您有所帮助

        #include <opencv\cv.h>
        #include <opencv2\highgui\highgui.hpp>
        #include <opencv2\imgproc\imgproc.hpp>
        #include <WinSock2.h>
        #include <WS2tcpip.h>
        #include <stdio.h>
        #include <iostream>
    
        #pragma comment(lib, "Ws2_32.lib")
        #define default_buflen 1024
    
        using namespace std;
        using namespace cv;
    
        #define default_port "1234"
    
        int main(int argc, char** argv)
        {
            Mat capture;
            VideoCapture cap(0);
            if(!cap.isOpened())
            {
                cout<<"Cannot connect to camera"<<endl;
                getchar();
                return -1;
            }
    
      namedWindow("Display",CV_WINDOW_AUTOSIZE); 
    
      double dWidth = cap.get(CV_CAP_PROP_FRAME_WIDTH); 
      double dHeight = cap.get(CV_CAP_PROP_FRAME_HEIGHT); 
    
      Size frameSize(static_cast<int>(dWidth), static_cast<int>(dHeight));
    
      VideoWriter oVideoWriter ("c:\\out.avi", CV_FOURCC('P','I','M','1'), 20, frameSize,true);
    
       if ( !oVideoWriter.isOpened() ) 
       {
          cout << "ERROR: Failed to write the video" << endl;
          return -1;
        }
    
        while(true)
        {
              Mat frame;
    
              bool bSuccess = cap.read(frame); // read a new frame from video
    
              if (!bSuccess) //if not success, break loop
              {
                 cout << "ERROR: Cannot read a frame from video file" << endl;
                 break;
              }
    
              oVideoWriter.write(frame); //writer the frame into the file
    
             imshow("Display", frame); 
    
             if (waitKey(10) == 27) 
             {
                cout << "esc key is pressed by user" << endl;
                break; 
             }
          }
      }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-01-09
      • 1970-01-01
      • 1970-01-01
      • 2010-11-13
      • 2020-03-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多