Convert RGB to binary image in Java

In this tutorial, we will learn how to convert RGB to a binary image (black and withe image) in Java programming language.

A binary image consists of only black and white in color. That means we are actually going to convert a colored image into black and white in Java.

In this topic, we will read an image first and then we will change it to the binary (black and white) image. We will write it into a new image.

Before we go forward, first of all, we have to read the image file. For reading our image we are going to use the BufferedImage class. Below is the Java program:

BufferedImage img = null;
String fileName = "apple.jpeg"; // if image was not in current directory copy the path of the image

try {
    //Read in new image file
    img = ImageIO.read(new File(fileName));    
} 
catch (IOException e){
    System.out.println("Error: "+e);
}

 In this step, we use the existing image file as a new File, after that we read this file by ImageIO and store it in the BuffererdImage.

To handle the exception like FileNotFountEcxeption we are using the Java try and catch block.

Now let’s find the width and height of the image:

int h=img.getHeight();
int w=img.getWidth();

After that, create a new BufferedImage with the width and height of the previous image:

BufferedImage bufferedImage = new BufferedImage(w,h, BufferedImage.TYPE_INT_RGB);

In this step, we will find the RGB value of the specific point of the image first. Next, we find the value of R, G, B by using the getRGB() method:

int val = img.getRGB(i, j);
int r = (0x00ff0000 & val) >> 16;
int g = (0x0000ff00 & val) >> 8;
int b = (0x000000ff & val);

In this step, we will set a color for each pixel black or white by using the setRGB() method.

int m=(r+g+b);
//(255+255+255)/2 =283 middle of dark and light
if(m>=383)
{
    // for light color it set white
    bufferedImage.setRGB(i, j, Color.WHITE.getRGB()); 
}
else{  
    // for dark color it will set black
    bufferedImage.setRGB(i, j, 0);
}

Because of 383 is the average of black and white colors, if the total RGB value of one color is greater than 383 it means that color is the light color we will set to it white, otherwise, we will set black for that color in the specific pixel by i and j coordinates

For changing all the pixels we are using two for loop with the width into the height number of looping.

At the end of this, we will write and save this BufferedImage into a jpg file.

File file = new File("MyImage.jpg");
ImageIO.write(bufferedImage, "jpg", file);

 

The complete Java program to convert RGB to a binary image

Below is our complete Java program that is able to do our task is given below:

import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;

public class Codespeedy  {
 
    public static void main(String[] args) throws IOException {
            
       BufferedImage img = null;
       String fileName = "apple.jpeg";
       
       try {
           //Read in new image file
           img = ImageIO.read(new File(fileName));    
       } 
       catch (IOException e){
           System.out.println("Error: "+e);
       }
       int h=img.getHeight();
       int w=img.getWidth();
       
       
       BufferedImage bufferedImage = new BufferedImage(w,h, BufferedImage.TYPE_INT_RGB);

       if (img == null) {
             System.out.println("No image loaded");
        } 
       else {
           for(int i=0;i<w;i++)
           {
               for(int j=0;j<h;j++)
               {
           
               
                    //Get RGB Value
                    int val = img.getRGB(i, j);
                    //Convert to three separate channels
                    int r = (0x00ff0000 & val) >> 16;
                    int g = (0x0000ff00 & val) >> 8;
                    int b = (0x000000ff & val);

                    int m=(r+g+b);
                    //(255+255+255)/2 =283 middle of dark and light
                    if(m>=383)
                    {
                        // for light color it set white
                        bufferedImage.setRGB(i, j, Color.WHITE.getRGB()); 
                    }
                    else{  
                        // for dark color it will set black
                        bufferedImage.setRGB(i, j, 0);
                    }
                }  
            }
        }
       
       
        File file = new File("MyImage.jpg");
        ImageIO.write(bufferedImage, "jpg", file);
 
    }
}

Just run the above code and you will see that our colored image converted into a binary image.

Below is given the original image before the binary conversion:

Apple

Now See our converted image below:

Apple Binary Image

 

 

Leave a Reply

Your email address will not be published.