简体   繁体   中英

How can I get white colored pixel value from gray scale image and replace it with another color?

I am trying to get the value of the White Colored pixel from a GrayScale image and replace it with another Color but when I run my code, the whole GrayScale image is transfered to another Color. Can anyone please tell me where is fault in the code or how can I get my desired results?? This is the code...

public class gray {
    public static void main (String args[])throws IOException{
        int width;
        int height;

        BufferedImage myImage = null;

        File f = new File("E:\\eclipse\\workspace\\Graphs\\src\\ColorToGray\\1.png");
        myImage = ImageIO.read(f);

        width = myImage.getWidth();
        height = myImage.getHeight();

        BufferedImage image = new BufferedImage(width,height,BufferedImage.TYPE_INT_ARGB);

        int pixels[];


        pixels = new int[width * height];
        myImage.getRGB(0, 0, width, height, pixels, 0, width);

        for (int i = 0; i < pixels.length; i++) {
            if (pixels[i] == 0xFFFFFF) { 
                pixels[i] = 0x000000FF;
            }
        }
        File f2 = new File("E:\\eclipse\\workspace\\Graphs\\src\\ColorToGray\\out 1.png");
        image.setRGB(0, 0, width, height, pixels, 0, width);
        ImageIO.write( image, "jpg", f2);

    }
}

Image Before:

Image Before Output

Image After:

Image After Output

I looked into it, and found a bunch of problems.

First of all, when specifying the filename to save, you supply a ".png" extension, but when you call the ImageIO.write() function, you specify file type "jpg". That tends not to work very well. If you try to open up the resulting file, most programs will give you a "this is not a valid .PNG file" error. Windows explorer tries to be smart, and re-interprets the .PNG as a .JPG, but this spared you from the chance of discovering your mistake.

This takes care of the strange redness problem.

However, if you specify "png" in ImageIO.write() , you still don't get the right image. One would expect an image that looks mostly like the original, with just a few patches of blue there where bright white used to be, but instead what we get is an overall brighter version of the original image.

I do not have enough time to look into your original image to find out what is really wrong with it, but I suspect that it is actually a bright image with an alpha mask that makes it look less bright, AND there is something wrong with the way the image gets saved that strips away alpha information, thus the apparent added brightness.

So, I tried your code with another image that I know has no tricks in it, and still your code did not appear to do anything. It turns out that the ARGB format of the int values you get from myImage.getRGB(); returns 255 for "A", which means that you need to be checking for 0xFFFFFFFF, not 0x00FFFFFF.

And of course when you replace a value, you must replace it with 0xFF0000FF, specifying a full alpha value. Replacing a pixel with 0x000000FF has no visible effect, because regardless of the high blue value, alpha is zero, so the pixel would be rendered transparent.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM