首页 > 解决方案 > 为什么调整图像大小最终也会改变通道?

问题描述

所以我在 python 中有这个代码,它使用 TensorFlow 打开一个图像并调整它的大小:

def parse_image(filename):
    label = filename
    image = tf.io.read_file(path+'/'+filename)
    print(image.shape)
    image = tf.image.decode_png(image)
    image = tf.image.convert_image_dtype(image, tf.float32)
    image = tf.image.resize(image, [340, 340])
    print(image.shape)
    return image, label

当我使用 OpenCV 打开测试灰度图像时:

img = cv2.imread(path+"/test.png")
print(img.shape) #returns (2133,3219,3)

但是当我在同一张图片上调用上述函数时:

image, label = parse_image('test.png')

print(image.shape) #returns (340,340,1)

我知道 340 x 340 是我刚刚设置的宽度和高度,但为什么频道会改变?我正在尝试计算结构相似性,但测试图像和我要比较的图像具有不同的通道,这会引发错误。最糟糕的是,它是这个特定的测试图像。其他灰度图像工作正常。

标签: pythontensorflowopencvtensorflow2.0

解决方案


推荐阅读