【问题标题】:Change shape, upscale, image array in Neural Net在神经网络中改变形状、高档、图像阵列
【发布时间】:2020-06-22 23:58:02
【问题描述】:

我有一组图像像素值,我想将其放大以输入到我的神经网络中。它是一个形状数组 (28000, 48, 48, 1)。这些是归一化的图像像素值,并且希望将它们放大到更高的分辨率以输入到我的 CNN 中。数组看起来像这样......

array([[[[-0.6098866 ],
         [-0.4592209 ],
         [-0.40325198],
         ...,
         [-0.7694696 ],
         [-0.90518403],
         [-0.95160526]],

        [[-0.66049284],
         [-0.68162924],
         [-0.694159  ],

我的X_trainy_train 图像数组的形状都是(28000,48,48,1)。我想将这 28000 个图像阵列放大或调整大小为 75x75。请帮忙。我应该将数组转换回非标准化数组或图像,然后使用 cv2 进行升级吗?我该怎么做?

【问题讨论】:

  • 试过scipy.misc.imresize()skimage.transform.resize() 吗?
  • 也在调查这个。对于我只有数组而不是图像的用例来说很难。
  • import matplotlib.pyplot as plt import cv2 image_array = X_train[0] img = image_array.reshape(48,48) resized_width = 75 resized_height = 75 dimension = (resized_width, resized_height) resized = cv2.resize (img, 尺寸, 插值 = cv2.INTER_CUBIC) print(resized.shape) image_array = resized.flatten() print(len(image_array))
  • 上面的代码在 cv2 上也能正常工作。这让我在展平后得到了一个长度为 5625(75x75)的数组。所以现在我需要对所有 28000 个图像执行此操作以获得最终大小数组 (28000, 5625) 我将如何循环并执行此操作?

标签: python computer-vision conv-neural-network object-detection image-resizing


【解决方案1】:

调整图像大小的一种简单方法是使用 Python 模块 PIL(Python 图像库),您可以使用 pip install pillow 安装它。下面的示例演示如何调整单个图像的大小:

import numpy as np
from PIL import Image
import matplotlib.pyplot as plt

# Open image
panda_pil = Image.open("panda.jpg")
print(np.array(panda_pil).shape)
# (613, 696, 3)

panda_pil_resized = panda_pil.resize((75, 75))
print(np.array(panda_pil_resized).shape)
# (75, 75, 3)

plt.imshow(np.array(panda_pil_resized))
plt.show()

熊猫图片下载方式如下:

import urllib.request

panda_fname = "panda.jpg"
panda_url = "https://upload.wikimedia.org/wikipedia/commons/f/fe/Giant_Panda_in_Beijing_Zoo_1.JPG"
urllib.request.urlretrieve(panda_url, panda_fname)

要调整所有 28000 张图像的大小,一种方法是将其作为 for 循环中的预处理步骤,并将图像保存到 numpy 数组中。

编辑:您可以遍历原始 28000x2304 图像阵列,并在 for-loop 中单独放大每个图像。要从np.ndarray 对象中获取PIL.Image 对象,您可以使用Pil.Image.from_array,如下所示(我刚刚生成了一个随机高斯噪声数组,但它应该与您的图像相同):

import numpy as np
from PIL import Image
from time import perf_counter

old_width, old_height = 48, 48
new_width, new_height = 75, 75
num_images = 28000

old_image_array = np.random.normal(size=[num_images, old_width*old_height])
new_image_array = np.empty(shape=[num_images, new_width*new_height])

print("Starting conversion...")
t0 = perf_counter()

# Loop over each image individually
for i in range(num_images):
    # Get the ith image and reshape
    old_image = old_image_array[i].reshape(old_width, old_height)
    # Convert to PIL.Image
    old_image_pil = Image.fromarray(old_image)
    # Upscale resolution
    new_image_pil = old_image_pil.resize((new_width, new_height))
    # Convert to numpy array
    new_image = np.array(new_image_pil)
    # Reshape and store in new image array
    new_image_array[i] = new_image.reshape(new_width*new_height)

t1 = perf_counter()
print("Time taken = {:.3f} s".format(t1 - t0))
print(old_image_array.shape, new_image_array.shape)

控制台输出:

Starting conversion...
Time taken = 2.771 s
(28000, 2304) (28000, 5625)

很可能有一种更有效的方法来做到这一点,但这种方法很简单,如果你还不知道它们,它使用的工具很有用(PIL 是一个很好的图像处理模块,如果您想了解更多关于PIL的信息,请参阅this blog post)。

【讨论】:

  • 看起来这是朝着正确的方向,或者是朝着我想要实现的目标迈出了一大步。我唯一的问题是我没有原始图像,只有一个包含 28000 行的 48x48 图像的 csv 文件。所以只有数组而不是原始图像。您开始打开图像,我没有要打开的图像。那么在调整大小和使用 .resize 函数之前,我必须先将数组转换为图像吗?我不确定我会如何写这个 for 循环。
  • 这是我的 X_train 数组在标准化之前的样子... [[ 70. 80. 82. ... 106. 109. 82.] [151. 150. 147. ... 193. 183. 184.] [231. 212. 156. ... 88. 110. 152.]] X_train.shape -> (28000, 2304) 所以这就是我必须开始的。我该如何循环呢?
  • import cv2 image_array = X_train[0] img = image_array.reshape(48,48) resized_width = 75 resized_height = 75 dimension = (resized_width, resized_height) resized = cv2.resize(img, dimension, interpolation = cv2.INTER_CUBIC) print(resized.shape) image_array = resized.flatten() print(len(image_array))
  • 上面的代码在 cv2 上也能正常工作。这让我在展平后得到了一个长度为 5625(75x75)的数组。所以现在我需要对所有 28000 个图像执行此操作以获得最终大小数组 (28000, 5625) 我将如何循环并执行此操作?
  • @willbecoding 查看我上面的编辑。这能回答你的问题吗?
猜你喜欢
  • 2020-10-07
  • 2020-12-28
  • 2017-05-28
  • 2015-10-15
  • 2022-01-02
  • 1970-01-01
  • 2016-11-29
  • 1970-01-01
相关资源
最近更新 更多