-1
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import numpy as np

my_image = mpimg.imread('mountain.png')
print('The image is:- ',type(image), 
         'dimensions is:-', image.shape)

print(image[:,:,0])
print(image[:,:,1])
print(image[:,:,2])

I am unable to understand what image[:,:,0] or image[:,:,1] mean ?

Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555
Tanay
  • 167
  • 1
  • 2
  • 7

2 Answers2

1

A colour rgb image is read as a three dimensional array. The first two dimensions are x and y, and the third dimension is colour in the order red, green, blue.

The bracket notation is used to refer to subsets of this three dimensional array in the form [x, y, c]. A colon indicates that all values in that dimension should be selected.

Therefore image[:,:,0] refers to the red channel, image[:,:,1] to the blue channel and image[:,:,2] is the green channel.

D Greenwood
  • 415
  • 2
  • 11
1

Most image representations work with a bitmap with an RGB color space. An image is seen as a rectangle of pixels, and we assign a specific color to every pixel. A color is then represented as 3-tuple: where the first item of the tuple represents the intensity of red, the second one the intensity of green, and the last one the intensity of blue. An important note is that this is a representation of an image: there are other ones. Like for instance using vector graphics. Furthermore there are other color-spaces as well.

This thus means that if we load an image into memory, we obtain a matrix with shape (h, w, 3) with h the height of the image (in pixels), and w the width of the image (again in pixels).

Now numpy allows advanced indexing: we can construct a view by using image[:,:,0]. This means that we construct a (h, w)-shaped matrix, where for an item at index [i, j], we obtain the value that is placed at [i, j, 0] in the original image. We thus obtain an image, that only takes the intensity of the red channel into account.

The same holds for image[:,:,1] and image[:,:,2] where we take respecively the green and blue channel into account. The representation uses floats where 1.0 means maximum intensity, and 0.0 means lowest intensity. For instance if (red, green, blue) = (1.0, 0.5, 0.0), this is a color that most people see as yellow.

Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555