用Java着色图像

Pau*_*cks 4 java colorize bufferedimage colors

我正在研究一些代码来用Java着色图像.基本上我想做的是GIMP的colorize命令,所以如果我有一个BufferedImage和Color,我可以用给定的颜色着色Image.有人有任何想法吗?我目前做这样的事情的最佳猜测是获取BufferedImage中每个像素的rgb值,并使用一些缩放因子将Color的RGB值添加到其中.

Nic*_*ick 7

Y = 0.3*R + 0.59*G + 0.11*B图像中的每个像素,然后将它们设置为

((R1+Y)/2,(G1+Y)/2,(B1+Y)/2)

如果(R1,G1,B1)你正在着色.


Ber*_*rez 4

我从未使用过 GIMP 的 colorize 命令。但是,如果您获取每个像素的 RGB 值并向其添加 RGB 值,您实际上应该使用LookupOp以下是我编写的一些代码,用于将 BufferedImageOp 应用于 BufferedImage。

使用上面的尼克斯示例,我将如何做到这一点。

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

(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);
}
Run Code Online (Sandbox Code Playgroud)

它创建一个BufferedImageOp,如果 mask 布尔值为 true,它将遮盖每种颜色。

调用起来也很简单。

BufferedImageOp colorizeFilter = createColorizeOp(R1, G1, B1);
BufferedImage targetImage = colorizeFilter.filter(sourceImage, null);
Run Code Online (Sandbox Code Playgroud)

如果这不是您想要的,我建议您更多地研究 BufferedImageOp。

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