gpt4 book ai didi

c++ - OpenCV 中的 accumulateWeighted 断言失败

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:53:40 24 4
gpt4 key购买 nike

我正在使用 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

我可能看不出哪些参数对 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 行,此断言之前的行是:

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

关于c++ - OpenCV 中的 accumulateWeighted 断言失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7059817/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com