2014-01-22 51 views
0

下面给出的是我用来从我的网络摄像头获取视频并将其保存在我的硬盘上的代码。在运行该程序时,它说“视频编写器未打开”。我哪里错了?视频采集和保存使用OpenCV

#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的答案,但我不明白我要去的地方错了。

回答

1

根据你的代码,我不明白你为什么在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; 
     } 
     } 
    } 
2

尝试其它编解码器

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

CV_FOURCC( 'M', 'J', 'P', 'G')=运动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')写视频。

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

+0

尝试了所有这些。还是行不通。接下来做什么?另外,如何找出哪些编解码器已安装? –

+0

@PrakharMohanSrivastava检查您的编解码器,重新安装它们,如果使用调试选项编译它,请调试opencv – Dabo