断言在OpenCV中累积加权时失败

时间:2011-08-14 21:22:16

标签: c++ opencv image-processing

我正在使用openCV并尝试计算背景的移动平均值,然后获取当前帧并减去背景以确定移动(某种类型)。

然而,当我运行程序时,我得到:

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

我无法看到积累加权的错误论点。

下面插入的代码:

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

3 个答案:

答案 0 :(得分:10)

查看OpenCV源,特别是modules/imgproc/src/accum.cpp第431行,这个断言之前的行是:

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

在你的情况下发生的事情是getAccTabIdx()正在返回-1,这反过来使func成为零。

要使accumulateWeighted()正常工作,colourFramemovingAverage的深度必须是以下选项之一:

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

任何不同的内容都会使getAccTabIdx()返回-1并在第431行触发异常。

答案 1 :(得分:8)

OpenCV API上的文档中您可以看到 accumulateWeighted 的输出图像是

  • dst - 与输入图像,32位或64位浮点数相同通道数的累加器图像。

所以你的初始化是错误的。您应首先检索colourFrame大小,然后执行以下操作:

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

答案 2 :(得分:0)

在 Python 上,一个可行的解决方案是使用 movingAverage 启动 FIRSTcolourFrame.copy().astype("float")。 我在此 website

上找到了解决方案