Java Image Processing – How to Convert Colored Image in Java

1. Java Image Processing – Objective

In this Java Image Processing Tutorial, we will study How to Convert Colored Image- in Grayscale, in a red, green & blue image, in the sepia image and in negative images in Java Processing Language. Moreover, we will study this convert colored image in Java with algorithms and implementation java programs to understand well.

So, Let’s start How to Convert Colored Image in Java.

Java Image Processing - How to Convert Colored Image in Java

Java Image Processing – How to Convert Colored Image in Java

2. Convert Colored Image to Greyscale in Java

To convert colored image to greyscale in Java, the Alpha part of the picture will be same as the first picture. However, the RGB will be changed i.e., every one of the three RGB segments will have the same incentive for every pixel.

Let’s revise Java Character Class Methods with Syntax and Examples

i. Algorithm for the following Java Image Proecssing–

  • Get the RGB estimation of the pixel.
  • Locate the normal of RGB i.e., Avg = (R+G+B)/3
  • Supplant the R, G and B estimation of the pixel with normal (Avg) ascertained in stage 2.
  • Rehash Step 1 to Step 3 for every pixel of the picture.

ii. Program to implement the following algorithm

import java.io.File;
import java.io.IOException;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
public class Grayscale
  {
      public static void main(String args[])throws IOException
        {
           BufferedImage img = null;
           File f = null;
           try
           {
              f = new File("G:\\Inp.jpg");
              img = ImageIO.read(f);
           }
        catch(IOException e)
          {
             System.out.println(e);
          }
        int width = img.getWidth();
        int height = img.getHeight();
        for (int y = 0; y < height; y++)
          {
             for (int x = 0; x < width; x++)
              {
                  int p = img.getRGB(x,y);
                  int a = (p>>24)&0xff;
                  int r = (p>>16)&0xff;
                  int g = (p>>8)&0xff;
                  int b = p&0xff;
                  int avg = (r+g+b)/3;
                  p = (a<<24) | (avg<<16) | (avg<<8) | avg;
                  img.setRGB(x, y, p);
              }
          }
          try
           {
              f = new File("G:\\Out.jpg");
              ImageIO.write(img, "jpg", f);
           }
          catch(IOException e)
          {
              System.out.println(e);
          }
      }
  }

Read about Java Method – Declaration and Calling a Java Method

3. Convert Colored Image to Negative Image in Java

In this Java Image Processing, to convert colored image to negative image, the Alpha part of the picture will be same as the first picture. However, the RGB will be changed i.e., each of the three RGB segments will have an estimation of 255-unique segment esteem.

i. Algorithm for the following Java Image Proecssing–

  • Get the RGB estimation of the pixel.
  • Figure new RGB esteems as takes after:

R = 255 – R
G = 255 – G
B = 255 – B

  • Supplant the R, G and B estimation of the pixel with the qualities ascertained in stage 2.
  • Rehash Step 1 to Step 3 for every pixel of the picture.

ii. Implementation of the algorithm

import java.io.File;
import java.io.IOException;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
public class Negative
  {
    public static void main(String args[])throws IOException
    {
        BufferedImage img = null;
        File f = null;
        try
        {
            f = new File("G:\\Inp.jpg");
            img = ImageIO.read(f);
        }
        catch(IOException e)
        {
            System.out.println(e);
        }
        int width = img.getWidth();
        int height = img.getHeight();
        for (int y = 0; y < height; y++)
        {
            for (int x = 0; x < width; x++)
            {
                int p = img.getRGB(x,y);
                int a = (p>>24)&0xff;
                int r = (p>>16)&0xff;
                int g = (p>>8)&0xff;
                int b = p&0xff;
                r = 255 - r;
                g = 255 - g;
                b = 255 - b;
                p = (a<<24) | (r<<16) | (g<<8) | b;
                img.setRGB(x, y, p);
            }
        }
        try
       {
            f = new File("G:\\Out.jpg");
            ImageIO.write(img, "jpg", f);
        }
        catch(IOException e)
        {
            System.out.println(e);
        }
    }
}

What is Inheritance in Java – How Java Inheritance is used

4. Convert Colored Image to Red Green Blue Image in Java

Here, Java Image Processing, the essential thought is to get the pixel esteem for each coordinate and after that keep the coveted resultant shading pixel incentive to be same and set the other two as zero.
Changing over to Red Colored Image

How to convert Colored image in Java

How to convert Colored image in Java

i. Algorithm for converting colored image to red hued:

  • Get the RGB estimation of the pixel.
  • Set the RGB esteems as takes after:

R: NO CHANGE
G: Set to 0
B: Set to 0

  • Supplant the R, G and B estimation of the pixel with the qualities figured in stage 2.
  • Rehash Step 1 to Step 3 for every pixel of the picture.

Do you Know Method Overloading vs Overriding in Java

ii. Implementation of the algorithm

import java.io.File;
import java.io.IOException;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
public class RedImage
{
    public static void main(String args[])throws IOException
    {
        BufferedImage img = null;
        File f = null;
        try
        {
            f = new File("G:\\Inp.jpg");
            img = ImageIO.read(f);
        }
        catch(IOException e)
        {
            System.out.println(e);
        }
        int width = img.getWidth();
        int height = img.getHeight();
        for (int y = 0; y < height; y++)
        {
            for (int x = 0; x < width; x++)
            {
               int p = img.getRGB(x,y);
                int a = (p>>24)&0xff;
                int r = (p>>16)&0xff;
               p = (a<<24) | (r<<16) | (0<<8) | 0;
              img.setRGB(x, y, p);
           }
        }
        try
        {
            f = new File("G:\\Out.jpg");
            ImageIO.write(img, "jpg", f);
        }
      catch(IOException e)
        {
            System.out.println(e);
        }
    }
}

Let’s Explore Serialization and Deserialization in Java

Java Quiz

5. Convert Colored Image to Sepia Image in Java

To convert colored image to sepia picture, the Alpha segment of the picture will be same as the first image(since alpha segment indicates the straightforwardness). However, the RGB will be changed, which will be computed by the accompanying formula.

newRed = 0.393*R + 0.769*G + 0.189*B

newGreen = 0.349*R + 0.686*G + 0.168*B

newBlue = 0.272*R + 0.534*G + 0.131*B

i. Algorithm for the following Java Image Proecssing–

  • Get the RGB estimation of the pixel.
  • Ascertain newRed, newGree, newBlue utilizing the above equation (Take the whole number esteem)
  • Set the new RGB estimation of the pixel according to the accompanying condition:

In the event that newRed > 255 then R = 255 else R = newRed
In the event that newGreen > 255 then G = 255 else G = newGreen
In the event that newBlue > 255 then B = 255 else B = newBlue

  • Supplant the estimation of R, G, and B with the new esteem that we computed for the pixel.
  • Rehash Step 1 to Step 4 for every pixel of a picture

Let’s Learn Java URL Class With Examples

ii. Implementation of Algorithm

import java.io.File;
import java.io.IOException;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
public class Sepia
{
    public static void main(String args[])throws IOException
    {
        BufferedImage img = null;
        File f = null;
        try
        {
            f = new File("G:\\Inp.jpg");
            img = ImageIO.read(f);
        }
        catch(IOException e)
       {
            System.out.println(e);
        }
        int width = img.getWidth();
        int height = img.getHeight();
        //convert to sepia
        for(int y = 0; y < height; y++)
        {
            for(int x = 0; x < width; x++)
            {
                int p = img.getRGB(x,y);
               int a = (p>>24)&0xff;
                int R = (p>>16)&0xff;
                int G = (p>>8)&0xff;
                int B = p&0xff;
                int newRed = (int)(0.393*R + 0.769*G + 0.189*B);
                int newGreen = (int)(0.349*R + 0.686*G + 0.168*B);
                int newBlue = (int)(0.272*R + 0.534*G + 0.131*B);
                if (newRed > 255)
                    R = 255;
                else
                    R = newRed;
                if (newGreen > 255)
                    G = 255;
                else
                    G = newGreen;
                if (newBlue > 255)
                    B = 255;
               else
                   B = newBlue;
               p = (a<<24) | (R<<16) | (G<<8) | B;
                img.setRGB(x, y, p);
            }
        }
        try
        {
            f = new File("G:\\Out.jpg");
            ImageIO.write(img, "jpg", f);
        }
        catch(IOException e)
        {
            System.out.println(e);
        }
    }
}

This was all about Java Image Processing tutorial, we hope you like our explanation of How to Convert Colored Image in Java.

Read about Switch Statement in Java With Examples

6. Conclusion

In this Java Image Processing tutorial, we discuss four ways to convert a coloured image in java image processing: convert color image to a grayscale conversion of images, convert a coloured image to negative image conversion, convert coloured image to Red Blue Green image, and convert coloured image to Sepia image conversion in java. In conclusion, we saw the algorithm and implementation program for each Java Image Processing. In our next Java Image processing tutorial, we will study watermarking, face detection etc.

Furthermore, if you have any query feel free to ask in a comment section.

Related Topic- Wrapper Class in Java

For reference

Leave a Reply

Your email address will not be published. Required fields are marked *