Image Processing in Java – Colored to Red Green Blue Image Conversion
Prerequisites:
In this set, we will be converting a colored image to an image with either red effect, green effect, or blue effect.
Colored Image – The colored image or RGB color model is an additive mixing model in which red, green, and blue light are added together in various ways to reproduce a broad array of colors.
The basic idea is to get the pixel value for each coordinate and then keep the desired resultant color pixel value to be the same and set the other two as zero.
Converting to Red Colored Image
Algorithm:
- Get the RGB value of the pixel.
- Set the RGB values as follows:
- R: NO CHANGE
- G: Set to 0
- B: Set to 0
- Replace the R, G, and B values of the pixel with the values calculated in step 2.
- Repeat Step 1 to Step 3 for each pixel of the image.
Implementation:
Java
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
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(
"C:/Users/hp/Desktop/Image Processing in Java/gfg-logo.png" );
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(
"C:/Users/hp/Desktop/Image Processing in Java/GFG.png" );
ImageIO.write(img, "png" , f);
}
catch (IOException e) {
System.out.println(e);
}
}
}
|
Output –
Note: This code will not run on an online IDE as it needs an image on a disk.
Converting to Green Colored Image
Algorithm
- Get the RGB value of the pixel.
- Set the RGB values as follows:
- R: Set to 0
- G: NO CHANGE
- B: Set to 0
- Replace the R, G, and B values of the pixel with the values calculated in step 2.
- Repeat Step 1 to Step 3 for each pixel of the image.
Implementation:
Java
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
public class GreenImage {
public static void main(String args[])
throws IOException
{
BufferedImage img = null ;
File f = null ;
try {
f = new File(
"C:/Users/hp/Desktop/Image Processing in Java/gfg-logo.png" );
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 g = (p >> 8 ) & 0xff ;
p = (a << 24 ) | ( 0 << 16 ) | (g << 8 ) | 0 ;
img.setRGB(x, y, p);
}
}
try {
f = new File(
"C:/Users/hp/Desktop/Image Processing in Java/GFG.png" );
ImageIO.write(img, "png" , f);
}
catch (IOException e) {
System.out.println(e);
}
}
}
|
Output –
Note: This code will not run on an online IDE as it needs an image on a disk.
Converting to Blue Colored Image
Algorithm
- Get the RGB value of the pixel.
- Set the RGB values as follows:
- R: Set to 0
- G: Set to 0
- B: NO CHANGE
- Replace the R, G, and B values of the pixel with the values calculated in step 2.
- Repeat Step 1 to Step 3 for each pixel of the image.
Implementation
Java
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
public class BlueImage {
public static void main(String args[])
throws IOException
{
BufferedImage img = null ;
File f = null ;
try {
f = new File(
"C:/Users/hp/Desktop/Image Processing in Java/gfg-logo.png" );
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 b = p & 0xff ;
p = (a << 24 ) | ( 0 << 16 ) | ( 0 << 8 ) | b;
img.setRGB(x, y, p);
}
}
try {
f = new File(
"C:/Users/hp/Desktop/Image Processing in Java/GFG.png" );
ImageIO.write(img, "png" , f);
}
catch (IOException e) {
System.out.println(e);
}
}
}
|
Output –
Note: This code will not run on an online IDE as it needs an image on a disk.
Last Updated :
14 Nov, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...