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 2010

我正在尝试实现用于背景减法/前景提取的 FGD 算法。

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

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

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

我不知道这是什么......有什么想法吗?

谢谢你的帮助!

4

2 回答 2

3

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

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

于 2012-06-01T07:57:54.803 回答
0

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

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

于 2018-03-26T15:23:20.290 回答