0

我正在尝试从我的摄像头获取运行平均帧数,但几秒钟后,平均帧数的图像变得越来越亮,而且比白色更亮。

我的 cam 提供具有 3 个通道的灰度图像。我在 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
    }
}

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

4

2 回答 2

2

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

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

幸运的是,修复很简单:

imshow(“acc”,acc/255);

于 2014-10-17T19:34:25.660 回答
1

我为这个问题找到了一个更优雅的解决方案。您需要的功能已经由 OpenCV 提供。这适用于 3 通道彩色或 1 通道灰度图像:

方法convertScaleAbs

内联文档

缩放数组元素,计算绝对值并将结果转换为 8 位无符号整数:dst(i)=saturate_castabs(src(i)*alpha+beta)

签名

convertScaleAbs(InputArray src, OutputArray dst, double alpha=1, double beta=0)

示例代码

// Variables
Mat frame;
Mat accumulator;
Mat scaled;

// Initialize the accumulator matrix
accumulator = Mat::zeros(frame.size(), CV_32FC3);

while(1){
   // Capture frame
   capture >> frame;

   // Get 50% of the new frame and add it to 50% of the accumulator
   accumulateWeighted(frame, accumulator, 0.5);

   // Scale it to 8-bit unsigned
   convertScaleAbs(accumulator, scaled);

   imshow("Original", frame);

   imshow("Weighted Average", scaled);

   waitKey(1);
}
于 2015-02-08T23:46:08.313 回答