为什么 tensorflow 解码 jpeg 图像与 scipy imread 不同?

编程入门 行业动态 更新时间:2024-10-06 06:51:08
本文介绍了为什么 tensorflow 解码 jpeg 图像与 scipy imread 不同?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

tf.image.decode_jpeg() 函数对 jpg 图像给出的数值结果与 scipy.misc.imread() 不同.虽然图像看起来相似,但像素值不同.

tf.image.decode_jpeg() function of Tensorflow gives different numerical result than scipy.misc.imread() for jpg images. While the images look similar, pixel values are different.

import numpy as np
import scipy
import tensorflow as tf
import matplotlib.pyplot as plt
def minimal_example():
    def _bytes_feature(value):
        return tf.train.Feature(bytes_list=tf.train.BytesList(value=[value]))

    tffilename = 'astronaut.tfrecords'
    writer = tf.python_io.TFRecordWriter(tffilename)
    #image_source = 'https://upload.wikimedia/wikipedia/commons/8/88/Astronaut-EVA.jpg'
    image_path = 'astronaut.jpg'
    image_file = open(image_path,'rb')
    image = image_file.read()
    image_scipy = scipy.misc.imread(image_path)
    example = tf.train.Example(features=tf.train.Features(feature={'image':_bytes_feature(image)}))
    writer.write(example.SerializeToString())
    writer.close()

    record_iterator = tf.python_io.tf_record_iterator(path=tffilename)
    example = tf.train.Example()
    example.ParseFromString(next(record_iterator))
    image = example.features.feature['image'].bytes_list.value[0]
    image_tf = tf.image.decode_jpeg(image).eval(session=tf.Session())
    fig = plt.figure()
    ax1 = fig.add_subplot(121)
    ax2 = fig.add_subplot(122)
    ax1.imshow(image_scipy)
    ax2.imshow(image_tf)
    print('Reconstruction Error', np.sum(np.abs(image_tf - image_scipy)))
    plt.show()

结果:

Reconstruction Error 3420883624

这是一个错误还是我做错了什么?

Is this a bug or am I doing something wrong?

推荐答案

出现这种差异是因为 Tensorflow 使用的默认离散余弦变换不准确但速度很快

The discrepancy arises because of inaccurate, but fast, default Discrete Cosine Tranform used by Tensorflow

根据 一个>

According to the Source code

//TensorFlow 选择的 jpeg 解码默认为 IFAST,牺牲

// The TensorFlow-chosen default for jpeg decoding is IFAST, sacrificing

//速度的图像质量.

flags_.dct_method = JDCT_IFAST;

flags_.dct_method = JDCT_IFAST;

为了得到准确的解码,可以设置属性dct_method = 'INTEGER_ACCURATE',如下例所示

In order to get accurate decoding one can set the attribute dct_method = 'INTEGER_ACCURATE' as show in example below

def minimal_example():
    #image_source = 'https://upload.wikimedia/wikipedia/commons/8/88/Astronaut-EVA.jpg'
    image_path = 'astronaut.jpg'
    image_file = open(image_path,'rb')
    image_raw = image_file.read()
    image_scipy = scipy.misc.imread(image_path)
    image_tf = tf.image.decode_jpeg(image_raw).eval(session=tf.Session())
    image_tf_accurate = tf.image.decode_jpeg(image_raw,dct_method="INTEGER_ACCURATE").eval(session=tf.Session())
    print('Error For Default: ', np.sum(np.abs(image_tf - image_scipy)))
    print('Error For Accurate: ', np.sum(np.abs(image_tf_accurate - image_scipy)))
    #Error For Default:  3420883624
    #Error For Accurate:  0

这篇关于为什么 tensorflow 解码 jpeg 图像与 scipy imread 不同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-05-01 01:07:41,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1402435.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:图像   jpeg   tensorflow   scipy   imread

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!