在tensorflow中将二进制掩码转换为边界框

在tensorflow中将二进制掩码转换为边界框,tensorflow,tensor,Tensorflow,Tensor,我有一个二进制掩码作为tensorflow中的张量 如何使用tensorflow操作将此二进制掩码转换为边界框?经过一点工作,我终于解决了它。请注意,给出的解决方案仅适用于单个对象,但是只要稍加调整,您也可以将其应用于多个对象 基本上,您希望检查整个轴上是否存在任何真实像素。从边缘开始,然后进一步向内移动,直到击中至少有一个真实像素的轴。对左侧、右侧、顶部和底部执行此操作 image = tf.io.read_file('mask.png') image = tf.io.decode_png(i

我有一个二进制掩码作为tensorflow中的张量


如何使用tensorflow操作将此二进制掩码转换为边界框?

经过一点工作,我终于解决了它。请注意,给出的解决方案仅适用于单个对象,但是只要稍加调整,您也可以将其应用于多个对象

基本上,您希望检查整个轴上是否存在任何真实像素。从边缘开始,然后进一步向内移动,直到击中至少有一个真实像素的轴。对左侧、右侧、顶部和底部执行此操作

image = tf.io.read_file('mask.png')
image = tf.io.decode_png(image)
image = tf.image.resize(image, size=(300, 300), method='nearest')

rows = tf.math.count_nonzero(image, axis=0, keepdims=None, dtype=tf.bool) # return true if any pixels in the given row is true
rows = tf.squeeze(rows, axis=1) #make a scalar

columns = tf.math.count_nonzero(image, axis=1, keepdims=None, dtype=tf.bool)
columns = tf.squeeze(columns, axis=1)

def indicies_by_value(value): return tf.where(tf.equal(value, True))[:,-1] #return all the indices where mask is present along given axis

#coordinates
y_min = indicies_by_value(columns)[0] #first true pixel along axis
y_max = indicies_by_value(columns)[-1] #last true pixel along axis
x_min = indicies_by_value(rows)[0]
x_max = indicies_by_value(rows)[-1]

#apply the bounding box
image = tf.image.convert_image_dtype(image, dtype=tf.float32)
img = tf.expand_dims(image, axis=-0)
img = tf.reshape(img, shape=[1, 300, 300, 1])

box = tf.stack([y_min, x_min, y_max, x_max], axis=0)
box = tf.math.divide(box, 300)
box = box.numpy()
boxes = box.reshape([1,1,4])

colors = np.array([[0.5, 0.9, 0.5], [0.5, 0.9, 0.5]])
boundning_box = tf.image.draw_bounding_boxes(img, boxes, colors)

tf.keras.preprocessing.image.save_img('boxed.png', boundning_box.numpy()[0])

经过一点努力,我终于解决了这个问题。请注意,给出的解决方案仅适用于单个对象,但是只要稍加调整,您也可以将其应用于多个对象

基本上,您希望检查整个轴上是否存在任何真实像素。从边缘开始,然后进一步向内移动,直到击中至少有一个真实像素的轴。对左侧、右侧、顶部和底部执行此操作

image = tf.io.read_file('mask.png')
image = tf.io.decode_png(image)
image = tf.image.resize(image, size=(300, 300), method='nearest')

rows = tf.math.count_nonzero(image, axis=0, keepdims=None, dtype=tf.bool) # return true if any pixels in the given row is true
rows = tf.squeeze(rows, axis=1) #make a scalar

columns = tf.math.count_nonzero(image, axis=1, keepdims=None, dtype=tf.bool)
columns = tf.squeeze(columns, axis=1)

def indicies_by_value(value): return tf.where(tf.equal(value, True))[:,-1] #return all the indices where mask is present along given axis

#coordinates
y_min = indicies_by_value(columns)[0] #first true pixel along axis
y_max = indicies_by_value(columns)[-1] #last true pixel along axis
x_min = indicies_by_value(rows)[0]
x_max = indicies_by_value(rows)[-1]

#apply the bounding box
image = tf.image.convert_image_dtype(image, dtype=tf.float32)
img = tf.expand_dims(image, axis=-0)
img = tf.reshape(img, shape=[1, 300, 300, 1])

box = tf.stack([y_min, x_min, y_max, x_max], axis=0)
box = tf.math.divide(box, 300)
box = box.numpy()
boxes = box.reshape([1,1,4])

colors = np.array([[0.5, 0.9, 0.5], [0.5, 0.9, 0.5]])
boundning_box = tf.image.draw_bounding_boxes(img, boxes, colors)

tf.keras.preprocessing.image.save_img('boxed.png', boundning_box.numpy()[0])

解决了吗?没有。我找不到一组可以模仿OpenCV的findContours的操作。解决了吗?没有。我找不到一组可以模仿OpenCV的findContours的操作。有趣的是,我提出了一个类似的解决方案,它输出一个边界框。我想知道是否有办法得到所有的边界框。有趣的是,我已经想出了一个类似的解决方案,输出一个边界框。我想知道是否有办法得到所有的边界框。