簡體   English   中英

斷言在 OpenCV 中累積加權失敗

[英]Assertion failed with accumulateWeighted in OpenCV

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

我看不出 arguments 的累積加權有什么問題。

下面插入的代碼:

#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 行觸發異常。

OpenCV API上的文檔中,您可以看到來自累積加權的 output 圖像是

  • dst – 與輸入圖像具有相同通道數的累加器圖像,32 位或 64 位浮點。

所以你的初始化是錯誤的。 您應該先檢索 colourFrame 大小,然后執行以下操作:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM