首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >背景减法的图像平均值.不正确的结果

背景减法的图像平均值.不正确的结果
EN

Stack Overflow用户
提问于 2013-12-24 20:35:01
回答 1查看 542关注 0票数 0

当我试图总结存储在一个列表中的N个以前的帧,然后除以num帧,产生的背景模型并不像预期的那样。我能看出来,因为我在前面的视频中尝试过Matlab中的algo。

代码语言:javascript
复制
class TemporalMeanFilter {
private:
    int learningCount;
    list<Mat> history;
    int height, width;

    Mat buildModel(){
        if(history.size() == 0)
            return Mat();

        Mat image_avg(height, width, CV_8U, Scalar(0));
        double alpha = (1.0/history.size());

        list<Mat>::iterator it = history.begin();
        cout << "History size: " << history.size() << " Weight per cell: " << alpha << endl;

        while(it != history.end()){
            image_avg += (*it * alpha);
            it++;
        }

        return image_avg;
    }

public:
    TemporalMeanFilter(int height, int width, int learningCount){
        this->learningCount = learningCount;
        this->height = height;
        this->width = width;
    }

    void applyFrameDifference(Mat& frame_diff, Mat& bg_model, Mat& input_frame){
        if(history.size() == learningCount)
            history.pop_front();

        history.push_back(input_frame);

        bg_model = buildModel();
        frame_diff = bg_model - input_frame;
    }
};

//主要内容如下

// .从文件中读取视频

代码语言:javascript
复制
TemporalMeanFilter meanFilter(height, width, 50); //background subtraction algorithm
meanFilter.applyFrameDifference(diff_frame, bg_model, curr_frame);

//.在屏幕上显示。凸起端

图片:http://imagegur.com/images/untitled.png

左边是bg_model,中间是curr_frame,右边是输出。

也许是因为在CV_U8上做了四舍五入?我试图更改为CV_32FC1,但后来程序崩溃了,因为出于某种原因,它无法添加两个CV_32FC1矩阵。

任何洞察力都将不胜感激。谢谢!

更多信息:

在类中,我现在将平均值保存在CV_16UC1 Mat中,以防止裁剪,以及在连续添加后如何导致错误。

add函数/运算符+都将结果类型从CV_16UC1更改为CV8UC1。这个错误是由这个错误引起的。有什么建议要求它保存原始数据类型吗?(我礼貌地问.不起作用)

代码语言:javascript
复制
background_model += *it;

OpenCV Error: Bad argument (When the input arrays in add/subtract/multiply/divid
e functions have different types, the output array type must be explicitly speci
fied) in unknown function, file C:\buildslave64\win64_amdocl\2_4_PackSlave-win32
-vc11-shared\opencv\modules\core\src\arithm.cpp, line 1313
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-12-25 09:44:56

你说得对,这几乎肯定是你通过累积比例灰度值而得到的舍入误差。但是,它没有理由使用浮点像素崩溃,所以您应该尝试如下所示:

代码语言:javascript
复制
Mat buildModel()
{
    if (history.size() == 0)
        return Mat();

    Mat image_avg = Mat::zeros(height, width, CV_32FC1);
    double alpha = (1.0 / history.size());

    list<Mat>::iterator it = history.begin();

    while (it != history.end())
    {
        Mat image_temp;
        (*it).convertTo(image_temp, CV_32FC1);
        image_avg += image_temp;
        it++;
    }
    image_avg *= alpha;

    return image_avg;
}

根据您想要对结果做什么,您可能需要将其规范化或重新调整,或者在显示之前将其转换回灰度,等等。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20766488

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档