How to Convert Three Channels of Colored Image into Grayscale Image in MATLAB?


In this article, we will explore how to convert a colored image with three channels, i.e. RGB (Red, Green, and Blue) into a gray scale image using MATLAB.

An RGB image is a digital image in which each pixel is represented as a combination of intensity of three−color channels, namely red, green, and blue. RGB image is mainly used display color images on display screens.

On the other hand, a gray scale image is one that uses only two colors, i.e. black and white to represents the elements in an image. In other words, a gray scale image is a digital image in which gray shades are used to represent the object in the image. These kinds of images are mainly used to display on monochromatic displays or printouts on paper.

How to Convert Colored Image into Grayscale Image in MATLAB

MATLAB is a powerful tool to process digital images. It provides a variety of built−in functions and tools to process digital images.

To convert the three−channels of a colored image into a gray scale image, there is a MATLAB function named, ‘rgb2gray’. This function takes the three−channel colored image as input and produces a grayscale image.

Syntax

gray_image = rgb2gray(rgb_image);

Algorithm

The step−by−step process to convert a three−channel colored image into a gray image is explain below:

Step (1) − Read the input RGB image.

Step (2) − Convert the input RGB image into gray scale image using the ‘rgb2gray’ function.

Step (3) − Display the output grayscale image.

The following MATLAB program demonstrates the use of the ‘rgb2gray’ function to convert a three−channel colored image into a gray scale image.

Example

% MATLAB Code for converting three-channel colored image into gray scale image
% Read the three-channel colored image
rgb_img = imread('https://www.tutorialspoint.com/assets/questions/media/14304-1687425236.jpg'); 

% Check whether the input image is in three-channel color format or not
if size(rgb_img, 3) == 3

% Convert the colored image to grayscale image
gray_img= rgb2gray(rgb_img);
else

% If the input image is already grayscale
disp('The input image is already a grayscale image.');
end

% Display the colored image and grayscale image
figure;
subplot(1, 2, 1); imshow(rgb_img); title('Three-Channel Colored Image');
subplot(1, 2, 2); imshow(gray_img); title('Grayscale Image');

Output

Code Explanation

This MATLAB program illustrates code implementation to convert a three−channel colored image into a gray scale image in MATLAB. In this code, we start by loading the input three−channel colored image by using the ‘imread’ function and store it in a variable ‘rgb_img’. Next, we check if the input image is a colored or grayscale image.

If it is a colored image, the we convert it into a gray scale image by using the ‘rgb2gray’ function. If the input image is already a grayscale image, then we display the message ‘The input image is already a grayscale image.’.

Finally, we display the input−colored image and the output grayscale image side by side using the ‘imshow’ function with their respective titles.

Conclusion

Hence, in this article, we have explained how to convert a three−channel colored image into a gray scale image in MATLAB. You may try the above MATLAB code with your colored image to convert that into a gray scale image.

Updated on: 07-Aug-2023

138 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements