【问题标题】:resize image of a 4-dimensional array in python在python中调整4维数组的图像大小
【发布时间】:2017-08-18 13:03:00
【问题描述】:

我正在与Python 3 合作。

我有一个来自unit8 类型和大小(60000, 1, 28, 28) 的变量数据,这意味着我有60000 具有1 个通道和28x28 像素大小的图像。
现在我想将图像大小调整为60x60 像素,这样我就得到了(60000, 1, 60, 60) 的大小

有人可以帮我吗?

【问题讨论】:

    标签: python python-3.x lasagne


    【解决方案1】:

    以下解决方案对我有用。在列表理解之后,列表将被堆叠回一个带有 numpy 的批次:

    import cv2
    import numpy as np
    
    target_shape = (60, 60)
    
    def _resize_image(image, target):
       return cv2.resize(image, dsize=(target[0], target[1]), interpolation=cv2.INTER_LINEAR)
    
    image = [_resize_image(image=i, target=target_shape) for i in data]
    data_batch = np.stack(image, axis=0)
    

    【讨论】:

      【解决方案2】:

      如果你有opencv,你可以使用resize

      img = pics[i][0]
      new_img = cv2.resize(img, None, fx=2, fy=2, interpolation = cv2.INTER_CUBIC)
      

      现在您的ith 图像的大小从xy 方向调整为2 倍(这里picsnumpy 数组与您的(60000, 1, 28, 28) 图像)。如果你不使用opencv,你可以安装它(搜索google)

      如果没有,您可以使用PIL 包。您可以使用它来安装它,

      pip install Pillow
      

      并使用以下代码调整大小。

      from PIL import Image
      img = pics[i][0]
      im = Image.fromarray(np.rollaxis(img, 0,3))
      new_im = im.resize((new_x, new_y), Image.BICUBIC)
      

      您可以使用for loop 将这些图像放入(60000, 1, x ,y) 的数组中

      【讨论】:

        【解决方案3】:

        如果您使用的是 Keras,请在 Keras 模型设置中使用它

        model = Sequential()
        model.add(Lambda(lambda x: tf.image.resize_images(x,[66,200]), input_shape=(160,320,3)))
        

        【讨论】:

          猜你喜欢
          • 2018-12-18
          • 1970-01-01
          • 2018-11-15
          • 2016-08-25
          • 2013-10-24
          • 1970-01-01
          • 1970-01-01
          • 2021-04-21
          相关资源
          最近更新 更多