首页 > 解决方案 > ImageDataGenerator 不显示我的 RGB processing_function 的图像颜色(使用 openCV)

问题描述

我正在尝试将 a 传递processing_function给我ImageDataGenerator进行基于颜色的分割,它基本上只保留图像的绿色像素。我在代码中的每一行解释:

def segmented(image):

#Input is a NumPy array rank 3
np_image = np.array(image)

#Keras uses load_img to read the images in RGB

# For segmentation, we convert to HSV to get the mask
hsv_foto = cv2.cvtColor(foto, cv2.COLOR_RGB2HSV)

#The mask threshold: green colors
colormin=(25,50,50)
colormax=(86,255,255)

# Get the mask
mask = cv2.inRange(hsv_foto, colormin , colormax)

#Apply the mask on the RGB image (foto= RGB so result= RGB as well)
result = cv2.bitwise_and(foto, foto, mask=mask)

#Keras reads the images with pillow so we convert to Pil image
 pil_image= Image.fromarray(result)

#Return a NumPy array rank 3
return pil_image

此功能在 可视化第一行有微小变化的分段图像时工作正常(我使用 cv2.imread 打开图像)。

问题是在将此函数ImageDataGenerator作为processing_function.

train_datagen = ImageDataGenerator(
    rescale=1./255,
    shear_range=0.2,
    zoom_range=0.2,
    horizontal_flip=True,
    validation_split=0.2,
    preprocessing_function = segmented)

基本上,我在ImageDataGeneratorx,y = train_generator.next()

TypeError: unsupported operand type(s) for *=: 'Image' and 'float'

有谁知道为什么会出现这个错误?

标签: opencvtensorflowimage-processingkerascomputer-vision

解决方案


序列

foto = cv2.cvtColor(np_image, cv2.COLOR_BGR2RGB)
hsv_foto = cv2.cvtColor(foto, cv2.COLOR_BGR2HSV)

没有按照您的预期做,因为foto是 RGB,而不是 BGR。

尝试

hsv_foto = cv2.cvtColor(np_image, cv2.COLOR_BRG2HSV)

推荐阅读