8.2 Image Layers (or Channels)

Lets see how the data in the image four-circles.jpg is stored. Then plot the original image along with the three RGB layers as shown below.

from matplotlib import pyplot as plt

img = plt.imread('../four-circles.jpg')
fig, ax = plt.subplots(nrows=1, ncols=4, figsize=(16, 4))

#------------ Display the original image ----------#
ax[0].imshow(img)
ax[0].set_title('Original (RGB)')

#------------------ Red channel ------------------- #
# ALL row & ALL columns of layer 0
ax[1].imshow(img[:, :, 0], cmap='gray')
ax[1].set_title('Red Channel(in Grayscale)')

#------------------ Green channel ----------------- #
# ALL row & ALL columns of layer 1
ax[2].imshow(img[:, :, 1], cmap='gray')
ax[2].set_title('Green Channel(in Grayscale)')

#------------------ Blue channel ------------------- #
# ALL row & ALL columns of layer 2
ax[3].imshow(img[:, :, 2], cmap='gray')
ax[3].set_title('Blue Channel(in Grayscale)')

for a in ax.flat:
    a.axis('off')

plt.tight_layout()
plt.savefig('09_image-processing-1_four-circles-layers.png', dpi=150)
plt.show()

Notice that:

  • For the red channel, we cannot see the green and blue circles. But, we can still see the yellow as you need some red (and green) to make yellow.
  • For the green channel, we cannot see the red and blue circles. But, we can still see the yellow as you need some green (and red) to make yellow.
  • For the blue channel, we cannot see the red and green circles. The yellow circle is also not visible. You don’t need blue to make yellow.

To extract information from images comfortably, we first need to be comfortable manipulating arrays and lists. So, lets first practice some array gymnastics before looking at some images from biology.