简体   繁体   中英

Java: Taking a Image and turning it into Black in White with RGB

So I am doing a group project for my programming class, we are makeing a photo editing program and one of my parts of the program is taking the image and turning it into black and white using rgb. I was wondering what would be the best value or way in RGB to achieve black and white?

I would recommend letting the Java 2D library worry about the conversion:

  • create a greyscale BufferedImage (BufferedImage.TYPE_BYTE_GRAY);
  • get a graphics context by createGraphics()
  • ensure that colour rendering is accurate on that graphics context: call setRenderingHint(RenderingHints.KEY_COLOR_RENDERING, RenderingHints.VALUE_COLOR_RENDER_QUALITY)
  • draw the colour image you want to "convert" to the graphics context

If you do the conversion "manually" and you want to do it as accurately as possible, then you need to take into account that the eye is more sensitive to certain colour components than others. (If you want a "rough and ready" conversion, you can average the colour components, but this isn't strictly speaking the most accurate conversion.)

对于每个像素,您可以将RGB转换为HSB(使用Color.RGBtoHSB ),将饱和度设置为0,然后使用Color.getHSBColor转换回Color实例。

实际上,一旦有了RGB颜色, Wikipedia就会很好地介绍如何执行转换。

嘿,此页面上有许多Java图像过滤器可以免费下载。所有过滤器都是标准Java BufferedImageOps ,可以直接插入现有程序中GrayscaleFilter可以将图像转换为黑白图像

Using java.awt.image.ColorConvertOp with a gray destination ColorSpace is very efficient. There's an excellent example here .

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