java 如何将彩色图像转换为纯黑白图像(0-255格式)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14851049/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 17:37:34  来源:igfitidea点击:

How to convert color image to pure black and white image(0-255 format)

javaimageswingimage-processingbufferedimage

提问by Yogesh

public class BlackWhite {

    public static void main(String[] args) 
    {
        try 
        {
         BufferedImage original = ImageIO.read(new File("colorimage"));
         BufferedImage binarized = new BufferedImage(original.getWidth(), original.getHeight(),BufferedImage.TYPE_BYTE_BINARY);

         int red;
         int newPixel;
         int threshold =230;

            for(int i=0; i<original.getWidth(); i++) 
            {
                for(int j=0; j<original.getHeight(); j++)
                {

                    // Get pixels
                  red = new Color(original.getRGB(i, j)).getRed();

                  int alpha = new Color(original.getRGB(i, j)).getAlpha();

                  if(red > threshold)
                    {
                        newPixel = 0;
                    }
                    else
                    {
                        newPixel = 255;
                    }
                    newPixel = colorToRGB(alpha, newPixel, newPixel, newPixel);
                    binarized.setRGB(i, j, newPixel);

                }
            } 
            ImageIO.write(binarized, "jpg",new File("blackwhiteimage") );
         }
        catch (IOException e) 
        {
                e.printStackTrace();
        }    
    }

     private static int colorToRGB(int alpha, int red, int green, int blue) {
            int newPixel = 0;
            newPixel += alpha;
            newPixel = newPixel << 8;
            newPixel += red; newPixel = newPixel << 8;
            newPixel += green; newPixel = newPixel << 8;
            newPixel += blue;

            return newPixel;
        }
}

I got a black and white output image, but as I zoomed the image I discovered some gray area. I want the output image to only contain the colors black or white.

我得到了一个黑白输出图像,但是当我放大图像时,我发现了一些灰色区域。我希望输出图像只包含黑色或白色。

Please let me know if I am correct or incorrect in my current approach? And if I am, please suggest another way.

请让我知道我目前的方法是否正确?如果我是,请提出另一种方法。

回答by iTech

You are converting the image correctly from color to black and white; however, when you save the output as JPEGsome colors are created as a result of the lossycompression.

您正在正确地将图像从彩色转换为黑白;但是,当您保存输出时,JPEG由于有损压缩会创建一些颜色。

Simply save the output to PNG(or anything other than JPEG), and the output will be only black and white, as you had expected.

只需将输出保存到PNG(或除 之外的任何内容JPEG),输出将只有黑色和白色,正如您所期望的那样。

ImageIO.write(binarized, "png",new File("blackwhiteimage") );

For example, if you have a binary image that saved as PNGthe histogram can be something like (strictly black and white pixels only):

例如,如果您有一个保存为PNG直方图的二值图像可能类似于(仅限黑色和白色像素):

PNG histogram

PNG直方图

And for the same image if you saved as JPEG, you can see that in the histogram some pixels near the white and black color start to appear

对于相同的图像,如果您另存为JPEG,您可以看到在直方图中,白色和黑色附近的一些像素开始出现

JPEG histogram

JPEG直方图