在Java中着色图像 [英] Colorizing images in Java

查看:156
本文介绍了在Java中着色图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用一些代码来在Java中着色图片。基本上我想做的是沿着GIMP的colorize命令的行,所以如果我有一个BufferedImage和一个颜色,我可以用给定的颜色着色图像。任何人有任何想法?我目前最好的猜测是这样做是为了获得BufferedImage中每个像素的rgb值,并用一些缩放因子将Color的RGB值添加到它。

解决方案

我从来没有使用过GIMP的colorize命令。但是,如果您获取每个像素的RGB值并添加RGB值,则应该使用 LookupOp 以下是我为BufferedImage应用BufferedImageOp而写的代码。



使用Nicks从上面的例子,我会怎么做。


让Y = 0.3 * R + 0.59 * G + 0.11 * B for
每个像素



(R1,G1,B1)是您要着色的




  protected LookupOp createColorizeOp(short R1,short G1,short B1){
short [] alpha = new short [ 256];
short [] red = new short [256];
short [] green = new short [256];
short [] blue = new short [256];

int Y = 0.3 * R + 0.59 * G + 0.11 * B

for(short i = 0; i <256; i ++){
alpha [i] = i;
red [i] =(R1 + i * .3)/ 2;
green [i] =(G1 + i * .59)/ 2;
blue [i] =(B1 + i * .11)/ 2;
}

short [] [] data = new short [] [] {
red,green,blue,alpha
}

LookupTable lookupTable = new ShortLookupTable(0,data);
return new LookupOp(lookupTable,null);
}

它创建一个 BufferedImageOp 如果掩码布尔值为真, b
$ b

它也很简单。

  BufferedImageOp colorizeFilter = createColorizeOp(R1,G1,B1 ); 
BufferedImage targetImage = colorizeFilter.filter(sourceImage,null);

如果这不是你的寻找,我建议你看看BufferedImageOp的更多。



这也会更有效,因为你不需要在不同的图像上多次进行计算。或者在不同的BufferedImages上重新计算,只要R1,G1,B1的值不变。


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.

解决方案

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.

Let Y = 0.3*R + 0.59*G + 0.11*B for each pixel

(R1,G1,B1) is what you are colorizing with

protected LookupOp createColorizeOp(short R1, short G1, short B1) {
    short[] alpha = new short[256];
    short[] red = new short[256];
    short[] green = new short[256];
    short[] blue = new short[256];

    int Y = 0.3*R + 0.59*G + 0.11*B

    for (short i = 0; i < 256; i++) {
        alpha[i] = i;
        red[i] = (R1 + i*.3)/2;
        green[i] = (G1 + i*.59)/2;
        blue[i] = (B1 + i*.11)/2;
    }

    short[][] data = new short[][] {
            red, green, blue, alpha
    };

    LookupTable lookupTable = new ShortLookupTable(0, data);
    return new LookupOp(lookupTable, null);
}

It creates a BufferedImageOp that will mask out each color if the mask boolean is true.

Its simple to call too.

BufferedImageOp colorizeFilter = createColorizeOp(R1, G1, B1);
BufferedImage targetImage = colorizeFilter.filter(sourceImage, null);

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.

这篇关于在Java中着色图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆