2019-07-12
OpenCV: Extracting the colour channels from an RGB image
stackoverflow
Question

I am trying to segment a colour image using Mean-Shift clustering using sklearn. I have read the image into a numpy array, however I want to extract each colour channel (R,G,B) so that I can use each as a variable for classification.

I have found the following code online, which extracts the RGB colour channels of an image which is represented as a numpy array.

  red = image[:,:,2]
  green = image[:,:,1]
  blue = image[:,:,0]

However, I don't understand how this code works. What do the 2, 1 and 0 mean? Any insights are appreciated.

Answer
1

A normal picture you will have 3 layer, Red Green and Blue.

When you read an picture by a tool (example is open-cv), it will return for you a numpy array with shape (width_image x length_image x channels).

The arrange of channels depend on what you used, if you read by open-cv it will be Blue is first then Green then Red, else if you use matplotlib.pyplot.imread it will be Red-Green-Blue.

That code wrote like that because they read picture by open-cv

OpenCV: Extracting the colour channels from an RGB image
See more ...