Converting an image from colour to grayscale using OpenCV (2024)

Table of Contents
Algorithm Example Code Output FAQs

In this program, we will change the color scheme of an image from rgb to grayscale

Algorithm

Step 1: Import OpenCV.Step 2: Read the original image using imread().Step 3: Convert to grayscale using cv2.cvtcolor() function.

Example Code

import cv2image = cv2.imread('colourful.jpg')cv2.imshow('Original',image)grayscale = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)cv2.imshow('Grayscale', grayscale)

Output

Original Image:

Converting an image from colour to grayscale using OpenCV (30)

Grayscale Image:

Converting an image from colour to grayscale using OpenCV (31)

Updated on: 17-Mar-2021

14K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started

Converting an image from colour to grayscale using OpenCV (33)

Advertisem*nts

';adpushup.triggerAd(ad_id); });

Converting an image from colour to grayscale using OpenCV (2024)

FAQs

How to convert color image to grayscale in OpenCV? ›

To convert an RGB image to grayscale using OpenCV and Python, you can use the cvtColor() function from the OpenCV library. Here is an example code that reads an RGB image, converts it to grayscale, and then displays the grayscale image: pythonCopy codeimport cv2 # Read the RGB image img_rgb = cv2.

How do I convert a color image to grayscale? ›

How to grayscale an image
  1. Launch Canva. Head to Canva on your browser, or download the free mobile app on your phone or tablet. ...
  2. Upload your picture. Navigate the Uploads tab and select your image. ...
  3. Convert to grayscale. On the menu bar, click on the Edit image button. ...
  4. Add finishing touches. ...
  5. Save and use your grayscale image.

How do I change the color of my OpenCV to gray? ›

Converting Color video to grayscale using OpenCV in Python
  1. Import the cv2 module.
  2. Read the video file to be converted using the cv2. VideoCapture() method.
  3. Run an infinite loop.
  4. Inside the loop extract the frames of the video using the read() method.
  5. Pass the frame to the cv2. ...
  6. Display the frame using the cv2.
Jan 3, 2023

How do I show an image in grayscale in OpenCV? ›

Method 1: Using the cv2.

Import the OpenCV and read the original image using imread() than convert to grayscale using cv2. cvtcolor() function. destroyAllWindows() function allows users to destroy or close all windows at any time after exiting the script.

How do I convert cv2 PNG to grayscale? ›

Method 1: Using imread() function

So to convert the color image to grayscale we will be usingcv2. imread("image-name. png",0)or you can also writecv2. IMREAD_GRAYSCALEin the place of 0 as it also denotes the same constant.

What is the quickest way to convert a color image to black and white grayscale non destructively? ›

  1. Open the image in Adobe Photoshop.
  2. Duplicate the image layer by pressing Ctrl/Cmd+J.
  3. Go to Image > Adjustments > Desaturate to convert the image to grayscale.
  4. Go to Select > Subject to select the text in the image.
  5. Go to Select > Inverse to invert the selection.
  6. Press Delete to remove the background of the image.
Oct 30, 2023

What command converts a color image to grayscale values? ›

The rgb2gray function converts RGB images to grayscale by eliminating the hue and saturation information while retaining the luminance. If you have Parallel Computing Toolbox™ installed, rgb2gray can perform this conversion on a GPU.

How do you convert a color image to black and white in Python? ›

Color() method to get Grayscale images. You can also simply apply the . Color() method with a value of 0 which will turn down all the RGB colors and turn in a grayscale image. The results you get with this method will be visually identical to converting the image to L or LA modes.

How do I save an image as grayscale in cv2? ›

Write ndarray as an image file with cv2.

If 2D ndarray of row(height) x column(width) is specified as the argument of cv2. imwrite() , it is saved as a grayscale image file. If you want to save a color image (3D ndarray ) as a grayscale image file, convert it to grayscale with cv2. cvtColor() and cv2.

How do I change the color of an image in OpenCV? ›

Changing Color-space

For color conversion, we use the function cv. cvtColor(input_image, flag) where flag determines the type of conversion. For HSV, hue range is [0,179], saturation range is [0,255], and value range is [0,255]. Different software use different scales.

Why do we convert images to grayscale? ›

It helps in simplifying algorithms and as well eliminates the complexities related to computational requirements. It makes room for easier learning for those who are new to image processing. This is because grayscale compressors an image to its barest minimum pixel. It enhances easy visualisation.

How to show image using OpenCV Python? ›

GETTING STARTED (HOW TO READ IMAGES)
  1. Open PyCharm.
  2. Import cv2.
  3. Paste a test image in the directory.
  4. Create variable to store image using imread() function.
  5. Display the image using imshow() function.
  6. Add a delay using a waitkey() function.
Dec 11, 2020

How do you save an image as grayscale in Python cv2? ›

Write ndarray as an image file with cv2.

If 2D ndarray of row(height) x column(width) is specified as the argument of cv2. imwrite() , it is saved as a grayscale image file. If you want to save a color image (3D ndarray ) as a grayscale image file, convert it to grayscale with cv2. cvtColor() and cv2.

Top Articles
Latest Posts
Article information

Author: Terence Hammes MD

Last Updated:

Views: 6348

Rating: 4.9 / 5 (49 voted)

Reviews: 80% of readers found this page helpful

Author information

Name: Terence Hammes MD

Birthday: 1992-04-11

Address: Suite 408 9446 Mercy Mews, West Roxie, CT 04904

Phone: +50312511349175

Job: Product Consulting Liaison

Hobby: Jogging, Motor sports, Nordic skating, Jigsaw puzzles, Bird watching, Nordic skating, Sculpting

Introduction: My name is Terence Hammes MD, I am a inexpensive, energetic, jolly, faithful, cheerful, proud, rich person who loves writing and wants to share my knowledge and understanding with you.