Open In App

How to Convert RGB Image to Binary Image Using MATLAB?

Improve
Improve
Like Article
Like
Save
Share
Report

An Image, by definition, is essentially a visual representation of something  that depicts or records visual perception. Images are classified in one of the three types. 

  • Binary Images
  • Grayscale Images
  • Color Images

Binary Images: This is the most basic type of image that exists. The only permissible pixel values in Binary images are 0(Black) and 1(White). Since only two values are required to define the image wholly, we only need one bit and hence binary images are also known as 1-Bit images. 

Grayscale Images: Grayscale images are by definition, monochrome images. Monochrome images have only one color throughout and the intensity of each pixel is defined by the gray level it corresponds to. Generally, an 8-Bit image is the followed standard for grayscale images implying that there are 28= 256 grey levels in the image indexed from 0 to 255. 

Color Images: Color images can be visualized by 3 color planes(Red, Green, Blue) stacked on top of each other. Each pixel in a specific plane contains the intensity value of the color of that plane. Each pixel in a color image is generally comprised of 24 Bits/pixel with 8 pixels contributing from each color plane.   

In this article, we will be discussing how to convert an RGB image to a Binary image using MATLAB. 

Approach :

  • Read the RGB Image.
  • Using the im2bw() function in MATLAB, applying thresholding and classifying the pixel values as 0 or 1 by comparing with the threshold. 
  • Show both the Images together for comparison purposes. 

Example 1:

Matlab




% Matlab Code to convert an RGB Image to Binary image
% reading image 
I = imread('GFG.jpeg');
  
% Creating figure window for input image
% Displaying the input image
imshow(I);
  
% Converting the image from rgb to binary using thresholding 
J = im2bw(I,0.7);
  
% Creating figure window for the output image
% Displaying the output image
imshow(J);


Output:

Figure 1: Input Image

Figure 2 : Output Image

Consider another example with MATLAB’s inbuilt image of a lighthouse. 

Example 2:

Matlab




% Matlab Code to convert an RGB Image to Binary image
% reading image 
I = imread('lighthouse.png');
  
% Creating figure window for input image
% Displaying the input image
imshow(I);
  
% Converting the image from rgb to 
% binary using thresholding 
J = im2bw(I,0.5);
  
% Creating figure window for the output image
% Displaying the output image
imshow(J);


Output:

Figure 3: Input Image

Figure 4: Output Image

Code Explanation:

  • I = imread(‘lighthouse.png’); This line reads the image
  • imshow(I); This line displays the input image I in the figure window
  • J = im2bw(I,0.5); This line converts the RGB Image to Binary with the threshold level set at 0.5 for comparing intensity levels of pixels. Essentially all 256 intensity levels are mapped to numbers between 0 and 1 and then based on the threshold value and pixel value, the pixel is classified as either black or white.
  • imshow(J); This line displays the output image J in another figure window.

This method can be applied to various other RGB Images as well and can be toggled by changing the different threshold values and seeing how classification occurs based on varying threshold values. 



Last Updated : 23 Feb, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads