简体   繁体   English

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

[英]Difference between the return values of PIL.Image.open and tf.image.decode_jpeg

I use PIL.Image.open and tf.image.decode_jpeg to parse image files to arrays.我使用 PIL.Image.open 和 tf.image.decode_jpeg 将图像文件解析为数组。 But found that the pixel values in PIL.Image.open() are not the same as tf.image.decode_jpeg.但是发现PIL.Image.open()中的像素值和tf.image.decode_jpeg不一样。 Why this happens?为什么会发生这种情况?

Thanks !谢谢!

CodeOutput:代码输出:

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]))

A common cause of this problem is that tensorflow attempts to take shortcuts when decompressing jpegs.这个问题的一个常见原因是tensorflow在解压jpegs时试图走捷径。 This offers a pretty large speedup for image reading, which can be the bottleneck for training certain CNNs, but does jitter the pixel values a bit.这为图像读取提供了相当大的加速,这可能是训练某些 CNN 的瓶颈,但确实会稍微抖动像素值。

Luckily, the developers have exposed options to turn off some of these efficiencies.幸运的是,开发人员已经公开了关闭其中一些效率的选项。 In particular, check out the argument dct_method .特别是,请查看参数dct_method

Try changing your call to tf.image.decode_jpeg to:尝试将您对tf.image.decode_jpeg的调用tf.image.decode_jpeg为:

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

You may also need to mess with fancy_upscaling depending on the sorts of images you're reading and other things going on in the underlying version of libjpeg that your software is using.您可能还需要根据您正在阅读的图像类型以及您的软件正在使用的 libjpeg 的底层版本中发生的其他事情来搞乱fancy_upscaling

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM