Created
October 10, 2017 13:15
-
-
Save yfe404/4126155fa67f0bb9bed88071c75105fd to your computer and use it in GitHub Desktop.
robo-matplotlib-numpy-image-rgb-viz
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
# Import some packages from matplotlib | |
import matplotlib.image as mpimg | |
import matplotlib.pyplot as plt | |
# Import the "numpy" package for working with arrays | |
import numpy as np | |
# Uncomment the next line for use in a Jupyter notebook | |
#%matplotlib inline | |
# Define the filename, read and plot the image | |
filename = 'sample.jpg' | |
image = mpimg.imread(filename) | |
# Note: we use the np.copy() function rather than just saying red_channel = image | |
# because in Python, such a statement would set those two arrays equal to each other | |
# forever, meaning any changes made to one would also be made to the other! | |
red_channel = np.copy(image) | |
# Note: here instead of extracting individual channels from the image | |
# I'll keep all 3 color channels in each case but set the ones I'm not interested | |
# in to zero. | |
red_channel[:,:,[1, 2]] = 0 # Zero out the green and blue channels | |
green_channel = np.copy(image) | |
green_channel[:,:,[0, 2]] = 0 # Zero out the red and blue channels | |
blue_channel = np.copy(image) | |
blue_channel[:,:,[0, 1]] = 0 # Zero out the red and green channels | |
fig = plt.figure(figsize=(12,3)) # Create a figure for plotting | |
plt.subplot(131) # Initialize subplot number 1 in a figure that is 3 columns 1 row | |
plt.imshow(red_channel) # Plot the red channel | |
plt.subplot(132) # Initialize subplot number 2 in a figure that is 3 columns 1 row | |
plt.imshow(green_channel) # Plot the green channel | |
plt.subplot(133) # Initialize subplot number 3 in a figure that is 3 columns 1 row | |
plt.imshow(blue_channel) # Plot the blue channel | |
plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment