import tensorflow as tf
import matplotlib.pyplot as plt
image_bytes = tf.gfile.FastGFile("dog.jpg", 'rb').read() # 字节
with tf.Session() as session:
img = tf.image.decode_jpeg(image_bytes)
# 调整图片大小
img_resize = tf.image.resize_image_with_crop_or_pad(img, 300, 300)
# 按比例截取图片
boxes = tf.constant([[[0.31, 0.22, 0.46, 0.38], [0.38, 0.53, 0.53, 0.71]]]) # 两个标注框
# boxes = tf.constant([[[0.31, 0.22, 0.46, 0.38]]]) # 设置一个RGB,设置四个角的比例位置
# 给原始图片添加一个图层, #因为draw_bounding_boxes输入为一个batch的很多图片所以为四维矩阵,
#这里需要加一维
batched = tf.expand_dims(tf.image.convert_image_dtype(img_resize, tf.float32), 0)
# 把boxes标注的框画到原始图片上
image_with_boxes = tf.image.draw_bounding_boxes(batched, boxes)
# 重新将原始图片设置为RGB
image_with_boxes = tf.reshape(image_with_boxes, [300, 300, 3])
img_array = image_with_boxes.eval()
plt.imshow(img_array)
plt.show()
image_with_boxes函数是用来重新将四维图片变为三维的,300*300是图像切割后的值,用原图像的话就加入print(sess.run(img_data).shape)
代码来寻找。