Last active
June 1, 2020 03:56
-
-
Save charliememory/34bc5a9a89c8eb849baae27edc90731e to your computer and use it in GitHub Desktop.
tensorflow implement of bounding box to mask transform
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## 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