Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.File;
- import javax.imageio.ImageIO;
- import java.awt.image.BufferedImage;
- import java.io.IOException;
- import java.awt.Color;
- import static java.lang.System.out;
- public class Grayscaler
- {
- public static String ImageName="raw.png";
- public static void main(String args[])throws IOException
- {
- // Image objects to store all the types of gray-scaled images
- BufferedImage image,averaged,luma,desaturated,decoMax,decoMin,channelR,channelG,channelB;
- image = ImageIO.read(new File(ImageName));
- int WIDTH = image.getWidth();
- int HEIGHT = image.getHeight();
- averaged = new BufferedImage(WIDTH,HEIGHT,image.getType());
- luma = new BufferedImage(WIDTH,HEIGHT,image.getType());
- decoMax = new BufferedImage(WIDTH,HEIGHT,image.getType());
- decoMin = new BufferedImage(WIDTH,HEIGHT,image.getType());
- desaturated = new BufferedImage(WIDTH,HEIGHT,image.getType());
- channelR = new BufferedImage(WIDTH,HEIGHT,image.getType());
- channelG = new BufferedImage(WIDTH,HEIGHT,image.getType());
- channelB = new BufferedImage(WIDTH,HEIGHT,image.getType());
- // Looping through each pixel
- for(int y=0;y<HEIGHT;y++)
- {
- for(int x=0;x<WIDTH;x++)
- {
- int RGB = image.getRGB(x,y);
- int R = (RGB >> 16) & 0xff; // Red Value
- int G = (RGB >> 8) & 0xff; // Green Value
- int B = (RGB) & 0xff; // Blue Value
- // Getting the max of RGB
- int MAX = Math.max(R,G);
- MAX = Math.max(MAX,B);
- // Getting the min of RGB
- int MIN = Math.min(R,G);
- MIN = Math.min(MIN,B);
- // Averaging
- int GRAY = (R+G+B)/3;
- Color color = new Color(GRAY,GRAY,GRAY);
- averaged.setRGB(x,y,color.getRGB());
- // Human Eye Correction
- GRAY = (int)(R*0.3f + G*0.59f + B*0.11f);
- color = new Color(GRAY,GRAY,GRAY);
- luma.setRGB(x,y,color.getRGB());
- // Desaturation
- GRAY = (MAX + MIN)/2;
- color = new Color(GRAY,GRAY,GRAY);
- desaturated.setRGB(x,y,color.getRGB());
- // Decomposition - MAX
- color = new Color(MAX,MAX,MAX);
- decoMax.setRGB(x,y,color.getRGB());
- // Decomposition - MIN
- color = new Color(MIN,MIN,MIN);
- decoMin.setRGB(x,y,color.getRGB());
- // Single Channel Gray-Scaling - Red
- color = new Color(R,R,R);
- channelR.setRGB(x,y,color.getRGB());
- // Single Channel Gray-Scaling - Green
- color = new Color(G,G,G);
- channelG.setRGB(x,y,color.getRGB());
- // Single Channel Gray-Scaling - Blue
- color = new Color(B,B,B);
- channelB.setRGB(x,y,color.getRGB());
- }
- }
- // Saving the Images
- ImageIO.write(averaged,"PNG",new File("averaged.png"));
- ImageIO.write(luma,"PNG",new File("luma.png"));
- ImageIO.write(desaturated,"PNG",new File("desaturated.png"));
- ImageIO.write(decoMax,"PNG",new File("decoMax.png"));
- ImageIO.write(decoMin,"PNG",new File("decoMin.png"));
- ImageIO.write(channelB,"PNG",new File("ChannelB.png"));
- ImageIO.write(channelG,"PNG",new File("ChannelG.png"));
- ImageIO.write(channelR,"PNG",new File("ChannelR.png"));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement