【问题标题】:How to replicate tensorflow image processing using opencv python如何使用opencv python复制张量流图像处理
【发布时间】:2017-09-25 11:18:52
【问题描述】:

您好,我需要使用 python cv2 在下面的代码 sn-p 中执行与 tensorflow 完全相同的预处理。请帮忙!

file_name = 'image path'
input_name = 'file_reader'
input_height=299
input_width=299
input_mean=0
input_std=255

file_reader = tf.read_file(file_name, input_name)
image_reader = tf.image.decode_jpeg(file_reader, channels = 3,
                                    name='jpeg_reader')
float_caster = tf.cast(image_reader, tf.float32)
dims_expander = tf.expand_dims(float_caster, 0);
resized = tf.image.resize_bilinear(dims_expander, [input_height, input_width])
normalized = tf.divide(tf.subtract(resized, [input_mean]), [input_std])
sess = tf.Session()
result = sess.run(normalized)

【问题讨论】:

    标签: python opencv tensorflow


    【解决方案1】:

    我要为你的 tensorflow 代码逐行编写 OpenCV 代码:

    file_name = 'image path'
    input_name = 'file_reader'
    input_height=299
    input_width=299
    input_mean=0
    input_std=255
    
    #file_reader = tf.read_file(file_name, input_name)
    #image_reader = tf.image.decode_jpeg(file_reader, channels = 3,name='jpeg_reader')                              
    image = cv2.imread(file_name, -1)
    
    
    #float_caster = tf.cast(image_reader, tf.float32)
    float_caster = image.astype(numpy.float32, copy=False)
    
    
    #dims_expander = tf.expand_dims(float_caster, 0);
    #This line just adds another dimension to the image, which is not needed for OpenCV but if you want:
    #dims_expander = numpy.expand_dims(float_caster, axis=0)
    
    
    #resized = tf.image.resize_bilinear(dims_expander, [input_height, input_width])
    resized = cv2.resize(float_caster, (input_height,input_width),interpolation=cv2.INTER_LINEAR)
    
    #normalized = tf.divide(tf.subtract(resized, [input_mean]), [input_std])
    normalized = resized - input_mean
    normalized /= input_std
    

    请记住,OpenCV 将图像读取为BGR,而 tensorflow 将图像读取为RGB。所以如果你想直观地比较结果,你需要将一个转换为另一个的顺序。

    【讨论】:

    • 你能帮我转换下面的 img_contents = tf.read_file(input_queue[0]) img = tf.image.decode_jpeg(img_contents, channels=3) img_r, img_g, img_b = tf.split (value=img, num_or_size_splits=3, axis=2) img = tf.cast(tf.concat([img_b, img_g, img_r], 2), dtype=tf.float32) # 提取均值。 img -= IMG_MEAN
    【解决方案2】:

    tf.image.resize_bilinear 是一个有问题的函数。因此,在 TF 和 CV2 中调整相同图像的大小后,您将不会得到完全相同的值。

    快速示例:

    img_original = np.random.randint(0, 255, (4,4,3)).astype(np.uint8)
    img_rescaled = cv2.resize(img_original, (3,3), cv2.INTER_LINEAR)
    print(img_original)
    
    array([[[144,   9, 253],
        [  5,  87,   5],
        [ 21, 125, 117],
        [109, 159, 142]],
    
       [[ 64, 124, 196],
        [ 43, 230,  80],
        [ 42, 166,  36],
        [158, 121,  11]],
    
       [[238, 234,  57],
        [ 86, 254, 239],
        [149, 133, 161],
        [ 96, 245,  99]],
    
       [[128,   7, 134],
        [169,  69,  70],
        [246,  31,  95],
        [143,   1,  58]]], dtype=uint8)
    

    检查数组是如何通过 CV2 转换的:

    print(img_rescaled)
    
    array([[[111,  42, 206],
        [ 18, 121,  60],
        [102, 149, 117]],
    
       [[137, 189, 132],
        [ 80, 196, 129],
        [122, 177,  62]],
    
       [[148,  54, 117],
        [192,  74, 102],
        [151,  42,  71]]], dtype=uint8)
    

    现在,让我们定义 TF 会话并看看幕后发生了什么:

    x = tf.placeholder(tf.uint8, shape=(None,4,4,3), name='x')
    resize_op_TF = tf.image.resize_bilinear(x, (3,3), name='resize_op')
    session = tf.InteractiveSession()
    img_resized_TF = session.run(resize_op_TF, {x: [img_original]})[0]
    print(img_resized_TF)
    
    array([[[144.      ,   9.      , 253.      ],
        [ 10.333334,  99.66667 ,  42.333336],
        [ 79.66667 , 147.66667 , 133.66667 ]],
    
       [[122.00001 , 160.66667 , 149.66666 ],
        [ 64.111115, 210.33333 , 114.55556 ],
        [117.44445 , 159.88889 ,  52.77778 ]],
    
       [[164.66666 ,  82.66664 , 108.33334 ],
        [165.44446 , 108.777756, 123.22222 ],
        [156.11111 ,  76.55554 ,  86.77777 ]]], dtype=float32)
    

    如您所见,使用 TF 重新缩放的数组中的值和使用 CV2 重新缩放的数组中的值差别很大。这是 TensorFlow 中的常见问题,开发人员由于种种原因仍未解决。

    【讨论】:

      猜你喜欢
      • 2020-05-29
      • 1970-01-01
      • 1970-01-01
      • 2022-07-01
      • 2018-06-23
      • 2013-09-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多