视频捕获并使用OpenCV保存

Video capture and save using OpenCV

本文关键字:OpenCV 保存 视频      更新时间:2023-10-16

下面给出的是我使用的代码,用于从网络摄像头获取视频并将其保存在硬盘上。在运行程序时,它说"视频作者没有打开"。我要去哪里?

#include <opencvcv.h>
#include <opencv2highguihighgui.hpp>
#include <opencv2imgprocimgproc.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;
        }
    }
}

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

尝试其他编解码器

cv_fourcc('p','i','m','1')= mpeg-1 codec

cv_fourcc('m','j','p','g')=运动 - jpeg编解码器(效果不佳)

cv_fourcc('m','p','4','2')= mpeg-4.2 codec

cv_fourcc('d','i','v','3')= mpeg-4.3 codec

cv_fourcc('d','i','v','x')= mpeg-4 codec

cv_fourcc('u','2','6','3')= H263编解码器

cv_fourcc('i','2','6','3')= h263i codec

cv_fourcc('f','l','v','1')= flv1 codec

复制从这里粘贴。我设法用cv_fourcc('f','l','v','1'编写视频。

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

根据您的代码,我无法理解为什么在循环时每次创建窗口Display,也可以每次都会初始化VideoWriter对象。我对您的代码进行了稍微修改,请尝试,它可能会帮助您

    #include <opencvcv.h>
    #include <opencv2highguihighgui.hpp>
    #include <opencv2imgprocimgproc.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; 
         }
      }
  }