2012-05-05 29 views
3
#include "opencv/cvaux.h" 
#include "opencv2/opencv.hpp" 
#include "opencv2/opencv_modules.hpp" 
#include "opencv2/imgproc/imgproc.hpp" 
#include "opencv2/highgui/highgui.hpp" 
#include "opencv2/video/background_segm.hpp" 
#include "opencv2/video/tracking.hpp" 
#include "opencv2/video/video.hpp" 
#include "opencv2/features2d/features2d.hpp" 
#include "ctype.h" 
#include "stdio.h" 

int main(int argc, char* argv[]) 
{ 

    /* Start capturing */ 
    CvCapture* capture = 0; 

    /*//Capture from CAM 
    if(argc == 1 || (argc == 2 && strlen(argv[1]) == 1 && isdigit(argv[1][0]))) 
    capture = cvCaptureFromCAM(argc == 2 ? argv[1][0] - '0' : 0); 
    else if(argc == 2)*/ 

    capture = cvCaptureFromAVI("pool.avi" ); 

    if(!capture) 
    { 
     fprintf(stderr,"Could not initialize...\n"); 
     return -1; 
    } 

    /* Capture 1 video frame for initialization */ 
    IplImage* videoFrame = NULL; 

    videoFrame = cvQueryFrame(capture); 

    if(!videoFrame) 
    { 
     printf("Bad frame \n"); 
     exit(0); 
    } 

    // Create windows 
    cvNamedWindow("BG", 1); 
    cvNamedWindow("FG", 1); 
    //cvNamedWindow("Blobs", 1); 
    //cvNamedWindow("Contours",1); 

    // Select parameters for Gaussian model. 
    CvFGDStatModelParams* params = new CvFGDStatModelParams; 
    params->Lcc=64; /* Quantized levels per 'color co-occurrence' component. Power of two, typically 16, 32 or 64.   */ 
    params->N1cc=25; /* Number of color co-occurrence vectors used to model normal background color variation at a given pixel. */ 
    params->N2cc=40; /* Number of color co-occurrence vectors retained at given pixel. Must be > N1cc, typically ~ 5/3 of N1cc. */ 
    /* Used to allow the first N1cc vectors to adapt over time to changing background.    */ 
    params->is_obj_without_holes=TRUE; /* If TRUE we ignore holes within foreground blobs. Defaults to TRUE.      */ 
    params->perform_morphing=1;/* Number of erode-dilate-erode foreground-blob cleanup iterations.      */ 
    /* These erase one-pixel junk blobs and merge almost-touching blobs. Default value is 1.   */ 
    params->alpha1=0.1; /* How quickly we forget old background pixel values seen. Typically set to 0.1    */ 
    params->alpha2=0.005; /* "Controls speed of feature learning". Depends on T. Typical value circa 0.005.     */ 
    params->alpha3=0.1; /* Alternate to alpha2, used (e.g.) for quicker initial convergence. Typical value 0.1.    */ 
    params->delta=2; /* Affects color and color co-occurrence quantization, typically set to 2.     */ 
    params->T=0.9; /* "A percentage value which determines when new features can be recognized as new background." (Typically 0.9).*/ 
    params->minArea=15; /* Discard foreground blobs whose bounding box is smaller than this threshold.     */ 

    CvBGStatModel* bgModel = cvCreateFGDStatModel(videoFrame ,params); 

    //Write FG in a file 
    CvVideoWriter *writer = 0; 
    int isColor = 1; 
    int fps  = 25; 
    int frameW = 640; 
    int frameH = 480; 
    writer=cvCreateVideoWriter("out.avi",CV_FOURCC('D', 'I', 'V', 'X'), 
          fps,cvSize(frameW,frameH),isColor); 


    int key=-1; 
    while(key != 'q') 
    { 
     // Grab a fram 
     videoFrame = cvQueryFrame(capture); 

     if(!videoFrame) 
      { 
      break;} 

     // Update model 
     cvUpdateBGStatModel(videoFrame,bgModel); 

     // Display results 
     cvShowImage("BG", bgModel->background); 
     cvShowImage("FG", bgModel->foreground); 

    // Write foreground in AVI formet 
    cvWriteFrame(writer,bgModel->foreground); 

     key = cvWaitKey(10); 
    } 

    cvDestroyWindow("BG"); 
    cvDestroyWindow("FG"); 
    //cvDestroyWindow("Blobs"); 
    //cvDestroyWindow("Contours"); 

    cvWaitKey(0); 

    cvReleaseBGStatModel(&bgModel); 
    cvReleaseCapture(&capture); 
    cvReleaseVideoWriter(&writer); 

    return 0; 
} 

私はopencv2.3.1とVisual Studio 2010OpenCVの:背景差分:アクセス違反

を使用しています私は、背景差分/フォアグラウンド抽出のためのFGDアルゴリズムを実装しようとしています。

私はすでにMOGアルゴリズムをうまく実装しています。次に、関数とパラメータをMOGからFGDに変更するだけです。

プロジェクトはビジュアルスタジオで正常にコンパイルされましたが、機能は次のとおりです。 cvShowImage( "BG"、bgModel-> background); 0xc0000005で:アクセス違反の書き込み場所0xfffffffcc40b40e0 hello_opencv_231.exeで0x000007feef085d09で

未処理の例外: それは、次のエラーが発生します。

私はこれが何であるか分かりません... アイデア?

ありがとうございました!

答えて

3

存在しない位置にアクセスしようとすると、エラーアクセス違反が表示されます。これは、たとえば、Mat(3,1、CV_32FC1)の位置4にアクセスしたときに発生します。または、Mat(3,1、CV_32FC1)x Mat(3,1、CV_32FC1)の2つの行列に、互換性のないサイズを乗算した場合。

コードをステップバイステップで(Visual StudioのF10)デバッグし、クラッシュすると正確な行がわかるため、アクセス違反の原因となっているものを正確に分析できます。