简体   繁体   English

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

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

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. 如果我是,请提出另一种方式。

You are converting the image correctly from color to black and white; 您正在将图像从颜色正确转换为黑白; however, when you save the output as JPEG some colors are created as a result of the lossy compression . 但是,当您将输出保存为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 PNG the histogram can be something like (strictly black and white pixels only): 例如,如果您有一个保存为PNG的二进制图像,则直方图可能类似于(仅限严格的黑白像素):

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直方图

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

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