1

我知道灰度的java代码是这样的(0.2126 * red + 0.7152 * green + 0.0722 * blue(我想知道是否有人知道我怎样才能找到更多种类的着色公式,比如如果我想以旧时尚的方式制作图片,更橙色,使其更亮或更暗......更清晰等等

 int pixel = image.getRGB(j, i);
 int red = (pixel) & 0xff;
 int green = (pixel >> 8) & 0xff;
 int blue = (pixel >> 16) & 0xff;
 int newPixel = (int) (0.2126 * red  + 0.7152 * green + 0.0722 * blue);
                    image1.setRGB(j, i, newPixel);
4

3 回答 3

3

您提到的旧时尚方式称为“棕褐色”效果。看一下这个问题,尤其是这个答案,它指出了以下代码片段(请注意,我没有编写此代码,只是帮助您找到问题的答案)

/**
*
* @param img Image to modify
* @param sepiaIntensity From 0-255, 30 produces nice results
* @throws Exception
*/
public static void applySepiaFilter(BufferedImage img, int
sepiaIntensity) throws Exception
{
// Play around with this. 20 works well and was recommended
// by another developer. 0 produces a grey image
int sepiaDepth = 20;

int w = img.getWidth();
int h = img.getHeight();

WritableRaster raster = img.getRaster();

// We need 3 integers (for R,G,B color values) per pixel.
int[] pixels = new int[w*h*3];
raster.getPixels(0, 0, w, h, pixels);

// Process 3 ints at a time for each pixel. Each pixel has 3 RGB
colors in array
for (int i=0;i<pixels.length; i+=3)
{
int r = pixels[i];
int g = pixels[i+1];
int b = pixels[i+2];

int gry = (r + g + b) / 3;
r = g = b = gry;
r = r + (sepiaDepth * 2);
g = g + sepiaDepth;

if (r>255) r=255;
if (g>255) g=255;
if (b>255) b=255;

// Darken blue color to increase sepia effect
b-= sepiaIntensity;

// normalize if out of bounds
if (b<0) b=0;
if (b>255) b=255;

pixels[i] = r;
pixels[i+1]= g;
pixels[i+2] = b;
}
raster.setPixels(0, 0, w, h, pixels);
}
于 2012-07-19T07:08:30.807 回答
1

我只会玩数字。

更多的橙色,

更多的红色和更多的绿色(红色+绿色=黄色)

更亮

增加所有因素

较暗

减少所有因素

更锐利

这是比较周围像素以查找边缘的特定过滤器。这不仅仅是玩弄颜色的问题。

顺便说一句:您应该添加值的上限。即 Math.min(255, Math.max(0, value))

于 2012-07-19T07:07:11.003 回答
0

您可以操纵颜色通道之间的比例以改变场景“气氛”。下面的图像是使用ColorChannel插件创建的。

在此处输入图像描述

算法源代码如下。getAttribute()方法获取用户传递的参数(红色、绿色、蓝色)。方法getIntComponent0getIntComponent1getIntComponent2获取每个颜色通道(红色、绿色和蓝色)。方法setIntColor设置每个通道的值。

@Override
public void process
(
    MarvinImage imageIn,
    MarvinImage imageOut,
    MarvinAttributes attrOut,
    MarvinImageMask mask,
    boolean preview
) {

    int vr = (Integer)getAttribute("red");
    int vg = (Integer)getAttribute("green");
    int vb = (Integer)getAttribute("blue");

    double mr = 1+Math.abs((vr/100.0)*2.5);
    double mg = 1+Math.abs((vg/100.0)*2.5);
    double mb = 1+Math.abs((vb/100.0)*2.5);

    mr = (vr > 0? mr : 1.0/mr);
    mg = (vg > 0? mg : 1.0/mg);
    mb = (vb > 0? mb : 1.0/mb);

    int red,green,blue;
    for(int y=0; y<imageIn.getHeight(); y++){
        for(int x=0; x<imageIn.getWidth(); x++){
            red = imageIn.getIntComponent0(x, y);
            green = imageIn.getIntComponent1(x, y);
            blue = imageIn.getIntComponent2(x, y);

            red     = (int)Math.min(red * mr, 255);
            green   = (int)Math.min(green * mg, 255);
            blue    = (int)Math.min(blue * mb, 255);

            imageOut.setIntColor(x, y, 255, red, green, blue);
        }
    }
}
于 2013-11-05T18:57:15.413 回答