Skip to content

Instantly share code, notes, and snippets.

@CMCDragonkai
Last active June 18, 2018 05:34
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 CMCDragonkai/cf25ad969e3a7fc1e48d60ecf20a2253 to your computer and use it in GitHub Desktop.
Save CMCDragonkai/cf25ad969e3a7fc1e48d60ecf20a2253 to your computer and use it in GitHub Desktop.
Resizing Images for Convolutional Neural Networks #python #numpy
import math
import skimage.transform
import numpy as np
# you use this just before passing any image to a CNN
# which usually expects square images
# however your input images can be of variable size
# you don't want to just squash the images to a square
# because you will lose valuable aspect ratio information
# you want to resize while preserving the aspect ratio
# these 2 functions perform this resizing behaviour
# images are assumed to be formatted as Height, Width, Channels
# we will use bound_image_dim to first bound the image to a range
# the smallest dimension will be scaled up to the min_size
# the largest dimension will be scaled down to the max_size
# then afterwards you square_pad_image to pad the image to a square
# filling the empty space with zeros
def bound_image_dim(image, min_size=None, max_size=None):
if (max_size is not None) and \
(min_size is not None) and \
(max_size < min_size):
raise ValueError('`max_size` must be >= to `min_size`')
dtype = image.dtype
(height, width, *_) = image.shape
# scale the same for both height and width for fixed aspect ratio resize
scale = 1
# bound the smallest dimension to the min_size
if min_size is not None:
image_min = min(height, width)
scale = max(1, min_size / image_min)
# next, bound the largest dimension to the max_size
# this must be done after bounding to the min_size
if max_size is not None:
image_max = max(height, width)
if round(image_max * scale) > max_size:
scale = max_size / image_max
if scale != 1:
image = skimage.transform.resize(
image, (round(height * scale), round(width * scale)),
order=1,
mode='constant',
preserve_range=True)
return image.astype(dtype)
def square_pad_image(image, size):
(height, width, *_) = image.shape
if (size < height) or (size < width):
raise ValueError('`size` must be >= to image height and image width')
pad_height = (size - height) / 2
pad_top = math.floor(pad_height)
pad_bot = math.ceil(pad_height)
pad_width = (size - width) / 2
pad_left = math.floor(pad_width)
pad_right = math.ceil(pad_width)
return np.pad(
image, ((pad_top, pad_bot), (pad_left, pad_right), (0, 0)),
mode='constant')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment