Numpy 错误的参数错误,因为我试图使用Keras预处理函数向图像添加噪声
为了执行图像预处理,我尝试使用Keras中的类。以下是如何使用它:Numpy 错误的参数错误,因为我试图使用Keras预处理函数向图像添加噪声,numpy,opencv,image-processing,keras,computer-vision,Numpy,Opencv,Image Processing,Keras,Computer Vision,为了执行图像预处理,我尝试使用Keras中的类。以下是如何使用它: data_generator = keras.preprocessing.image.ImageDataGenerator( rotation_range = 60, width_shift_range = 0.1, height_shift_range = 0.1, brightness_range = [0.5, 1.5], shear_
data_generator = keras.preprocessing.image.ImageDataGenerator(
rotation_range = 60,
width_shift_range = 0.1,
height_shift_range = 0.1,
brightness_range = [0.5, 1.5],
shear_range = 0.01,
zoom_range = [0, 1],
horizontal_flip = True,
vertical_flip = True,
preprocessing_function = preprocess_other
)
preprocessing_function
属性已分配一个名为preprocess_other
的函数,定义如下:
def preprocess_other(image):
flip = np.random.random()
if flip > 0.5:
# Add noise
blank_image = np.zeros(image.shape, np.uint8)
cv2.randn(blank_image, 0, 5)
noisy_image = cv2.add(image, blank_image)
return noisy_image
else:
# Return the original image
return image
此函数的作用是以0.5的概率向图像添加噪声
当我开始训练过程(训练CNN)时,它会工作几秒钟,但由于preprocess\u other
函数出现错误而失败,错误如下:
error: OpenCV(3.4.3) /io/opencv/modules/core/src/arithm.cpp:683:
error: (-5:Bad argument) When the input arrays in
add/subtract/multiply/divide functions have different types,
the output array type must be explicitly specified in function
'arithm_op'
我进行了调试,但无法理解其原因。我是否试图以错误的方式添加噪音?如何更正此错误?问题在于
图像
和空白图像
有不同的类型
您可以更改:
blank_image = np.zeros(image.shape, np.uint8)
致:
或:
blank_image = np.zeros_like(image)
试试:
blank\u image=np.zeros(image.shape,image.dtype)
或np.zeros\u like(image)
。另外,如果您没有翻转图像,请不要命名您的开关变量flip
:)我建议您进行检查,但我认为此时您的图像可能在[0,1]
范围内,因此[0,5]
可能噪音太大。@Berriel好的!我已经应用了1./255
的重缩放,但是当调用函数时,最大值是255
,所以我想5
是可以的。当然,检查总是好的,这就是我说的原因。我的第一个建议有助于解决你的问题吗?@Berriel有。这是个错误。你可以把它贴出来作为答案。
blank_image = np.zeros_like(image)