tf.image.crop_and_resize() returns 损坏的裁剪图像
tf.image.crop_and_resize() returns broken cropped image
我正在尝试使用 TensorFlow API 实现的 Faster R-CNN 的边界框来捕获裁剪后的图像。 (特别是,我从 tensorflow 关注并定制了 this tutorial)
我按照上面的教程编写的代码如下:
for image_path in TEST_IMAGE_PATHS[0:1]:
image = Image.open(image_path)
image_np = load_image_into_numpy_array(image)
image_np_expanded = np.expand_dims(image_np, axis=0)
(_image_tensor,_boxes, scores, classes, num) = sess.run(
[image_tensor,detection_boxes, detection_scores, detection_classes, num_detections],
feed_dict={image_tensor: image_np_expanded})
test = tf.image.crop_and_resize(image=image_np_expanded,
boxes=[[0.27640104,0.2573258,0.57859987,0.7340185]],
box_ind=[0],
crop_size=[50,50])
plt.figure()
plt.imshow(image_np)
plt.figure()
plt.imshow(test[0].eval())
上面的代码执行后,结果如下图:
如您所见,裁剪后的第二张图片损坏了。边界框的值是变量'_boxes'的第一个值,即"_boxes[0]"
有什么我遗漏的吗?我被这个问题困住了。
似乎 tf.image.crop_and_resize
需要 [0,1] 范围内的像素值。将代码更改为
test = tf.image.crop_and_resize(image=image_np_expanded/255., ...)
帮我解决了问题。
TensorFlow 提供 tf.image.convert_image_dtype 用于 int 和 float 之间的转换。它在转换回 int 时特别有用(并使用 saturate=True
)。但我建议在任一方向使用它。
我正在尝试使用 TensorFlow API 实现的 Faster R-CNN 的边界框来捕获裁剪后的图像。 (特别是,我从 tensorflow 关注并定制了 this tutorial)
我按照上面的教程编写的代码如下:
for image_path in TEST_IMAGE_PATHS[0:1]:
image = Image.open(image_path)
image_np = load_image_into_numpy_array(image)
image_np_expanded = np.expand_dims(image_np, axis=0)
(_image_tensor,_boxes, scores, classes, num) = sess.run(
[image_tensor,detection_boxes, detection_scores, detection_classes, num_detections],
feed_dict={image_tensor: image_np_expanded})
test = tf.image.crop_and_resize(image=image_np_expanded,
boxes=[[0.27640104,0.2573258,0.57859987,0.7340185]],
box_ind=[0],
crop_size=[50,50])
plt.figure()
plt.imshow(image_np)
plt.figure()
plt.imshow(test[0].eval())
上面的代码执行后,结果如下图:
如您所见,裁剪后的第二张图片损坏了。边界框的值是变量'_boxes'的第一个值,即"_boxes[0]"
有什么我遗漏的吗?我被这个问题困住了。
似乎 tf.image.crop_and_resize
需要 [0,1] 范围内的像素值。将代码更改为
test = tf.image.crop_and_resize(image=image_np_expanded/255., ...)
帮我解决了问题。
TensorFlow 提供 tf.image.convert_image_dtype 用于 int 和 float 之间的转换。它在转换回 int 时特别有用(并使用 saturate=True
)。但我建议在任一方向使用它。