在 Java 中为图像着色
我正在编写一些代码来用 Java 为图像着色。 基本上我想要做的就是沿着 GIMP 的 colorize 命令进行操作,这样如果我有 BufferedImage 和 Color,我就可以使用给定的颜色对图像进行着色。 有人有什么想法吗? 我目前做这样的事情的最佳猜测是获取 BufferedImage 中每个像素的 RGB 值,并将颜色的 RGB 值与一些缩放因子添加到其中。
I'm working on some code to colorize an image in Java. Basically what I'd like to do is something along the lines of GIMP's colorize command, so that if I have a BufferedImage and a Color, I can colorize the Image with the given color. Anyone got any ideas? My current best guess at doing something like this is to get the rgb value of each pixel in the BufferedImage and add the RGB value of the Color to it with some scaling factor.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
让图像中每个像素的
Y = 0.3*R + 0.59*G + 0.11*B
,然后将它们设置为((R1+Y)/2,(G1+Y) /2,(B1+Y)/2)
如果
(R1,G1,B1)
是您要着色的内容。Let
Y = 0.3*R + 0.59*G + 0.11*B
for each pixel in the image, then set them to be((R1+Y)/2,(G1+Y)/2,(B1+Y)/2)
if
(R1,G1,B1)
is what you are colorizing with.我从未使用过 GIMP 的 colorize 命令。 但是,如果您获取每个像素的 RGB 值并向其添加 RGB 值,您实际上应该使用 LookupOp。 这是我编写的一些代码,用于将 BufferedImageOp 应用于 BufferedImage。
使用上面的尼克斯示例,我将如何做到这一点。
它创建一个 BufferedImageOp如果 mask 布尔值为 true,将屏蔽每种颜色。
调用起来也很简单。
如果这不是您想要的,我建议您更多地研究 BufferedImageOp。
这也会更有效,因为您不需要在不同的图像上多次进行计算。 或者只要 R1、G1、B1 值不变,就在不同的 BufferedImage 上重新进行计算。
I have never used GIMP's colorize command. However, if your getting the RGB value of each pixel and adding RGB value to it you should really use a LookupOp. Here is some code that I wrote to apply a BufferedImageOp to a BufferedImage.
Using Nicks example from above heres how I would do it.
It creates a BufferedImageOp that will mask out each color if the mask boolean is true.
Its simple to call too.
If this is not what your looking for I suggest you look more into BufferedImageOp's.
This is would also be more efficient since you would not need to do the calculations multiple times on different images. Or do the calculations over again on different BufferedImages as long as the R1,G1,B1 values don't change.
这与 GIMP 中的 Colorize 功能完全相同,并且保留了透明度。 我还添加了一些内容,例如对比度和亮度、色调、饱和度和亮度 - 0circle0 Google Me --> '精灵创造者3'
This works exactly like the Colorize function in GIMP and it preserves the transparency. I've also added a few things like Contrast and Brightness, Hue, Sat, and Luminosity - 0circle0 Google Me --> ' Sprite Creator 3'
我想要做与问题发布者想要做的完全相同的事情,但是上面的转换并没有像 GIMP 那样删除颜色(即绿色与红色叠加产生令人不愉快的棕色等)。 所以我下载了 GIMP 的源代码并将 c 代码转换为 Java。
将其发布在此线程中,以防万一其他人也想做同样的事情(因为这是 Google 中出现的第一个线程)。 转换仍然会改变白色,而它不应该改变,这可能是从 double 到 int 的转换问题。 该类就地转换 BufferedImage。
I wanted to do the exact same thing as the question poster wanted to do but the above conversion did not remove colors like the GIMP does (ie green with a red overlay made an unpleasant brown color etc). So I downloaded the source code for GIMP and converted the c code over to Java.
Posting it in this thread just in case anyone else wants to do the same (since it is the first thread that comes up in Google). The conversion still changes the white color when it should not, it's probably a casting issue from double to int. The class converts a BufferedImage in-place.