Amir Mahmoudi
Amir Mahmoudi

Reputation: 25

accumulateWeighted function assertion fails in OpenCV

Using OpenCv with C++, I am trying to perform running average on frames of a video to extract the foreground. But I cannot find out what's wrong with the accumulateWeighted function. The program stops running when it comes to that function, giving this error:

Unhandled exception at 0x753b9617 in test.exe: Microsoft C++ exception: cv::Exception at memory location 0x0017f0d4..

According to OpenCV documentation, I see that SRC as 1- or 3-channel, should be 8-bit or 32-bit floating point. And DST with the same number of channels as SRC image, should be 32-bit or 64-bit floating-point:

void accumulateWeighted(InputArray src, InputOutputArray dst, double alpha, InputArray mask=noArray())

So I used CV_32F for both of them. Am I doing it in a wrong way? Here is my code:

#include <iostream> // for standard I/O
#include <string>   // for strings
#include "stdafx.h"
#include <opencv.hpp>

#ifdef _DEBUG
#pragma comment (lib, "opencv_highgui2410d.lib")
#pragma comment (lib, "opencv_imgproc2410d.lib")
#pragma comment (lib, "opencv_core2410d.lib")
#else
#pragma comment (lib, "opencv_highgui2410.lib")
#pragma comment (lib, "opencv_imgproc2410.lib")
#pragma comment (lib, "opencv_core2410.lib")
#endif

using namespace std;
using namespace cv;

int main(int argc, char* argv[])
{
//// Step 1 : Get ready to Capture Video

    VideoCapture cap("768x576.avi"); // open the video 

    if (!cap.isOpened())  // if not success, exit program
    {
        cout << "Cannot open the video cam" << endl;
        return -1;
    }

//// Step 2 : Find video frame size 

    double dWidth = cap.get(CV_CAP_PROP_FRAME_WIDTH); //get the width of frames of the video
    double dHeight = cap.get(CV_CAP_PROP_FRAME_HEIGHT); //get the height of          frames of the video

//// Step 3 : Running Average

    Mat sum=Mat::zeros(dHeight,dWidth,CV_32FC3);
    for (int iii=0;iii<100;iii++)  // for 100 frames
    {
        Mat frame_rgb,floatimg;

        bool bSuccess = cap.read(frame_rgb); // read a new frame from video

        if (!bSuccess) //if not success, break loop
        {
             cout << "Cannot read a frame from video stream" << endl;
             break;
        }

        frame_rgb.convertTo(floatimg, CV_32FC3);

        accumulateWeighted(floatimg,sum,0.03,NULL);
    }

    cap.release();

    return 0;    
}

Upvotes: 0

Views: 958

Answers (1)

mirosval
mirosval

Reputation: 6822

Maybe try removing the NULL at the end? The noArray() default in the signature about is not the same as NULL

Upvotes: 1

Related Questions