简体   繁体   English

如何从灰度图像中获取白色像素值并将其替换为另一种颜色?

[英]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. 我试图从GrayScale图像中获取白色像素的值,并将其替换为另一种Color,但是当我运行代码时,整个GrayScale图像都将转移到另一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". 首先,在指定要保存的文件名时,提供了“ .png”扩展名,但是在调用ImageIO.write()函数时,则指定了文件类型“ 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. 如果您尝试打开生成的文件,大多数程序将给您“这不是有效的.PNG文件”错误。 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. Windows资源管理器试图变得聪明,然后将.PNG重新解释为.JPG,但这使您避免了发现错误的机会。

This takes care of the strange redness problem. 这样可以解决奇怪的发红问题。

However, if you specify "png" in ImageIO.write() , you still don't get the right image. 但是,如果在ImageIO.write()指定“ png”,则仍然无法获得正确的图像。 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. 我没有足够的时间查看您的原始图像以找出真正的问题所在,但是我怀疑它实际上是一个带有alpha蒙版的明亮图像,从而使它看起来不太亮,并且该图像存在问题。保存图像的方式会去除Alpha信息,从而明显增加亮度。

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(); 事实证明,您从myImage.getRGB();获得的int值的ARGB格式myImage.getRGB(); returns 255 for "A", which means that you need to be checking for 0xFFFFFFFF, not 0x00FFFFFF. 对于“ A”返回255,这意味着您需要检查0xFFFFFFFF,而不是0x00FFFFFF。

And of course when you replace a value, you must replace it with 0xFF0000FF, specifying a full alpha value. 当然,当您替换一个值时,必须用0xFF0000FF替换它,并指定一个完整的alpha值。 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. 用0x000000FF替换像素没有可见效果,因为不管高蓝色值如何,alpha都为零,因此该像素将变为透明。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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