【问题标题】:How can i reshape an array from (280, 280, 3) to (28, 28, 3)如何将数组从 (280, 280, 3) 重塑为 (28, 28, 3)
【发布时间】:2020-10-05 01:37:33
【问题描述】:

您好,我尝试编写代码,使用 pygame 在屏幕上写一个数字,然后神经网络预测我写的数字。我的问题是我用 (28, 28, 3) 中的图像数组训练了我的神经网络。所以我试图重塑我的 (280, 280, 3) 数组。但是当我这样做时,我的数组是无的。 我使用 Python 3.7

string_image = pygame.image.tostring(screen, 'RGB')
temp_surf = pygame.image.fromstring(string_image, (280, 280), 'RGB')
array = pygame.surfarray.array3d(temp_surf)
array = array.resize((28, 28, 3))

谁能帮忙?

【问题讨论】:

  • 问题解决了吗?

标签: python-3.x pygame artificial-intelligence datashape


【解决方案1】:

我不熟悉pygame,但看起来pygame.surfarray.array3d 返回一个numpy-array。

How to iterate through a pygame 3d surfarray and change the individual color of the pixels if they're less than a specific value?

要保持相同数量的数据点,只需更改形状,您可以使用numpy.reshape

import numpy as np
c = 28*28*3 # just to start off with right amount of data
a = np.arange(c) # this just creates the initial data you want to format
b = a.reshape((28,28,3)) # this actually reshapes the data
d = b.shape #this just stores the new shape in variable d
print(d) #shows you the new shape

要将数据插入到新的大小,有多种方法,例如 CV 或继续 numpy 示例。

ary = np.resize(b,(18,18,1))
e = ary.shape
print(e)

希望对您有所帮助。

【讨论】:

    【解决方案2】:

    如果您只想缩放pygame.Surface,那么我建议使用pygame.transform.scale()pygame.transform.smoothscale()

    例如:

    temp_surf = pygame.image.fromstring(string_image, (280, 280), 'RGB')
    scaled_surf = pygame.transform.smoothscale(temp_surf, (28, 28))
    array = pygame.surfarray.array3d(scaled_surf)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-11-29
      • 2015-07-22
      • 1970-01-01
      • 2019-09-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多