【问题标题】:Channel width not displayed for data augmentation of Grayscale image未显示用于灰度图像数据增强的通道宽度
【发布时间】:2022-01-19 10:09:45
【问题描述】:

我有一张灰度图像,想使用 Keras 执行增强方法。 问题:导入图像后,它的尺寸缺少通道宽度,因此 ImageDataGenerator 面临问题。

#importing libraries 

import keras 
from keras import backend as K
import imageio
from keras.preprocessing.image import ImageDataGenerator
from skimage import io
from skimage import color
import numpy as np
from scipy import misc, ndimage


# Reading image

img = io.imread('img1.png')
img = img.reshape((1, ) + img.shape )  #reshaping the existing (height, width) dimension to (1, height, width)


# ImageDataGenerator class for augumentation

datagen = ImageDataGenerator(
        rotation_range=45, 
        width_shift_range=0.2, 
        height_shift_range=0.2, 
        shear_range=0.2, 
        zoom_range=0.2, 
        horizontal_flip=True,
        fill_mode='constant', cval=255)


# Creating an iterator for datagen.flow (we use this since currently working only on 1 image)

i = 0
for batch in datagen.flow(img, batch_size=5, save_to_dir="augumented", save_prefix="aug", save_format="png"):
    i += 1
    if i>20:
        break


我收到以下错误

Input data in `NumpyArrayIterator` should have rank 4. You passed an array with shape', (1, 2054, 2456)

如何将额外的通道轴添加到维度?灰度图像的数据增强还有其他解决方案吗?

【问题讨论】:

  • 回答有用吗?

标签: python numpy machine-learning keras grayscale


【解决方案1】:

只需使用tf.expand_dims为图像添加尺寸:

img = io.imread('/content/result_image.png')
img = img.reshape((1, ) + img.shape )  #reshaping the existing (height, width) dimension to (1, height, width)
img = tf.expand_dims(img, axis=-1)

原图:

增强示例:

【讨论】:

    猜你喜欢
    • 2014-05-30
    • 2013-05-20
    • 1970-01-01
    • 1970-01-01
    • 2017-09-04
    • 2018-02-21
    • 2015-02-13
    • 2019-01-19
    • 2012-11-20
    相关资源
    最近更新 更多