本文介绍了我怎样才能使用opencv取平均100张图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有100张图像,每张图像都是598 * 598像素,我想通过取像素的平均值来消除图形和噪点,但是如果我要使用逐个像素相加",则除法会写循环直到一幅图像重复596 * 598,百幅图像重复598 * 598 * 100.

i have 100 image, each one is 598 * 598 pixels, and i want to remove the pictorial and noise by taking the average of pixels, but if i want to use Adding for "pixel by pixel"then dividing i will write a loop until 596*598 repetitions for one image, and 598*598*100 for hundred of image.

有什么方法可以帮助我完成这项手术吗?

is there a method to help me in this operation?

推荐答案

您需要遍历每个图像,并累积结果.由于这可能会导致溢出,因此您可以将每个图像转换为 CV_64FC3 图像,并累积在 CV_64FC3 图像上.您也可以为此使用 CV_32FC3 CV_32SC3 ,即使用 float integer 代替 double .

You need to loop over each image, and accumulate the results. Since this is likely to cause overflow, you can convert each image to a CV_64FC3 image, and accumualate on a CV_64FC3 image. You can use also CV_32FC3 or CV_32SC3 for this, i.e. using float or integer instead of double.

一旦累积了所有值,就可以同时使用 convertTo :

Once you have accumulated all values, you can use convertTo to both:

  • 将图像设置为 CV_8UC3
  • 将每个值除以图像数,以获得实际平均值.

这是一个示例代码,可创建100个随机图像,并计算并显示意思是:

This is a sample code that creates 100 random images, and computes and shows themean:

#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;
}

这篇关于我怎样才能使用opencv取平均100张图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 05:33