c++ Opencv accumulateWeighted - 奇怪的行为

标签 c++ opencv image-processing visual-studio-2012 background-subtraction

我正在尝试从我的摄像头中获取帧的运行平均值,但几秒后,平均帧的图像变得越来越亮,而不是白色。

我的摄像头提供具有 3 个 channel 的灰度图像。 我在 Windows 7、Visualstudio 2012、opencv 243 上

#include<opencv2\opencv.hpp>
#include<opencv2\core\core.hpp>
#include<opencv2\highgui\highgui.hpp>
#include <iostream>

using namespace cv;
using namespace std;


int main(int argc, char* argv[])
{
    VideoCapture cap(0);
    Mat frame1;
    cap.read(frame1);
    Mat acc = Mat::zeros(frame1.size(), CV_32FC1);

    while(1){
        Mat frame;
        Mat gray;
        cap.read(frame);
        cvtColor(frame ,gray ,CV_BGR2GRAY,0);
        accumulateWeighted(gray, acc,0.005);
        imshow("gray", gray);
        imshow("acc", acc);
        waitKey(1); //don't know why I need it but without it the windows freezes
    }
}

谁能告诉我我做错了什么? 谢谢!

最佳答案

这里的问题是 imshow 如何将矩阵值映射到像素值。通常,来自凸轮的原始数据以整数数据类型的形式出现,通常在 [0, 255] 范围内。 accumulateWeighted 函数执行您期望的操作并计算帧的运行平均值。所以 acc 是一个浮点矩阵,其值在 [0, 255] 中。

现在,当您将该矩阵传递给 imshow 时,矩阵值需要映射到强度。由于数据类型是浮点类型,0 被映射到黑色,1 被映射到白色,范围之外的所有内容都被剪掉。因此,只有当图像的某个区域非常很暗并保持这种状态时,运行平均值才会保持在 1 以下并映射到纯白色以外的其他颜色。

幸运的是,修复很简单:

imshow("acc", acc/255);

关于c++ Opencv accumulateWeighted - 奇怪的行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18748775/

相关文章:

python - 在opencv python中将颜色从黑色反转为白色

c++ - 在 QLineEdit 中显示浮点值

c++ - 在 MFC 中,如何避免对话框停留在我的应用程序窗口的顶部?

python - 如何跟踪具有特定颜色的矩形

algorithm - 用于车牌识别的洪水填充

image-processing - 如何在opencv中找到图像的最大和最小亮度值?

c++ - 将 std::find() 与反向迭代器一起使用

c++ - 使用 Dart 端口将 Float32x4 数组从 native 主机发送到 Dart 虚拟机

c++ - 为 Visual Studio 2010 设置 OpenCV-2.3

python - 张开嘴需要Haar Casscade xml