断言在OpenCV中累积加权时失败 [英] Assertion failed with accumulateWeighted in OpenCV

查看:541
本文介绍了断言在OpenCV中累积加权时失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am using openCV and trying to calculate a moving average of the background, then taking the current frame and subtracting the background to determine movement (of some sort).

但是,在运行程序时,我得到:

However, when running 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 cant possibly see what arguments are wrong to 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;
}


推荐答案

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

Looking at the OpenCV sources, specifically at modules/imgproc/src/accum.cpp line 431, the lines that precede this assertion are:

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 为零。

What's happening in your case is that getAccTabIdx() is returning -1, which in turn makes func be ZERO.

对于 accumulateWeighted()才能正常工作,深度为 colourFrame movingAverage 必须是以下选项之一:

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

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行触发异常。

Anything different than that will make getAccTabIdx() return -1 and trigger the exception at line 431.

这篇关于断言在OpenCV中累积加权时失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆