Tensorflow 和 skimage 之间的像素值差异

Pixel values difference between Tensorflow and skimage

为什么我用skimage.io.imread(image)tf.image.decode_image(image)打开一张图片,像素值会有差异?

例如:

import skimage.io
original_img = skimage.io.imread("test.jpg")
print(original_img.shape)
print(np.amax(original_img))
print(original_img)

输出为:

(110, 150, 3)
255
array([[[ 29,  65, 117],
        [ 45,  43,  90],
        [ 78,  39,  68],
        ...,
        [ 30,  46,  95],
        [ 30,  43,  96],
        [ 31,  44,  97]],

       [[ 41,  54,  89],
        [ 95,  89, 123],
        [ 57,  39,  65],
        ...,
        [ 32,  46,  91],
        [ 32,  46,  95],
        [ 32,  45,  97]],

       [[ 62,  49,  69],
        [ 84,  76,  97],
        [ 68,  70,  95],
        ...,
        [ 18,  30,  70],
        [ 35,  47,  95],
        [ 34,  47,  99]],

       ...,

       [[136, 124,  22],
        [144, 136,  53],
        [134, 123,  44],
        ...,
        [ 16,  74,  16],
        [ 39,  89,  52],
        [ 53, 108,  69]],

       [[161, 125,   5],
        [149, 129,  42],
        [129, 116,  48],
        ...,
        [ 67, 119,  73],
        [ 39,  80,  48],
        [ 33,  69,  41]],

       [[196, 127,   6],
        [160, 111,  32],
        [141, 108,  55],
        ...,
        [ 26,  56,  32],
        [  8,  29,  10],
        [ 12,  24,  12]]], dtype=uint8)

如果我用 Tensorflow 打开同一张图片:

import tensorflow as tf

original_img = tf.image.decode_image(tf.io.read_file("test.jpg"))
print(np.amax(original_img))
print(original_img)

输出为:

255
<tf.Tensor: shape=(110, 150, 3), dtype=uint8, numpy=
array([[[ 44,  57, 101],
        [ 40,  42,  80],
        [ 65,  41,  65],
        ...,
        [ 25,  42,  88],
        [ 33,  49, 100],
        [ 25,  41,  92]],

       [[ 47,  53,  89],
        [ 96,  95, 127],
        [ 60,  44,  70],
        ...,
        [ 29,  43,  88],
        [ 40,  54, 103],
        [ 19,  35,  84]],

       [[ 59,  54,  74],
        [ 72,  69,  90],
        [ 70,  70,  96],
        ...,
        [ 23,  35,  77],
        [ 16,  29,  74],
        [ 50,  64, 111]],

       ...,

       [[145, 116,  24],
        [161, 131,  43],
        [141, 113,  30],
        ...,
        [ 19,  67,  19],
        [ 49,  95,  58],
        [ 53,  97,  64]],

       [[164, 119,  16],
        [166, 123,  28],
        [143, 108,  27],
        ...,
        [ 73, 119,  80],
        [ 29,  68,  37],
        [ 39,  75,  47]],

       [[182, 128,  20],
        [160, 112,  14],
        [149, 112,  32],
        ...,
        [ 11,  57,  21],
        [  7,  44,  13],
        [  0,  14,   0]]], dtype=uint8)>

我还注意到,如果我用 tensorflow 打开图像,对该图像进行一些更改,将图像保存在磁盘上并再次用 tf.image.decode_image(image) 打开,像素值又是不一样,但这次不一样。

这是由于用于解压缩的算法。默认情况下,使用系统特定的方法。 tf.image.decode_image() 不提供任何更改方法的可能性。

tf.image.decode_jpeg()中有dct_method参数可以用来改变解压的方法。目前有两个有效值可以设置:INTEGER_FASTINTEGER_ACCURATE.

如果您按以下方式打开图像,您应该得到与 skimage.io.imread(image) 相同的输出:

original_img = tf.image.decode_jpeg(tf.io.read_file("test.jpg"),dct_method="INTEGER_ACCURATE")