我正在尝试保存带有显示边界框的图像,只是为了测试我的注释文件是否正常工作。
一切都很好:图像写入磁盘,边界框位于正确的位置,依此类推。除了所有颜色都是倒置的。所以它看起来像原始图像的负片。
这是我的代码:
```
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 rotate image colors (BGR -> RGB)的其他建议,但没有运气;这些方法能够改变颜色,但不能改变正确的颜色。
https://imgur.com/a/cclKJ显示顶部的原始图像(没有边界框),以及下面的结果图像(带有颜色问题,以及边界框)。
答案 0 :(得分:0)
我使用saturate_cast()
解决了这个问题。看来uint8 - &gt; float32或float32 - &gt; 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提出的建议,就像我在测试固定代码一样。