我如何使用 opencv 取 100 张图像的平均值?

新手上路,请多包涵

我有 100 张图像,每张是 598 * 598 像素,我想通过取像素的平均值来消除图像和噪声,但是如果我想使用“逐像素添加”,那么除法我会写一个循环,直到一张图片重复596*598,一百张图片重复598*598*100。

有什么方法可以帮助我进行此操作吗?

原文由 Karmel Zaidan 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 976
2 个回答

您需要遍历每个图像,并累积结果。由于这可能导致溢出,您可以将每个图像转换为 CV_64FC3 图像,并在 CV_64FC3 图像上进行累积。 You can use also CV_32FC3 or CV_32SC3 for this, ie using float or integer instead of double .

累积所有值后,您可以同时使用 convertTo

  • 使图像成为 CV_8UC3
  • 将每个值除以图像的数量,以获得实际平均值。

这是一个创建 100 个随机图像并计算并显示平均值的示例代码:

 #include <opencv2\opencv.hpp>
using namespace cv;

Mat3b getMean(const vector<Mat3b>& images)
{
    if (images.empty()) return Mat3b();

    // Create a 0 initialized image to use as accumulator
    Mat m(images[0].rows, images[0].cols, CV_64FC3);
    m.setTo(Scalar(0,0,0,0));

    // Use a temp image to hold the conversion of each input image to CV_64FC3
    // This will be allocated just the first time, since all your images have
    // the same size.
    Mat temp;
    for (int i = 0; i < images.size(); ++i)
    {
        // Convert the input images to CV_64FC3 ...
        images[i].convertTo(temp, CV_64FC3);

        // ... so you can accumulate
        m += temp;
    }

    // Convert back to CV_8UC3 type, applying the division to get the actual mean
    m.convertTo(m, CV_8U, 1. / images.size());
    return m;
}

int main()
{
    // Create a vector of 100 random images
    vector<Mat3b> images;
    for (int i = 0; i < 100; ++i)
    {
        Mat3b img(598, 598);
        randu(img, Scalar(0), Scalar(256));

        images.push_back(img);
    }

    // Compute the mean
    Mat3b meanImage = getMean(images);

    // Show result
    imshow("Mean image", meanImage);
    waitKey();

    return 0;
}

原文由 Miki 发布,翻译遵循 CC BY-SA 3.0 许可协议

首先-将图像转换为浮点数。你有 N=100 张图片。想象一下,单个图像是 1 个图像的平均像素值的数组。您需要计算 N 个图像的平均像素值数组。

A - X 图像的平均像素值数组, B - Y 67178899823ad3f 图像的平均像素值数组然后 C = (A * X + B * Y) / (X + Y) - X + Y 图像的平均像素值数组。为了在浮点运算中获得更好的精度 XY 应该大致相等

您可以合并所有图像,例如 合并排序 中的子数组。 In you case merge operation is C = (A * X + B * Y) / (X + Y) where A and B are arrays of average pixel values of X and Y 图片

原文由 Sergey Krivohatskiy 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题