Skip to content

Instantly share code, notes, and snippets.

@charliememory
Last active June 1, 2020 03:56
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save charliememory/34bc5a9a89c8eb849baae27edc90731e to your computer and use it in GitHub Desktop.
Save charliememory/34bc5a9a89c8eb849baae27edc90731e to your computer and use it in GitHub Desktop.
tensorflow implement of bounding box to mask transform
## ref: https://stackoverflow.com/questions/34128104/tensorflow-creating-mask-of-varied-lengths
def tf_bbox2mask(y1, x1, y2, x2, img_H, img_W):
## Repeat for each row or column
y1_transposed = tf.expand_dims(tf.tile(y1,[img_W]), 0)
x1_transposed = tf.expand_dims(tf.tile(x1,[img_H]), 1)
y2_transposed = tf.expand_dims(tf.tile(y2,[img_W]), 0)
x2_transposed = tf.expand_dims(tf.tile(x2,[img_H]), 1)
## Get the range grid
range_row = tf.cast(tf.expand_dims(tf.range(0, img_H, 1), 1), tf.int32)
range_col = tf.cast(tf.expand_dims(tf.range(0, img_W, 1), 0), tf.int32)
## Generate bollean masks
mask_y1 = tf.less(y1_transposed, range_row)
mask_x1 = tf.less(x1_transposed, range_col)
mask_y2 = tf.less(range_row, y2_transposed)
mask_x2 = tf.less(range_col, x2_transposed)
result = tf.to_float(mask_y1)*tf.to_float(mask_x1)*tf.to_float(mask_y2)*tf.to_float(mask_x2)
return result
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment