简体   繁体   English

使用从头创建的java将彩色图像转换为黑白图像

[英]Convert color image into black and white using java created from scratch

I want to convert my picture from colored to Black and white which seems to be created from scratch. 我想将我的照片从彩色转换为黑白,这似乎是从头开始创建的。 Here is the code which i tried as described on the different post: 这是我在不同帖子中描述的代码:

    BufferedImage bi = ImageIO.read(new File("/Users/***/Documents/Photograph.jpg"));
    ColorConvertOp op = 
        new ColorConvertOp(ColorSpace.getInstance(ColorSpace.CS_GRAY), null);
    ImageIO.write(bi, "PNG", new File("/Users/bng/Documents/rendered2.png"));
    op.filter(bi, bi);

But still my image is not converted to the Black and white. 但我的图像仍未转换为黑白图像。 Additionally, this code is increasing the rendered2.png image size to 10 folds. 此外,此代码将rendered2.png图像大小增加到10倍。 Also, it would be great if i could find some Java 8 way of doing this. 而且,如果我能找到一些Java 8的方法,那将是很棒的。 Any suggestions? 有什么建议么?

Here is the code which worked for me: 这是适合我的代码:

    BufferedImage input = ImageIO.read(new File("/Users/bng/Documents/Photograph.jpg"));
    // Create a black-and-white image of the same size.
    BufferedImage im = new BufferedImage(input.getWidth(), input.getHeight(), BufferedImage.TYPE_BYTE_BINARY);
    // Get the graphics context for the black-and-white image.
    Graphics2D g2d = im.createGraphics();
    // Render the input image on it.
    g2d.drawImage(input, 0, 0, null);
    // Store the resulting image using the PNG format.
    ImageIO.write(im, "PNG", new File("/Users/bng/Documents/rendered.png"));

It was BufferedImage.TYPE_BYTE_BINARY which provided me the exact solution. 它是BufferedImage.TYPE_BYTE_BINARY,它为我提供了确切的解决方案。 Lokking for the Java 8 Version for above code. 为上面的代码寻找Java 8版本。

  1. You have to find RGB of the existing colors of the image you want to change it. 您必须找到要更改它的图像的现有颜色的RGB。
  2. Fyi, you want to change it as white RGB value is (255,255,255) and for black RGB value is (0,0,0) Fyi,你想改变它,因为白色RGB值是(255,255,255),黑色RGB值是(0,0,0)

  3. Following method easily do the color change if you apply correct way of your requirement 如果您应用正确的要求,以下方法可以轻松进行颜色更改

     private BufferedImage changeColor(BufferedImage image, int srcColor, int replaceColor) { BufferedImage destImage = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_ARGB); Graphics2D g = destImage.createGraphics(); g.drawImage(image, null, 0, 0); g.dispose(); for (int width = 0; width < image.getWidth(); width++) { for (int height = 0; height < image.getHeight(); height++) { if (destImage.getRGB(width, height) == srcColor) { destImage.setRGB(width, height, replaceColor); } } } return destImage; } 

you have to use the ColorConvertOp in a proper way: 你必须以正确的方式使用ColorConvertOp

  1. create Source image 创建源图像
  2. apply filter 应用过滤器
  3. save dest 保存目标

example: 例:

BufferedImage src = ImageIO.read(new File("/Users/***/Documents/Photograph.jpg"));

ColorConvertOp op = 
    new ColorConvertOp(ColorSpace.getInstance(ColorSpace.CS_GRAY), null);
BufferedImage dest = op.filter(src, null);

ImageIO.write(dest, "PNG", new File("/Users/bng/Documents/rendered2.png"));

src: SRC: 在此输入图像描述

dest: 目的地: 在此输入图像描述

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

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