0
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 ?

4
  • 2
    It fetches the red, green and blue channels of the image separately. Commented Jan 7, 2018 at 14:43
  • 1
    Related: "[:,]" list slicing python, what does it mean? Commented Jan 7, 2018 at 14:51
  • The canonical 'understanding slice notation' post has several answers extending this to numpy arrays, so I duped it there. Commented Jan 7, 2018 at 14:53
  • This question shouldn't be marked as a duplicate since this involves images and color dimensions, which is more specific and has a different "meaning" than regular multidimensional arrays. Commented Feb 14 at 2:43

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

Comments

2

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.

1 Comment

Willem Van Onsem, thanks for the nice explanation

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.