Opencv:背景减法:访问冲突

时间:2012-05-05 12:41:26

标签: opencv background-subtraction

#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 2010

我正在尝试为背景扣除/前景提取实现FGD算法。

我已经成功实现了MOG算法。然后我只需将功能和参数从MOG更改为FGD。

该项目在visual studio上成功编译,但功能如下:     cvShowImage(“BG”,bgModel-> background); 它给出了以下错误:

hello_opencv_231.exe中0x000007feef085d09处的未处理异常:0xC0000005:访问冲突写入位置0xfffffffcc40b40e0。

我不知道这是什么...... 任何想法?

感谢您的帮助!

2 个答案:

答案 0 :(得分:3)

当您尝试访问不存在的位置时,会出现错误访问冲突。例如,当您访问Mat(3,1,CV_32FC1)的位置4时会发生这种情况。或者当您将两个不相容的矩阵Mat(3,1,CV_32FC1)x Mat(3,1,CV_32FC1)相乘时。

逐步调试代码(在Visual Studio中为F10),当它崩溃时,您将知道确切的行,因此您可以分析导致访问冲突的确切原因。

答案 1 :(得分:0)

我不是百分百肯定,但我认为您的PC中可能没有CUDA兼容设备,或者您没有设置具有CUDA支持的OpenCV。

FGD算法似乎只有CUDA实现而没有CPU实现。