【问题标题】:Converting image to array using Open-CV and Kreas giving different output使用 Opencv 和 Keras 将图像转换为数组,给出不同的输出
【发布时间】:2021-07-16 19:03:54
【问题描述】:

我使用 cv2 和 keras 将图像转换为数组,并注意到两者都给出了不同的结果,为什么会这样?我正在读取相同的图像并调整它的大小并使用两种方法获得不同的结果。

方法一

img_array = cv2.imread("pic_data\others\C_E8415_S.png")
new_array = cv2.resize(img_array, (200, 200))
new_array[:,1,1]

array([239, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
       255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 247,
       255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
       255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
       255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
       255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
       255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
       255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
       255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
       255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
       255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
       255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
       255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
       255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
       255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
       255, 255, 255, 255, 239], dtype=uint8)

方法二

from keras.preprocessing import image
from keras.preprocessing.image import img_to_array

image = image.load_img("pic_data\others\C_E8415_S.png")
image = image.resize((200,200))
image = img_to_array(image, dtype='uint8')
image[:,1,1]

array([236, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
       255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 251, 239,
       255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
       255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 248, 241, 255,
       255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
       255, 255, 255, 255, 255, 255, 255, 255, 255, 248, 242, 255, 255,
       255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
       255, 255, 255, 255, 255, 255, 255, 255, 245, 245, 255, 255, 255,
       255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
       255, 255, 255, 255, 255, 255, 255, 244, 245, 255, 255, 255, 255,
       255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
       255, 255, 255, 255, 255, 255, 241, 248, 255, 255, 255, 255, 255,
       255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
       255, 255, 255, 255, 255, 241, 248, 255, 255, 255, 255, 255, 255,
       255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
       255, 255, 255, 255, 236], dtype=uint8)

【问题讨论】:

  • keras.preprocessing.image.load_img 返回一个 PIL Image 的实例,所以它实际上是关于 OpenCV 与 PIL。我不确定是否有任何声明保证两种实现都会产生相同的结果,但即使有,你也不是在比较同一件事。在这两种情况下,您都没有覆盖默认的插值方法。 OpenCV默认使用线性插值,PIL默认使用Hamming。

标签: python arrays image opencv keras


【解决方案1】:

首先,有一点要记住,opencv 将图像读取为bgr,而其他库通常将图像读取为rgb。但是,这可能不是您在打印索引 1 的通道值时看到的差异背后的原因,在这两种情况下都是绿色通道。

我能想到的另一个原因是您对图像应用的调整大小操作,请尝试在调整大小之前比较值以消除这种可能性。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-07-11
    • 2022-11-04
    • 2017-12-31
    • 1970-01-01
    • 2021-12-17
    • 1970-01-01
    • 1970-01-01
    • 2021-08-08
    相关资源
    最近更新 更多