关于 c :断言在 OpenCV 中累积加权失败

Assertion failed with accumulateWeighted in OpenCV

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

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

1
2
3
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

我看不出accumulateWeighted 的哪些参数是错误的。

下面插入的代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#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!\
"
);
        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 行,此断言之前的行是:

1
2
3
4
5
6
7
8
9
10
11
12
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 的深度必须是以下选项之一:

1
2
3
4
5
6
7
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 行触发异常。


从 OpenCV API 的文档中,您可以看到来自累积加权的输出图像是

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

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

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

在 Python 上,一个可行的解决方案是使用 FIRSTcolourFrame.copy().astype("float") 启动 movingAverage
我在这个网站上找到了解决方案