【问题标题】:ImageDataGenerator doesn't display the image colors of my RGB processing_function (Using openCV)ImageDataGenerator 不显示我的 RGB processing_function 的图像颜色(使用 openCV)
【发布时间】: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


【解决方案1】:

顺序

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)

【讨论】:

  • 感谢您的反馈!很抱歉,我认为我的代码中有错字,我更正了它:我尝试了两种组合,但没有奏效。您能否再次检查我更正的代码!
猜你喜欢
  • 2015-12-15
  • 2018-11-26
  • 1970-01-01
  • 1970-01-01
  • 2023-01-31
  • 2015-09-26
  • 2011-07-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多