2010-07-25 77 views
1

我有以下的源代碼來檢測BLOB和我使用的MS 2008,OpenVC 2.1無法創建/保存輸出的AVI文件在OpenCV中

#include "stdafx.h" 

#include <cv.h> 
#include <highgui.h> 
#include <stdio.h> 
#include <stdlib.h> 
using namespace std; 

/*You may change the values of the sthreshold and hlower and hupper to get different results....*/ 
const int sthreshold=210; 
const double hlower=178; 
const double hupper=3; 
int main(int argc, char* argv[]) { 

    int i,j,k;//for iterations 
    int height,width,step,channels;/*HSV means the frame after color conversion*/ 
    int heightmono,widthmono,stepmono,channelsmono;/*mono means the frame which has the monochrome image*/ 
    const char string1[]="monoimg.avi";/*This is the name of the video which would be the outcome of the blob detection program..*/ 
    uchar *data,*datamono; 


    i=j=k=0; 

    IplImage *frame = 0; 

    int key = 0;/*Initializing the capture from the video...*/ 

    CvCapture* capture = cvCreateFileCapture("partofvideo3.avi"); 

    double fps = cvGetCaptureProperty (/*getting the capture properties......the frame rate..*/ 
    capture,CV_CAP_PROP_FPS); 

    CvSize size = cvSize(
    (int)cvGetCaptureProperty(capture, CV_CAP_PROP_FRAME_WIDTH), 
    (int)cvGetCaptureProperty(capture, CV_CAP_PROP_FRAME_HEIGHT) 
    ); 

    CvVideoWriter *writer=cvCreateVideoWriter(string1, CV_FOURCC('D', 'I', 'V', 'X') ,fps,size) ; 


    if(writer !=NULL) 
     printf("Loaded\n"); 
    else 
     printf("Not Loaded\n"); 
    /* always check */ 

    if (!capture) { 
    fprintf (stderr, "Cannot open video file!\n"); 
    return(1); 
    } 


    height = frame->height; 
    width = frame->width; 
    step = frame->widthStep; 
    channels = frame->nChannels; 
    data = (uchar *)frame->imageData; 

    cvNamedWindow("monoimage", CV_WINDOW_AUTOSIZE); 
    cvNamedWindow("original frame", CV_WINDOW_AUTOSIZE); 
     for (;;) {/*keep looping till we are out of frames...*/ 
    if (!cvGrabFrame(capture)) { 
    break; 
    } 

    frame = cvRetrieveFrame(capture); 
    IplImage *colimgbot = cvCreateImage(cvGetSize(frame), 8, 3); 
    IplImage *monoimgbot = cvCreateImage(cvGetSize(frame), 8, 1); 
    cvCvtColor(frame,frame,CV_RGB2HSV); 

    for(i=0;i< (height);i++) 
     { 
      for(j=0;j<(width);j++) 
      { 
      if((data[(height-i)*step+j*channels]<=hlower) && (data[(height-i)*step+j*channels]>=hupper)) 
       { 
        if((data[(height-i)*step+j*(channels)+1])>sthreshold) 
          /*"height-i" because if we use only "i" we were getting vertically inverted result...hence reinverting the same 
          would do the required....*/ 
          datamono[i*stepmono+j*channelsmono]=255; 
         else  
          datamono[i*stepmono+j*channelsmono]=0;} 
         else datamono[i*stepmono+j*channelsmono]=0; 
       } 
      } 
     cvErode(monoimgbot,monoimgbot,0,14); 
     cvDilate(monoimgbot,monoimgbot,0,15); 
     cvWriteFrame(writer, monoimgbot); 
     cvShowImage("original frame", frame); 
     cvShowImage("monoimage", monoimgbot); 

     if((cvWaitKey(10) & 255) == 27) break; 

     } 

    cvReleaseVideoWriter(&writer) ; 

    cvDestroyWindow("monoimage"); 

    cvReleaseCapture(&capture); 


    return 0; 
} 

當我運行程序我得到以下運行時錯誤 當下面的行遇到

CvVideoWriter* writer=cvCreateVideoWriter(string1, CV_FOURCC(‘D’,'I’,'V’,'X’),fps,size) ; 

輸出#0,AVI,爲 'monoimg.avi': 流#0.0:視頻mgeg4,YUV420P,q = 2-31,90K TBN [MPEG4 @ 0x37e5c0]幀率不設置爲 OpenCV錯誤:錯誤參數(無法打開的編解碼器「MPEG 4':未指定的錯誤)的未知函數,文件 C:\用戶\ VP \ OCV \的OpenCV \ SRC \ highgui \ cvcap_ffmpeg.cpp,線1306

回答

3

首先getCaptureProperties在實際取得任何東西時都會覺得很糟糕,所以你應該檢查一下fps實際上有你認爲它的作用。一些編解碼器不能在某些幀速率下編碼,所以試着明確地將fps設置爲30,看看它是否有效。

否則你會錯過mpeg 4編解碼器。我推薦:

1.)下載一些編解碼器,然後重試。 http://www.divx.com/en/software/divx-plus/codec-pack可能有你在找什麼。

2)你可以改變

CvVideoWriter *writer=cvCreateVideoWriter(string1, CV_FOURCC('D', 'I', 'V', 'X') ,fps,size) ; 

線使用一些其他的編解碼器。我使用了幾個編解碼器,並在我的系統上放置了編碼7分鐘視頻的時間。

(\P,\I,\M,\1) ;= MPEG-1 codec  (112913.386195 msecs) (104 MB) 
    (\M,\J,\P,\G) ;= motion-jpeg codec (crashed)           
    (\M,\P,\4,\2) ;= MPEG-4.2 codec (107184.186774 msecs) (82 MB) 
    (\D,\I,\V,\3) ;= MPEG-4.3 codec (118308.933328 msecs) (83 MB) 
    (\D,\I,\V,\X) ;= MPEG-4 codec  (99037.738131 msecs) (85 MB) 
    (\U,\2,\6,\3) ;= H263 codec  (101141.993551 msecs) (89 MB) 
    (\I,\2,\6,\3) ;= H263I codec  (crashed) 
    (\F,\L,\V,\1) ;= FLV1 codec  (104307.567802 msecs) (93 MB) 

特別是我會建議嘗試FLV1編解碼器,因爲我已經有了很多運氣。所以總結嘗試:

CvVideoWriter *writer=cvCreateVideoWriter(string1, CV_FOURCC('F', 'L', 'V', '1') ,fps,size) ; 

祝你好運!

+0

我已經安裝了DIVX解碼包,我曾試圖在同一時間我試圖FLV1也由30手動設置fps的,但這次我收到以下錯誤 輸出#0,AVI,以「monoimg.avi」: 流#0.0:視頻:flv,yuv420p,q = 2-31,90k tbn,50 tbc 編譯器未對齊堆棧變量。 Libavcodec已被編譯錯誤 並可能非常緩慢或崩潰。這不是libavcodec, 中的錯誤,而是編譯器中的錯誤。您可以嘗試使用gcc> = 4.2進行重新編譯。 不要向FFmpeg開發人員報告崩潰。 圖片大小無效(爲0x0) 最後一條消息重複2次 AVI @ 0x37bfd0]尺​​寸沒有設置 – Hunt 2010-07-25 17:31:55

+0

設置fps至30手動爲我工作在OSX opencv2.2和編解碼器支持MPEG-4 – PhoebeB 2010-12-29 11:29:13

+0

請你幫我回答這個問題問題http://stackoverflow.com/questions/9063746/opencv-cv-fourccf-lv-1-not-working – Wazzzy 2012-01-31 13:58:39