Approval failed with accumulation. In opencv

I use openCV and try to calculate the moving average of the background, then taking the current frame and subtracting the background to determine the movement (something like).

However, when I run the program, I get:

OpenCV Error: Assertion failed (func != 0) in accumulateWeighted, file /home/sebbe/projekt/opencv/trunk/opencv/modules/imgproc/src/accum.cpp, line 431 terminate called after throwing an instance of 'cv::Exception' what(): /home/sebbe/projekt/opencv/trunk/opencv/modules/imgproc/src/accum.cpp:431: error: (-215) func != 0 in function accumulateWeighted 

I cannot figure out which arguments are wrong for accumulating.

Code entered below:

 #include <stdio.h> #include <stdlib.h> #include "cv.h" #include "highgui.h" #include "opencv2/imgproc/imgproc.hpp" #include "opencv2/highgui/highgui.hpp" #include "cxcore.h" using namespace cv; int main( int argc, char **argv ) { Mat colourFrame; Mat frame; Mat greyFrame; Mat movingAverage; Mat difference; Mat temp; int key = 0; VideoCapture cap(0); /* always check */ if ( !cap.isOpened() ) { fprintf( stderr, "Cannot open initialize webcam!\n" ); return 1; } namedWindow("Camera Window", 0); // Initialize cap >> movingAverage; while( key != 'q' ) { /* get a frame */ cap >> colourFrame; /* Create a running average of the motion and convert the scale */ accumulateWeighted(colourFrame, movingAverage, 0.02, Mat() ); /* Take the difference from the current frame to the moving average */ absdiff(colourFrame, movingAverage, difference); /* Convert the image to grayscale */ cvtColor(difference, greyFrame, CV_BGR2GRAY); /* Convert the image to black and white */ threshold(greyFrame, greyFrame, 70, 255, CV_THRESH_BINARY); /* display current frame */ imshow("Camera Window",greyFrame); /* exit if user press 'q' */ key = cvWaitKey( 1 ); } return 0; } 
+4
source share
2 answers

If you look at the sources of OpenCV, in particular the line modules/imgproc/src/accum.cpp line 431, the lines preceding this statement are as follows:

 void cv::accumulateWeighted( InputArray _src, CV_IN_OUT InputOutputArray _dst, double alpha, InputArray _mask ) { Mat src = _src.getMat(), dst = _dst.getMat(), mask = _mask.getMat(); int sdepth = src.depth(), ddepth = dst.depth(), cn = src.channels(); CV_Assert( dst.size == src.size && dst.channels() == cn ); CV_Assert( mask.empty() || (mask.size == src.size && mask.type() == CV_8U) ); intfidx = getAccTabIdx(sdepth, ddepth); AccWFunc func = fidx >= 0 ? accWTab[fidx] : 0; CV_Assert( func != 0 ); // line 431 

What happens in your case is that getAccTabIdx() returns -1 , which in turn makes func equal to ZERO.

For accumulateWeighted() to work correctly, the depth of colourFrame and movingAverage must be one of the following features:

 colourFrame.depth() == CV_8U && movingAverage.depth() == CV_32F colourFrame.depth() == CV_8U && movingAverage.depth() == CV_64F colourFrame.depth() == CV_16U && movingAverage.depth() == CV_32F colourFrame.depth() == CV_16U && movingAverage.depth() == CV_64F colourFrame.depth() == CV_32F && movingAverage.depth() == CV_32F colourFrame.depth() == CV_32F && movingAverage.depth() == CV_64F colourFrame.depth() == CV_64F && movingAverage.depth() == CV_64F 

Anything other than this will getAccTabIdx() return -1 and throw an exception on line 431.

+9
source

From the OpenCV API documentation, you can see that the output image from accumulates in

  • dst - image of the battery with the same number of channels as the input image, 32-bit or 64-bit floating point.

So your initialization is wrong. You must first get the size of the colourFrame, and then do the following:

 cv::Mat movingAverage = cv::Mat::zeros(colourFrame.size(), CV_32FC3); 
+8
source

All Articles