2016-04-29 342 views
0

我面臨的問題是,當我嘗試使用Videowriter保存圖像時,它創建視頻文件,但大小爲0.我已創建一個頭聲明一個函數,視頻和一個單獨的.cpp文件,它定義了函數。當我只用一個文件寫入整個代碼而不像以前那樣在單獨的文件中包括ViceoCapture和VideoWriter時,它運行良好,甚至可以保存適當文件大小的視頻文件。視頻大小是0字節使用videowriter opencv

當我開始調試,我發現每當我收到一個幀,但VideoWriter.open爲空,或者說加載opencv_highgui.dll無符號

**savevideo.cpp file** 
#include"SaveVideo.h" 


int CSaveVideo::savevideo()//string InpVideo, int pinstatus) 
{ 
    VideoCapture oVcam(0); 


    Mat vFrame; 
    string OutPath = "C:\\Users\\20080031\\Desktop\\"; 
    string Filename = "Vout.avi"; 
    int vfourcc = CV_FOURCC('M', 'J', 'P', 'G'); 
    int vfps = 20; 
    VideoWriter oVWrite; 
    oVWrite.open(Filename, vfourcc, vfps, Size(480, 640), true); 

    if (!oVcam.isOpened()) 
    { 
     cout << "Camera not opened" << endl; 
    } 
    while (1) 
    { 
     oVcam.read(vFrame); 
     imshow("Input", vFrame); 
     waitKey(1); 
     oVWrite.write(vFrame); 

    } 



} 
CSaveVideo::CSaveVideo() 
{ 
    cout << "Inside Constructor" << endl; 
} 
CSaveVideo::~CSaveVideo() 
{ 
    //VideoCapture Vcam0; 
    cout << "Inside Distructor" << endl; 
    //Vcam0.release(); 
} 
**saveVideo.h** 
#ifndef SAVEVIDEO_H 
#define SAVEVIDEO_H 

#include<iostream> 
#include<stdio.h> 
#include<string> 
#include<opencv2/highgui/highgui.hpp> 
#include<opencv2/imgproc/imgproc.hpp> 
#include<opencv2/core/core.hpp> 
#include <opencv2\opencv.hpp> 



using namespace std; 
using namespace cv; 


class CSaveVideo 
{ 
public: 
    CSaveVideo(); 
    ~CSaveVideo(); 

    //string m_sGPIOpinstatus; 
    //char m_cSTOP; 

    int savevideo();//string PinStatus, char End, ); 
}; 
#endif SAVEVIDEO_H 
**main.cpp** 
#include"SaveVideo.h" 


int main() 
{ 
    CSaveVideo save; 
    save.savevideo(); 
    /*cout << out<<endl;*/ 
    return 0; 
} 
+0

在Windows系統上:您的應用程序是否有權訪問opencv_ffmpeg dll文件?請將其複製到應用程序文件夾或相應地設置PATH變量。如果找不到這個DLL,OpenCV不會拋出錯誤,但如果沒有它,它將無法編碼視頻。 – Micka

+0

什麼是您的圖像大小?我想這是640x480(寬x高)?然後你必須更改爲'Size(640,480)' – Micka

+0

,你應該在你的循環中有一些術語標準。你應該關閉視頻文件以確保系統正確寫入並且玩家可以打開它。在循環後嘗試'if(waitKey(1)== 27)break;'後面跟一個'oVWrite.release();'。然後在處理過程中按Esc停止並關閉。 – Micka

回答

1

更好地使用這個格式 -

VideoWriter oVidWrite; 
int nframe_width = oVidCap.get(CV_CAP_PROP_FRAME_WIDTH); 
int nframe_height = oVidCap.get(CV_CAP_PROP_FRAME_HEIGHT); 
string str_Path = "video.avi"; 
int fps = 30; 

oVidWrite.open(str_Path, CV_FOURCC('M', 'J', 'P', 'G'), fps, Size(nframe_width, nframe_height), true);