我使用PIL.Image.open和tf.image.decode_jpeg将图像文件解析为数组。
但是发现PIL.Image.open()中的像素值与tf.image.decode_jpeg不同。
为什么会这样?
谢谢 !
代码输出:
tf 100 100 [132 145 161]
pil 100 100 [134 147 164]
我的代码:
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时,tensorflow会尝试采用快捷方式。这提供了用于图像读取的pretty large speedup,这可能是训练某些CNN的瓶颈,但会有点抖动像素值。
幸运的是,开发人员已经公开了关闭其中一些效率的选项。特别要检查argument dct_method
。
尝试将对tf.image.decode_jpeg
的呼叫更改为:
tf.image.decode_jpeg(value, channels=3, dct_method='INTEGER_ACCURATE')
您可能还需要弄乱fancy_upscaling
,具体取决于您正在读取的图像种类以及软件正在使用的libjpeg底层版本中发生的其他情况。
Tensorflow有大量的变换,可以应用于表示图像([高度,宽度,深度])(例如tf.image.rot90()或tf.image.random_flip_left_right())的3D张量。我知道它们应与队列一起使用,因此它们只能在一个图像上运行。但是,是否有一种方法可以对操作进行矢量化处理,以将4D张量([batch_size,height,widt…
将numpy数组图像转换为与request.get相同的格式 - python我有一个http端点,希望我以这种格式发送图像:url = 'https://example_image_url.jpg' img_bytes = requests.get(url).content endpoint.predict(img_bytes) 如果我有一个numpy数组格式的图像,如何将其转换为与上述img_bytes格式相同…
尝试将Python与photos.capture_image()结合使用以进行Kairos注册API - python我目前正在使用Kairos API,并试图使用Pythonista在我的iPad上拍摄一张新照片,然后将该照片上传到Kairos enroll API。我能够使它与URL图像一起正常工作,但是我一生无法通过使用photos模块拍摄照片来使其正常工作。根据我的理解,photos模块返回一个PIL图像,我认为在上传到Kairos API之前需要对它进行base6…
如何将枕头EPS调整为JPG品质 - python我正在尝试使用Pillow将EPS图像转换为JPEG。但是结果质量很差。我正在尝试使用resize方法,但是它被完全忽略了。我将JPEG图像的大小设置为(3600, 4700),但是结果图像的大小为(360, 470)。我的代码是:eps_image = Image.open('img.eps') height = eps_image.h…
用圆和线计算变换 - python我试图确定两个图像之间的转换(旋转+平移+缩放),以使其移动。图像是用两种不同的方式拍摄的,这些方式会产生非常不同的纹理。所以我不能使用基于维持光流的技术。我认为最好将图像阈值化以提取几何形状(请参见下面的示例)。但是然后我很难看到我能做些什么...也许可以提取垂直线和中心圆来帮助我提取变换。我在python中工作,我研究了Opencv可以提供的功能,但目前…