opencv - 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)
基本上,我在ImageDataGenerator
用x,y = train_generator.next()
TypeError: unsupported operand type(s) for *=: 'Image' and 'float'
有谁知道为什么会出现这个错误?
解决方案
序列
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)
推荐阅读
- amazon-web-services - 如何解决这个 gRPC 安装问题?
- php - 每个类别的自定义模板
- django - 如何使用reactjs在django url中的url中获取媒体图像
- plsql - 在plsql中打印n个数字的总和
- scrapy - 如何链接 items.py 和我的蜘蛛文件?
- kubernetes - 错误“没有可用于此声明的持久卷并且未设置存储类”
- ocaml - “模式匹配不详尽”和“此匹配案例未使用”问题
- sql - 显示未使用 HDFC 银行进行任何预订的用户名及其所在城市
- reactjs - Formik中如何自定义错误显示逻辑?
- python - 如何在 Python 中模仿 Excel 的 LOGEST 函数