Skip to content

Instantly share code, notes, and snippets.

@MalteGruber
Last active May 11, 2020 18:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MalteGruber/272b1e43d342a8df2d40f8ebe5488674 to your computer and use it in GitHub Desktop.
Save MalteGruber/272b1e43d342a8df2d40f8ebe5488674 to your computer and use it in GitHub Desktop.
Helper code to untangle tf.image.crop_and_resize when performing a simple crop. Not optimized for speed.
"""
Helper code to untangle tf.image.crop_and_resize when performing a simple crop. Not optimized for speed.
Arguments:
tf_img -- Input tensorflow image
output_size -- Output size in pixels (w,h)
crop_yx -- Top left coordinate of crop, (y,x)
crop_size -- Size of area to include in crop (w,h)
Returns:
-- The cropped tensorflow image
"""
def crop_image(tf_img,output_size,crop_yx,crop_size):
#tf.image.crop_and_resize's boxes argument
#requires inputs in range [0.,1.], hence we normalize
img_size=tf_img.shape[0:2]+tf_img.shape[0:2]
a=tf.constant(2*crop_yx)
b=tf.constant([0,0]+crop_size)
c=tf.add(a,b)
boxes=[tf.divide(c,img_size)]
crop = tf.image.crop_and_resize([tf_img],boxes,[0],output_size)
return tf.cast(crop, tf.uint8)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment