【问题标题】:How can I take generate mean image of many 32x32 grayscale images in Python?如何在 Python 中生成许多 32x32 灰度图像的平均图像?
【发布时间】:2023-03-12 03:20:01
【问题描述】:

我的数组是 50000x32x32。 arr[i] 存储第 i 个灰度图像。

我想计算这些图像的平均图像。我尝试了以下代码(我从堆栈溢出本身获得了此代码)。此代码实际上是用于 RGB 图像的。

我知道,我的这些改动有很多错误,抱歉。

import os, numpy, PIL
from PIL import Image

# Access all PNG files in directory
allfiles=os.listdir(os.getcwd())
imlist=arr
N=len(imlist)
# Assuming all images are the same size, get dimensions of first         image
w,h=Image.fromarray(imlist[0]).size


# Create a numpy array of floats to store the average (assume RGB images)
brr=numpy.zeros((h,w),numpy.float)

# Build up average pixel intensities, casting each image as an array     of floats
for im in imlist:
    imarr=numpy.array(Image.fromarray(im),dtype=numpy.float)
    brr=brr+imarr/N

# Round values in array and cast as 8-bit integer
brr=numpy.array(numpy.round(arr),dtype=numpy.uint8)

# Generate, save and preview final image
out=Image.fromarray(brr,mode="L")
out.save("Average.png")
out.show()

【问题讨论】:

  • 其实在我的机器学习作业中,我知道这是一个 DIP 问题,这是我第一次接触到 DIP 的东西
  • 标签与问题的内容有关,不是它的上下文;即,您可能需要帮助,例如,随后在宇宙飞船中使用的排序算法,这并不意味着问题与space-engineering有关。
  • 你还没有说问题出在哪里。你有错误吗?结果不是你预期的吗?您能否编辑您的问题以澄清您预期会发生什么以及出了什么问题。

标签: python numpy image-processing python-imaging-library grayscale


【解决方案1】:

拥有 5000 × 32 × 32 数组后,您可以使用 np.mean()axis=0(第一个轴,包含图像集合)来计算平均图像。让我们做一些随机数据:

import numpy as np
images = np.random.random((5000, 32, 32))

现在我们可以计算平均图像了:

mean_image = images.mean(axis=0)

我们可以这样看:

import matplotlib.pyplot as plt
plt.imshow(mean_image)

看起来像:

【讨论】:

    猜你喜欢
    • 2014-11-29
    • 1970-01-01
    • 2016-06-05
    • 1970-01-01
    • 2013-11-14
    • 2016-12-03
    • 1970-01-01
    • 2012-04-27
    • 1970-01-01
    相关资源
    最近更新 更多