-1

I need my program to go through the pixels in an image, change them to grey scale. Then I need to take a range of gray values and colorize them using if - else and if-else-if statements. Can someone please help me figure this out?

Here's my code so far:

import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import javax.imageio.ImageIO;
import javax.swing.JFrame;

public class Colorize {

    BufferedImage image;
    int width;
    int height;

    public Colorize()  {
        try {
            File input = new File("Grayscale.jpg");
            image = ImageIO.read(input);
            width = image.getWidth();
            height = image.getHeight();

            for(int i=0; i<height; i++){

                for(int j=0; j<width; j++){
                    int col = image.getRGB(i, j);
                    Color c = new Color(col, true);
                    int red = c.getRed();
                    int green = c.getGreen();
                    int blue = c.getBlue();
                    if ((red>= 1)&&(red<=30))  {
                        c = new Color(c.getRed() + 10, c.getGreen(), c.getBlue());
                    }
                    if ((red>= 31)&&(red<=60))  {
                        c = new Color(c.getRed(), c.getGreen() + 10, c.getBlue());
                    }
                    if ((red>= 61)&&(red<=90))  {
                        c = new Color(c.getRed(), c.getGreen(), c.getBlue() + 10);
                    }
                    if ((red>= 91)&&(red<=120))  {
                        c = new Color(c.getRed() + 10, c.getGreen() + 10, c.getBlue());
                    }
                    if ((red>= 121)&&(red<=150))  {
                        c = new Color(c.getRed() + 10, c.getGreen(), c.getBlue() + 10);
                    }
                    if ((red>= 151)&&(red<=180))  {
                        c = new Color(c.getRed(), c.getGreen() + 10, c.getBlue() + 10);
                    }
                    if ((red>= 181)&&(red<=210))  {
                        c = new Color(c.getRed() - 10, c.getGreen(), c.getBlue());
                    }
                    if ((red>= 211)&&(red<=240))  {
                        c = new Color(c.getRed(), c.getGreen() - 10, c.getBlue());
                    }
                    else {
                        c = new Color(c.getRed(), c.getGreen(), c.getBlue());
                    }
                    image.setRGB(j,i,c.getRGB());
                }
            }

            File output = new File("Colorize.jpg");
            ImageIO.write(image, "jpg", output);

        } catch (Exception e) {}
    }

    static public void main(String args[]) throws Exception 
    {
        Colorize obj = new Colorize();
    }
}

Here's the image in case you guys want to try the code out. So far nothing is being written to the folder.

enter image description here

Community
  • 1
  • 1

2 Answers2

1

There must be an exception somewhere, unfortunately you catch exception and do nothing.

Replace

catch (Exception e) {}

with

catch (Exception e) {
    e.printStackTrace();
}

That will help you find what's happening. My guess is you will get a FileNotFoundException because Grayscale.jpg is probably not in your working directory.

StephaneM
  • 4,779
  • 1
  • 16
  • 33
  • I am not getting an error. It just doesn't save the new image to the folder. – Coder02983409 Jul 13 '16 at 11:55
  • How can you be sure there is no error since you do nothing in case of error? If there were no error you should at least write a destination file equal to the original file. – StephaneM Jul 13 '16 at 12:04
  • Here is the error i'm getting now: java.lang.ArrayIndexOutOfBoundsException: Coordinate out of bounds! at sun.awt.image.ByteInterleavedRaster.getDataElements(ByteInterleavedRaster.java:318) at java.awt.image.BufferedImage.getRGB(BufferedImage.java:917) at Colorize.(Colorize.java:23) at Colorize.main(Colorize.java:72) at __SHELL27.run(__SHELL27.java:6) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) – Coder02983409 Jul 13 '16 at 12:26
  • 1
    `int col = image.getRGB(i, j);` should be `int col = image.getRGB(j, i);` as the 1st parameter of getRGB is X and the 2nd is Y. – StephaneM Jul 13 '16 at 14:02
  • Dude. That fixed the whole thing. That was amazing. Thank you so much for your help in fixing this problem. You taught me a very important lesson. Thank you! – Coder02983409 Jul 13 '16 at 14:40
0

To convert your image to a grayscale one, you need your colors Weighted average. 0.2126 r 0.7152 g 0.0722 b. Getting colors this way is the same as getRed() getGreen() and getBlue() works. Finally add up your colors and set it with setRGB. This will turn your image to a grayscale.

for(int i=0; i<image.getWidth(); i++){
      for(int j=0; j<image.getHeight(); j++){
          int color = image.getRGB(i,j);
          int r = ((color >> 16) & 0xFF) * 0.2126;
          int g = ((color >> 8) & 0xFF) * 0.7152;
          int b = ((color) & 0xFF) * 0.0722;
          int finalColor = (r << 16) | (g << 8) | b;
          image.setRGB(i,j,finalColor);
      }
 }

Edit your question with more information about your coloring so I could help more.

eldo
  • 1,327
  • 2
  • 16
  • 27