ImageDataGenerator不';t显示我的RGB处理函数的图像颜色(使用openCV)

ImageDataGenerator不';t显示我的RGB处理函数的图像颜色(使用openCV),opencv,tensorflow,image-processing,keras,computer-vision,Opencv,Tensorflow,Image Processing,Keras,Computer Vision,我试图将一个处理函数传递给我的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

我试图将一个
处理函数
传递给我的
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打开图像)时工作正常

问题在于将此函数作为
处理函数传递给my
ImageDataGenerator

train_datagen = ImageDataGenerator(
    rescale=1./255,
    shear_range=0.2,
    zoom_range=0.2,
    horizontal_flip=True,
    validation_split=0.2,
    preprocessing_function = segmented)
基本上,我是在用
x,y=train\u generator.next()可视化
ImageDataGenerator
的转换结果时遇到这个错误的

TypeError:不支持的操作数类型*=:“Image”和“float”

有人知道为什么会出现这种错误吗?

序列

foto = cv2.cvtColor(np_image, cv2.COLOR_BGR2RGB)
hsv_foto = cv2.cvtColor(foto, cv2.COLOR_BGR2HSV)
因为
foto
是RGB,而不是BGR,所以没有做您可能期望的事情

试一试


ِ您是否检查了输出类型?它是浮动的吗?将图像乘以255,看看是否正确。我检查了一下,似乎正确!这是输出()dtype=uint8在分段的命令之间放置一些日志,然后查看数据变为零的位置。@Meisam您的确切意思是什么?函数在ImageDataGenerator之前工作正常,但在它之后返回黑色图像。我应该在哪一点将日志放在命令之间?感谢您的反馈!只需将打印(foto),打印(hsv_foto)。。。在分段函数中,运行代码。Yo将捕获生成零数组的第一个命令。感谢您的反馈!很抱歉,我认为我的代码中有一个输入错误,我纠正了它:我尝试了两种组合,但都不起作用。你能再检查一下我更正的代码吗!
hsv_foto = cv2.cvtColor(np_image, cv2.COLOR_BRG2HSV)