# How to Convert RGB Image to Binary Image Using MATLAB?

In this article, we will explore how to convert an RGB image to a binary image using MATLAB programming.

An RGB image is one which is represented by using three color channels namely, Red, Green, and Blue. In the case of an RGB image, the three primary colors, namely red, green, and blue are used to represent all the colors in the image. In the RGB image, each pixel of the image is represented by a combination of intensity values for these three colors. Therefore, the three intensity values of RGB colors determine the color of the pixel.

In MATLAB, an RGB image is represented as a 3D array, where each dimension represents one of the RGB color channels.

On the other hand, a binary image is a monochromatic image which has only two intensity values for each pixel, i.e. 0 and 1. In a binary image, the background is represented by 0 and the foreground or object or the region of interest is represented by 1.

In MATLAB, the binary images are represented as a 2D array, where each element of the array can be either a 0 or a 1. Binary images are mainly used in digital image processing applications like pattern recognition, image segmentation, object detection, etc.

## Convert RGB Image to Binary Image in MATLAB

In MATLAB, we can use a built−in function ‘imbinarize’ to convert a given RGB image into a binary image.

The conversion of RGB image to binary image in MATLAB is performed as per the following steps:

Step (1) − Read the input RGB image.

Step (2) − Convert the input RGB image to a grayscale image.

Step (3) − Specify a threshold value to perform binary conversion. This value depends upon the specific image and requirements. It lies between 0 and 1.

Step (4) − Convert the grayscale image to binary image based on the defined threshold value.

Step (5) − Display the binary image.

Now, let us understand how to implement this algorithm to convert an RGB image to binary image in MATLAB with the help of example programs.

### Example

%MATLAB code for converting an RGB image to a binary image
% Read the input RGB image

% Convert RGB image to grayscale image
gray_img = rgb2gray(rgb_img);

% Specify a threshold value for binarization of the image
t = 0.7; % Adjust according to the image and requirements

% Convert grayscale image to binary image
binary_img = imbinarize(gray_img, t);

% Display the RGB and binary images
subplot(1, 2, 1); imshow(rgb_img); title('RGB Image');
subplot(1, 2, 2); imshow(binary_img); title('Binary Image');



### Code Explanation

In this MATLAB code, we start by reading the input RGB image using the ‘imread’ function and store it in a variable ‘rgb_img’. Then, we convert the input RGB image to a grayscale image by using the ‘rgb2gray’ function.

After that we define choose a threshold value ‘t’ based on the input image and requirements, in this case we have taken ‘t = 0.7’. To get a lighter image decrease this value.

Next, we convert the gray scale image to binary image based on the specified threshold value. For this, we use the ‘imbinarize’ function.

Finally, we display the input RGB image and its corresponding binary image by using the ‘imshow’ function.

Let us consider another example program to convert an RGB image to a binary image using MATLAB programming.

### Example

%MATLAB code for converting an RGB image to a binary image

% Convert RGB Image to Grayscale Image
gray_img = rgb2gray(rgb_img);

% Specify a Threshold Value
t = 0.7; % Adjust according to the image and requirements

% Convert Gray Scale Image to Binary Image
binary_img = imbinarize(gray_img, t);

% Display the RGB and Binary Images
subplot(1, 2, 1); imshow(rgb_img); title('RGB Image');
subplot(1, 2, 2); imshow(binary_img); title('Binary Image');



### Code Explanation

The implementation and execution of this MATLAB program is the same as that of the MATLAB program 1. In this code, we have taken a different image address in the ‘imread’ function for experiment.

## Conclusion

In conclusion, an RGB image is a colorful image in which each pixel of the image is represented as a combination of intensity values of red, green and blue color channels. This color space is widely for displaying color image on screens. On the other hand, a binary image is a monochromatic image in which each pixel of the image has only two intensity values, i.e. 0 and 1. This binary images are used for digital image processing jobs like image segmentation, pattern recognition, etc.

We can use a MATLAB function ‘imbinarize’ to convert an RGB image to a binary image. The process of RGB to binary image conversion is explained and demonstrated in the above sections of this article.

Updated on: 07-Aug-2023

337 Views