PIL.Image.open和tf.image.decode_jpeg返回值的区别

2024-05-16

我使用 PIL.Image.open 和 tf.image.decode_jpeg 将图像文件解析为数组。 但发现PIL.Image.open()中的像素值与tf.image.decode_jpeg不一样。 为什么会出现这种情况?

Thanks !

代码输出:

tf 100 100 [132 145 161]
pil 100 100 [134 147 164]

MyCode:

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

from datetime import datetime
import math
import time

import numpy as np
import tensorflow as tf

def decode_jpeg(image_file):
  from PIL import Image
  im = Image.open(image_file)
  data = np.array(im)
  return data

def tfimageread(filenames):
  filename_queue = tf.train.string_input_producer(filenames)
  reader = tf.WholeFileReader(name='image_reader')
  key, value = reader.read(filename_queue)
  uint8image = tf.image.decode_jpeg(value, channels=3)

  with tf.Session() as sess:
    coord = tf.train.Coordinator()
    threads = []
    for qr in tf.get_collection(tf.GraphKeys.QUEUE_RUNNERS):
      threads.extend(qr.create_threads(sess, coord=coord, daemon=True, start=True))
    image = sess.run(uint8image)
    coord.request_stop()
    coord.join(threads, stop_grace_period_secs=10)
    return image

if __name__ == '__main__':
  image_file = '。/o_1bchv9keu625336862107241874241888.jpg'
  image_tf = tfimageread([image_file])
  image_pil = decode_jpeg(image_file)
  i, j = 100, 100
  print ("tf %d %d %s" % (i,j,image_tf[i][j]))
  print ("pil %d %d %s" % (i,j,image_pil[i][j]))

造成此问题的一个常见原因是张量流在解压缩 jpeg 时尝试走捷径。这提供了一个相当大的加速 https://github.com/tensorflow/tensorflow/issues/4807#issuecomment-256718844对于图像读取,这可能是训练某些 CNN 的瓶颈,但确实会稍微抖动像素值。

幸运的是,开发人员已经提供了关闭其中一些效率的选项。特别是,请查看argument https://www.tensorflow.org/api_docs/python/tf/io/decode_jpeg#args dct_method.

尝试将您的通话更改为tf.image.decode_jpeg to:

tf.image.decode_jpeg(value, channels=3, dct_method='INTEGER_ACCURATE')

你可能还需要搞乱fancy_upscaling取决于您正在阅读的图像类型以及您的软件正在使用的 libjpeg 底层版本中发生的其他事情。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

PIL.Image.open和tf.image.decode_jpeg返回值的区别 的相关文章

随机推荐