【发布时间】:2020-04-18 20:05:17
【问题描述】:
我正在尝试将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
当visualizing the segmented images 在第一行稍作改动时,此功能可以正常工作(我使用 cv2.imread 打开图像)。
问题是在将此函数作为processing_function 传递给我的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_generator.next() 可视化ImageDataGenerator 的转换结果时遇到此错误
TypeError: unsupported operand type(s) for *=: 'Image' and 'float'
有谁知道为什么会出现这个错误?
【问题讨论】:
-
ِ你检查输出类型了吗?是浮动的吗?将图像乘以 255,看看是否正确。
-
我检查过,似乎是正确的!这是输出 (ibb.co/9y8Ff7Y) dtype=uint8
-
在命令之间分段放一些日志,看看数据在哪里变为零。
-
@Meisam 你到底是什么意思?该函数在 ImageDataGenerator 之前工作正常,但在它之后返回黑色图像。我应该在什么时候将日志放在命令之间?感谢您的反馈!
-
只需将 print(foto), print(hsv_foto), ... 放入分段函数并运行您的代码。 Yo 将捕获第一个生成零数组的命令。
标签: opencv tensorflow image-processing keras computer-vision