簡體   English   中英

使用TensorFlow和PIL寫入圖像時顏色反轉

[英]Inverted Colours when Writing an Image using TensorFlow and PIL

我正在嘗試保存帶有邊框的圖像,以測試我的注釋文件是否正常工作。

一切正常:將圖像寫入磁盤,將邊界框放在正確的位置,依此類推。 除了所有顏色都是反轉的。 因此,它看起來像原始圖像的底片。

這是我的代碼:

```

import tensorflow as tf
import numpy as np
from PIL import Image

def read_processed_data(filename, num_show):
    """ Reads in the processed data file and displays the
        given number of images, along with the bounding boxes.
    """
    with open(filename, 'r') as f:
        i = 0

        while i < num_show:
            for line in f:
                filename = line.rstrip()
                next_line = f.readline()
                num_faces = int(next_line.rstrip())
                face_num = 0

                #while face_num < num_faces:
                bb_line = f.readline().rstrip()
                y1, x1, y2, x2 = bb_line.split(',')
                y1 = float(y1)
                x1 = float(x1)
                y2 = float(y2)
                x2 = float(x2)

                box = [y1, x1, y2, x2]

                return box, filename


with tf.Session() as sess:
    bb, fn = read_processed_data("processed.txt", 1)
    image = tf.image.decode_image(tf.read_file(fn))

    image_as_float = tf.cast(image, dtype = tf.float32)
    image_4d = tf.expand_dims(image_as_float, 0)

    bb_2d = tf.expand_dims(bb, 0)
    bb_3d = tf.expand_dims(bb_2d, 0) # Box has to be 3d for the drawing to work
    bb_image = tf.image.draw_bounding_boxes(image_4d, bb_3d)
    bb_image_uint = tf.image.convert_image_dtype(bb_image, dtype = tf.uint8)
    bb_image_uint_3d = tf.reshape(bb_image_uint, [940, 650, 3]) # Reduce rank from 4 to 3
    data = bb_image_uint_3d.eval()

    base_fn = fn.split('.')[0]
    Image.fromarray(data).save(base_fn + "_bb.jpg")

```

我沒有搜索過tensorflow文檔。 我也嘗試過np.roll()PIL的其他建議再次旋轉圖像顏色(BGR-> RGB) ,但是沒有運氣。 這些方法能夠更改顏色,但不能更改為正確的顏色。

https://imgur.com/a/cclKJ在頂部顯示原始圖像(無邊界框),在下面顯示結果圖像(有顏色問題和邊界框)。

我通過使用saturate_cast()解決了這個問題。 似乎uint8-> float32或float32-> uint8轉換(我懷疑是后者)導致溢出。

代碼的固定部分是

image_as_float = tf.saturate_cast(image, dtype = tf.float32)
image_4d = tf.expand_dims(image_as_float, 0)    # Add in a batch dimension (of size 1)

bb_2d = tf.expand_dims(bb, 0)   # Add in dimension of size 1 (num_bounding_boxes)
bb_3d = tf.expand_dims(bb_2d, 0)    # Add in dimension of size 1 (batch)
bb_image = tf.image.draw_bounding_boxes(image_4d, bb_3d)
bb_image_uint = tf.saturate_cast(bb_image, dtype = tf.uint8)

感謝de1的建議,就像我測試固定代碼一樣。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM