Image Processing in Java | Set 5 (Colored to Red Green Blue Image Conversion)


We strongly recommend to refer below post as a prerequisite of this.

In this set we will be converting a colored image to an image with either red effect, green effect or blue effect.

The basic idea is to get the pixel value for each cordinates and then keep the desired resultant color pixel value to be same and set the other two as zero.

Converting to Red Colored Image

Algorithm for converting an colored image to red colored:

  1. Get the RGB value of the pixel.
  2. Set the RGB values as follows:
    • R: NO CHANGE
    • G: Set to 0
    • B: Set to 0
  3. Replace the R, G and B value of the pixel with the values calculated in step 2.
  4. Repeat Step 1 to Step 3 for each pixels of the image.

Implementation of the above algorithm:



// Java program to demonstrate colored to red colored image conversion
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;

        // read image
        try
        {
            f = new File("G:\\Inp.jpg");
            img = ImageIO.read(f);
        }
        catch(IOException e)
        {
            System.out.println(e);
        }

        // get width and height
        int width = img.getWidth();
        int height = img.getHeight();

        // convert to red image
        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;

                // set new RGB
                // keeping the r value same as in original
                // image and setting g and b as 0.
                p = (a<<24) | (r<<16) | (0<<8) | 0;

                img.setRGB(x, y, p);
            }
        }

        // write image
        try
        {
            f = new File("G:\\Out.jpg");
            ImageIO.write(img, "jpg", f);
        }
        catch(IOException e)
        {
            System.out.println(e);
        }
    }
}

Note : This code will not run on online IDE as it needs an image on disk.
Output:

Inp.jpg
Inp

Oup.jpg
Out red

 

Converting to Green Colored Image

Algotithm for converting an colored image to green colored:

  1. Get the RGB value of the pixel.
  2. Set the RGB values as follows:
    • R: Set to 0
    • G: NO CHANGE
    • B: Set to 0
  3. Replace the R, G and B value of the pixel with the values calculated in step 2.
  4. Repeat Step 1 to Step 3 for each pixels of the image.


Implementation of the above algorithm:

// Java program to demonstrate colored to green coloured
// image conversion
import java.io.File;
import java.io.IOException;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;

public class GreenImage
{
    public static void main(String args[])throws IOException
    {
        BufferedImage img = null;
        File f = null;

        //read image
        try
        {
            f = new File("G:\\Inp.jpg");
            img = ImageIO.read(f);
        }
        catch(IOException e)
        {
            System.out.println(e);
        }

        // get width and height
        int width = img.getWidth();
        int height = img.getHeight();

        // convert to green image
        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 g = (p>>8)&0xff;

                // set new RGB
                // keeping the g value same as in original
                // image and setting r and b as 0.
                p = (a<<24) | (0<<16) | (g<<8) | 0;

                img.setRGB(x, y, p);
            }
        }

        //write image
        try
        {
            f = new File("G:\\Out.jpg");
            ImageIO.write(img, "jpg", f);
        }
        catch(IOException e)
        {
            System.out.println(e);
        }
    }
}

Note : This code will not run on online IDE as it needs an image on disk.
Output:

Inp.jpg
Inp

Oup.jpg
Out green

 

Converting to Blue Colored Image

Algorithm for converting an colored image to blue colored:

  1. Get the RGB value of the pixel.
  2. Set the RGB values as follows:
    • R: Set to 0
    • G: Set to 0
    • B: NO CHANGE
  3. Replace the R, G and B value of the pixel with the values calculated in step 2.
  4. Repeat Step 1 to Step 3 for each pixels of the image.


Implementation of the above algorithm:

// Java program to demonstrate colored to blue coloured image conversion
import java.io.File;
import java.io.IOException;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;

public class BlueImage
{
    public static void main(String args[])throws IOException
    {
        BufferedImage img = null;
        File f = null;

        //read image
        try
        {
            f = new File("G:\\Inp.jpg");
            img = ImageIO.read(f);
        }
        catch(IOException e)
        {
            System.out.println(e);
        }

        // get width and height
        int width = img.getWidth();
        int height = img.getHeight();

        // convert to blue image
        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 b = p&0xff;

                // set new RGB
                // keeping the b value same as in original
                // image and setting r and g as 0.
                p = (a<<24) | (0<<16) | (0<<8) | b;

                img.setRGB(x, y, p);
            }
        }

        // write image
        try
        {
            f = new File("G:\\Out.jpg");
            ImageIO.write(img, "jpg", f);
        }
        catch(IOException e)
        {
            System.out.println(e);
        }
    }
}

Note : This code will not run on online IDE as it needs an image on disk.
Output:

Inp.jpg
Inp

Oup.jpg
Out


This article is contributed by Pratik Agarwal. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.




Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.

Recommended Posts:



3.5 Average Difficulty : 3.5/5.0
Based on 2 vote(s)