Created
October 6, 2017 17:39
-
-
Save araffin/6fca218bb6e61b305a352f50a97a8fec to your computer and use it in GitHub Desktop.
Preprocess a RGB image to feed it to a neural net
This file contains 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
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