ImageDataGenerator不显示我的RGB processing_function的图像颜色(使用openCV)

时间:2019-12-30 01:09:21

标签: opencv tensorflow image-processing keras computer-vision

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

def segmented(image):

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

#openCV read the images in BGR so I convert them to RGB
foto = cv2.cvtColor(np_image, cv2.COLOR_BGR2RGB)

# 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)

#Return a NumPy array rank 3
return result

visualizing the segmented images的第一行中有一点点更改(我使用cv2.imread打开图像),此功能可以正常工作。

问题是将该功能作为ImageDataGenerator传递到我的processing_function时。基本上,我没有任何错误,这意味着图像的输入和输出形状都很好。但是,在使用ImageDataGenerator来可视化x,y = train_generator.next()的转换结果时

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

我得到了完全黑图像,我看不到processing_function分割出的像素。

我知道Keras仅适用于RGB或灰度图像。并且根据我的功能,图像在RGB颜色空间中。有人可以解释一下为什么ImageDataGenerator不读取我的分割图像吗?

我认为我缺少与openCV和keras ImadeDataGenerator的色彩空间之间的转换有关的内容

1 个答案:

答案 0 :(得分:0)

序列

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)