Skip to content

Instantly share code, notes, and snippets.

@araffin
Created October 6, 2017 17:39
Show Gist options
  • Save araffin/6fca218bb6e61b305a352f50a97a8fec to your computer and use it in GitHub Desktop.
Save araffin/6fca218bb6e61b305a352f50a97a8fec to your computer and use it in GitHub Desktop.
Preprocess a RGB image to feed it to a neural net
def preprocessImage(image, width, height):
"""
Preprocessing script to convert image into neural net input array
:param image: (cv2 RBG image)
:param width: (int)
:param height: (int)
:return: (numpy array)
"""
image = cv2.resize(image, (width, height), interpolation=cv2.INTER_LINEAR)
x = image.flatten() # create a big 1D-array
# Normalize
x = x / 255. # values in [0, 1]
x -= 0.5 # values in [-0.5, 0.5]
x *= 2 # values in [-1, 1]
return x
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment