2014-01-22 77 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