未显示用于灰度图像数据增强的通道宽度
Posted
技术标签:
【中文标题】未显示用于灰度图像数据增强的通道宽度【英文标题】: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)
如何将额外的通道轴添加到维度?灰度图像的数据增强还有其他解决方案吗?
【问题讨论】:
回答有用吗? 【参考方案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)
原图:
增强示例:
【讨论】:
以上是关于未显示用于灰度图像数据增强的通道宽度的主要内容,如果未能解决你的问题,请参考以下文章