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;
}

I am using opencv2.3.1 and visual studio 2010

I am trying to implement the FGD algorithm for background subtraction/foreground extraction.

I already implement the MOG algorithm successfully. Then I just change the function and parameter from MOG to FGD.

The project was successfully compiled on visual studio but in function: cvShowImage("BG", bgModel->background); it gives the following error:

Unhandled exception at 0x000007feef085d09 in hello_opencv_231.exe: 0xC0000005: Access violation writing location 0xfffffffcc40b40e0.

i don’t know what this is... any ideas?

Thanks for your help!

Jav_Rock
  • 22,059
  • 20
  • 123
  • 164

2 Answers2

3

The error access violation appears when you try to access a position that doesn't exist. This happens, for example, when you access the position 4 of Mat(3,1,CV_32FC1). Or when you multiply two matrices of incompatible sizes Mat(3,1,CV_32FC1)x Mat(3,1,CV_32FC1).

Debug your code step by step (F10 in Visual Studio), and when it crashes you will know the exact line, so you can analize what exactly is causing the access violation.

Jav_Rock
  • 22,059
  • 20
  • 123
  • 164
0

I am not 100% sure, however I think that it is possible that you didn't have CUDA compatible device in your PC, or you haven't set up OpenCV with CUDA support.

FGD algorithm seems to have only CUDA implementation and no CPU implementation.

kwmen
  • 63
  • 2
  • 5