Cv2 Rgb To Gray

  1. Cv2 Rgb To Gray
  2. Cv2 Rgb To Grayscale
  3. Opencv Rgb To Gray

Contents

OpenCV cv2 imread()

Def channelconvert(inc, tartype, imglist): # conversion among BGR, gray and y if inc 3 and tartype 'gray': # BGR to gray graylist = cv2.cvtColor(img, cv2.COLORBGR2GRAY) for img in imglist return np.expanddims(img, axis=2) for img in graylist elif inc 3 and tartype 'y': # BGR to y ylist = bgr2ycbcr(img, onlyy=True. If you’re asking for a simple method the answer is no. Or what you’re asking is simply merging r,g,b channels together the answer is in the next section Let me explain Simply take an image containing an rainbow, it is very easy to a human to ident. Import cv2 img = cv2.imread('demo-image.jpg') grayimage = cv2.cvtColor(img,cv2.COLORBGR2GRAY) cv2.imwrite('opencv-greyscale.png',grayimage) Output. Convert image to grayscale in python using OpenCV module Conclusion. Greyscale Image conversion is a must before doing any further image processing.

You can read image into a numpy array using opencv library. The array contains pixel level data. And as per the requirement, you may modify the data of the image at a pixel level by updating the array values.

To read an image in Python using OpenCV, use cv2.imread() function. imread() returns a 2D or 3D matrix based on the number of color channels present in the image. For a binary or grey scale image, 2D array is sufficient. But for a colored image, you need 3D array.

In this tutorial, we shall learn in detail how to read an image using OpenCV, by considering some of the regular scenarios.

We will also learn the order in which imread() function decodes the color channels from an image and how imread() treats different image extensions.

Syntax of cv2.imread()

The syntax of cv2.imread() function is given below.

where /path/to/image has to be the complete absolute path to the image. The flag is optional and one of the following possible values can be passed for the flag.

  • cv2.IMREAD_COLOR reads the image with RGB colors but no transparency channel. This is the default value for the flag when no value is provided as the second argument for cv2.imread().
  • cv2.IMREAD_GRAYSCALE reads the image as grey image. If the source image is color image, grey value of each pixel is calculated by taking the average of color channels, and is read into the array.
  • cv2.IMREAD_UNCHANGED reads the image as is from the source. If the source image is an RGB, it loads the image into array with Red, Green and Blue channels. If the source image is ARGB, it loads the image with three color components along with the alpha or transparency channel.
Rgb

Example 1: OpenCV cv2 Read Color Image

In this example, we will read a color image. As the default value of the flag argument is cv2.IMREAD_COLOR, we are not passing the flag explicitly.

Cv2 Rgb To Gray

Python Program

Output

Rgb to gray python cv2

Run the above python program, and you shall get the following output.

img.shape returns tuple representing (height, width, number_of_channels). Height of the image is 400 pixels, width is 640 and there are three color channels in the image. For cv2.IMREAD_COLOR, transparency channel is ignored even if present.

Example 2: OpenCV cv2 – Read Image as Grey Scale

In this example, we will read image as a grey scale image. Input can be color image or grey scale image. But, if flag argument is cv2.IMREAD_GRAYSCALE, the image is read as grey scale image.

Python Program

Output

Height of the image is 400 pixels, width is 640. Each element in the array represents grey scale value at the respective pixel.

Example 3: OpenCV cv2 – Read Image with Transparency Channel

In this example, we will read an image with transparency channel. If there is a transparency channel in the image, then we can pass cv2.IMREAD_UNCHANGED to read the transparency channel along with the color channels.

Python Program

Output

We have read all the four channels of the image. Namely Red, Green, Blue and Transparency.

imread() and Color Channels

imread() decodes the image into a matrix with the color channels stored in the order of Blue, Green, Red and A (Transparency) respectively.

If (400, 640, 4) is the shape of the image, then

  • (:, :, 0) represents Blue channel
  • (:, :, 1) represents Green channel
  • (:, :, 2) represents Red channel
  • (:, :, 3) represents Transparency channel

imread() and File Extensions

Cv2 Rgb To Grayscale

There are many extensions used for images based on the operating system, compression technique, etc.

When imread() method reads image, it does not consider the extension of the image file name to determine the format of the image. But decides the extension based on the format present in the file data.

imread() supports JPEGs, PNGs, and TIFFs across all platforms. But for the combination of other formats and Operating Systems, imread() may consider the operating system level codecs. You may refer the official documentation of imread() for these special scenarios.

Summary

Opencv Rgb To Gray

Concluding this tutorial of Python Examples, we learned to use cv2 imread() method to read an image into a Python Array.

Related Tutorials