Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check the channel order of an image?

Question

With an image loaded into Python as shown below, how do I know which order the channels are in? (e.g. BGR or RGB)

Code

from PIL import Image
import numpy as np

image_pil = Image.open("Stonehenge.jpg")
image_np = np.array(image_pil)
image_np[0][0]

Result

array([ 52, 123, 155], dtype=uint8)

Specific question

How do I know whether the 52 corresponds to the red channel, the blue channel, or a different channel? Or does this question not make sense on a conceptual level?


Notes

In a similar question for Java instead of Python, one person claims:

If you are reading in the image file, or you have access to the code that reads in the file, know it is:

  • BGR order if you used cv2.imread(),
  • RGB order if you used mpimg.imread(), (assuming import matplotlib.image as mpimg)

If you don't know how the file was opened, the accepted answer BufferedImage is great for Java.

like image 592
Matt Kleinsmith Avatar asked Oct 23 '17 21:10

Matt Kleinsmith


People also ask

What are the 4 channels in an image?

A CMYK image has four channels: cyan, magenta, yellow, and key (black). CMYK is the standard for print, where subtractive coloring is used. A 32-bit CMYK image (the industry standard as of 2005) is made of four 8-bit channels, one for cyan, one for magenta, one for yellow, and one for key color (typically is black).

What is the channel of an image?

In the context of a digital image, a channel is simply an array of values, one per pixel, that together specify one aspect or dimension of the image. For example, you can describe an image by specifying the red, green, and blue values for each of the pixels in the image. This is known as RGB (for red, green, blue).

How can I tell how many channels a photo has?

Python - OpenCV & PyQT5 together To get the number of the channel, we have used a class of OpenCV named 'channels()'. When we pass the image matrix as an object of the class 'channels()', it gives the channel an integer value. The following program counts the number of the channels and show it in the console window.

What is the order of channels in OpenCV?

There are three channels in an RGB image- red, green and blue. The color space where red, green and blue channels represent images is called RGB color space. In OpenCV, BGR sequence is used instead of RGB. This means the first channel is blue, the second channel is green, and the third channel is red.


1 Answers

Since you use PIL and you don't specify any other mode to load the Image with, you get R G B.

You could verify that by checking the "mode" attribute on the Image instance:

image_pil.mode   # should return the string 'RGB'

Pillow supports the array interface, via image_pil.__array_interface__ magic method, so when when you create the ndarray numpy just uses that. i.e., it doesn't know anything about the colour channel order. If you have an image file stored as BGR, and you load it like this, you will get blue data in the red channel and vice-versa, and it would look wrong when you display it.

like image 131
wim Avatar answered Oct 24 '22 08:10

wim