调试一种方法,用于从视频中查找帧的运行平均值,以便在C ++中使用OpenCV进行运动检测

问题描述 投票:0回答:1

因此,为了练习C ++,我正在编写一个运动检测程序,使用OpenCV库将照片上传到Dropbox,这是我去年夏天在pyton之后按照本教程做的一个项目:https://www.pyimagesearch.com/2015/05/25/basic-motion-detection-and-tracking-with-python-and-opencv/

这部分代码运行一次,然后在第二个循环上抛出错误。

这是代码:

Mat set_delta(Mat &average, Mat gray_frame)
{
    Mat delta_frame;
    if (average.empty()==1)
    {
        cout<<"gray frame depth: "<<gray_frame.depth();

        gray_frame.convertTo(average, CV_32FC(gray_frame.channels()));
        //Mat Acc(gray_frame.rows,    gray_frame.cols,CV_32FC(gray_frame.channels()));
    }
    //cout<<"gray_frame average: "<< get_average(gray_frame)<<
    //      "\naverage_frame average: "<<get_average(average);
    Mat Acc(average.rows, average.cols,CV_32FC(average.channels()));

    cout<<"average depth: "<<average.depth()<<"\nAcc depth: "<<Acc.depth();

    accumulateWeighted(gray_frame, average, .5);
    convertScaleAbs(average, average);
    absdiff(gray_frame,average,delta_frame);
    return delta_frame;
}

我收到这个错误:

OpenCV Error: Assertion failed (func != 0) in accumulateWeighted, file /build/opencv/src/opencv-3.3.1/modules/imgproc/src/accum.cpp, line 635
terminate called after throwing an instance of 'cv::Exception'
what():  /build/opencv/src/opencv-3.3.1/modules/imgproc/src/accum.cpp:635: error: (-215) func != 0 in function accumulateWeighted

积累加权函数生成错误,我试图通过在读取这些页面后创建本地数组来修复:

http://answers.opencv.org/question/63781/accumulateweighted-problems/

Assertion failed with accumulateWeighted in OpenCV

我需要找出为什么这会在第二个循环上终止。我认为这是一个语义错误。代码按写入方式运行,但我的指令实际上并不是我打算/需要它做的。

一些可能有用的信息:

Acc最初基于gray_frame,但我意识到这种方法的设计取决于全局值平均值,导致delta帧在所有前进帧中具有原始帧的轮廓。所以我试图重新设计它以坚持使用全球平均水平。

所有3(gray_frame,average和Acc)的通道都是1。灰色框架的深度保持为0.最初的平均深度为5(在if语句之后),然后为0,而Acc的深度保持为5.看起来主循环执行一次然后退出。

在第二循环的累加加权之前的单个平均平均值与第一循环的平均值相同。大约97.89

整个C ++程序(正在进行中)可以在这里找到:https://github.com/skewballfox/SauronsEye/blob/master/SauronsEye.cpp

我去年夏天的工作,我有点用作指导(这主要是为了我练习,从来没有打算被人看到,所以它有点混乱):https://github.com/skewballfox/opencv-practice/blob/master/pi_surveillance.py

c++ opencv image-processing mat motion-detection
1个回答
0
投票

问题是当convertScaleAbs()中的缩放平均值将平均类型更改为0或CV_8U时。这是通过创建一个空的Mat()并将结果存储在那里来修复的,而不是缩放平均值本身:

Mat set_delta(Mat &average, Mat gray_frame)
{
    Mat delta_frame, scaled_average;
    //delta_frame is the storage for the difference between the accumulated average
    //and the current gray frame. scaled average is for storing the output of convertScaleAbs()
    //otherwise the depth of global average is 0 and causes an error on the second loop
    if (average.empty()==1)
    {
        gray_frame.convertTo(average, CV_32FC(gray_frame.channels()));
    }

    accumulateWeighted(gray_frame, average, .5);
    convertScaleAbs(average, scaled_average);
    absdiff(gray_frame,scaled_average,delta_frame);

    return delta_frame;
}
© www.soinside.com 2019 - 2024. All rights reserved.