python - 为什么使用 tf.image.sample_distorted_bounding_box 的边界框和裁剪的图片不匹配?
问题描述
我想得到一个边界框和根据边界框裁剪的图片,所以我用了tf.image.sample_distorted_bounding_box
。但我失败了,我做错了什么?我的结果看起来像
边界框与根据边界框截取的图片不匹配。
我的代码:
with tf.Session() as sess:
boxes = tf.constant([[[0.05, 0.05, 0.9, 0.7], [0.35, 0.47, 0.5, 0.56]]])
image_float = tf.image.convert_image_dtype(img_data, tf.float32) # uint8 -> float
# resize image
image_small = tf.image.resize_images(image_float, [180, 267], method=0)
# Generate a single distorted bounding box.
begin, size, bbox_for_draw = tf.image.sample_distorted_bounding_box(
tf.shape(image_small),
bounding_boxes=boxes,
min_object_covered=0.1)
# Draw the bounding box in an image summary.
image_with_box = tf.image.draw_bounding_boxes(tf.expand_dims(image_small, 0),
bbox_for_draw)
tf.summary.image('images_with_box', image_with_box)
# Employ the bounding box to distort the image.
distorted_image = tf.slice(image_small, begin, size)
plt.figure(figsize = (30, 20))
plt.subplot(3, 1, 1)
plt.title("image with a random box")
plt.imshow(image_with_box[0].eval())
plt.subplot(3, 1, 2)
plt.title("destorted image")
plt.imshow(distorted_image.eval())
plt.show()
解决方案
这是因为每次调用都会.eval()
触发图形的新运行,因此会产生一个新的随机边界框。
要获得一致的输出,您需要同时运行运算符,例如
res = sess.run([image_with_box[0], distorted_image])
推荐阅读
- react-native - 专注于 TextInput 上的焦点反应导航
- maven - 如何在工件中维护所需数量的 Maven 存储库版本?
- sql - 当我尝试使用“和”从表(来自子查询的表)中获取两列时,出现错误“AND 的参数必须是布尔类型,而不是整数类型”
- vue.js - 如何使用 Vue 实时加载?
- javascript - 在时间数组中将小时与分钟分开
- cmake - 如何在虚拟环境中使用 cmake
- python - 在两对中的列中查找最大值-pandas python
- angular - 在 Azure 上部署后 Angular 10 路由不起作用(错误 404)
- reactjs - 通过用户输入更改查询参数(反应)
- xcode - 从 Bit Bucket 克隆和设置存储库到 XCode