简体   繁体   English

Java:拍摄图像并将其变为带有RGB的白色

[英]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. 因此,我正在为我的编程课程做一个小组项目,我们正在制作一个照片编辑程序,该程序的其中一部分是拍摄图像,并使用rgb将其转换为黑白图像。 I was wondering what would be the best value or way in RGB to achieve black and white? 我想知道在RGB中实现黑白的最佳价值或方式是什么?

I would recommend letting the Java 2D library worry about the conversion: 我建议让Java 2D库担心转换:

  • create a greyscale BufferedImage (BufferedImage.TYPE_BYTE_GRAY); 创建一个灰度的BufferedImage(BufferedImage.TYPE_BYTE_GRAY);
  • get a graphics context by createGraphics() 通过createGraphics()获得图形上下文
  • ensure that colour rendering is accurate on that graphics context: call setRenderingHint(RenderingHints.KEY_COLOR_RENDERING, RenderingHints.VALUE_COLOR_RENDER_QUALITY) 确保在该图形上下文上色彩渲染准确:调用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. java.awt.image.ColorConvertOp与灰色目标ColorSpace非常有效。 There's an excellent example here . 这里有一个很好的例子在这里

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

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