gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 17:56:40 25 4
gpt4 key购买 nike

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

有什么方法可以帮助我完成这个操作吗?

最佳答案

您需要遍历每个图像,并累积结果。由于这很容易造成溢出,所以可以将每张图片转换成一张CV_64FC3图片,并累加到一张CV_64FC3图片上。您也可以为此使用 CV_32FC3CV_32SC3,即使用 floatinteger 而不是 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;
}

关于c++ - 我如何使用 opencv 取 100 张图像的平均值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35668074/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com