删除超出图像边缘的边界框

时间:2020-05-26 07:40:04

标签: python tensorflow

我在尝试删除超出图像边界的边界框时遇到了一些麻烦。

我能够使用 tf.clip_by_value 调整边界框,以使边界框不会超出边界。

x1 = tf.clip_by_value(boxes[:, :, 0], 0, width)
y1 = tf.clip_by_value(boxes[:, :, 1], 0, height)
x2 = tf.clip_by_value(boxes[:, :, 2], 0, width)
y2 = tf.clip_by_value(boxes[:, :, 3], 0, height)

但是我实际上正在尝试的是一种删除它们,而不是剪切它们的方法。 Tensorflow中是否有一个函数可能适合此问题?

谢谢!

1 个答案:

答案 0 :(得分:0)

以防将来有人遇到相同的问题:

top_indices = tf.where(tf.math.greater(boxes[:, 1], 0))
new_boxes = tf.gather(boxes, top_indices)[:, 0, :]
bottom_indices = tf.where(tf.math.less(new_boxes[:, 3], image_size))
final_boxes = tf.gather(new_boxes, bottom_indices)[:, 0, :]