java图像颜色公式

时间:2012-07-19 07:03:26

标签: java colors

我知道灰度的java代码是这样的(0.2126 *红色+ 0.7152 *绿色+ 0.0722 *蓝色( 我想知道是否有人知道我怎么能找到更多种类的着色公式,比如我想让照片老式的方式,更橙色,使其更亮,或更暗......更清晰等等

 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);

3 个答案:

答案 0 :(得分:3)

你提到的旧时尚方式被称为“棕褐色”效果。请查看this question,特别是this answer,其中指出了以下代码段(请注意,我没有编写此代码,只是帮助您找到问题的答案)

/**
*
* @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);
}

答案 1 :(得分:1)

我只想玩数字。

  

更多橙色,

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

  

增加所有因素

  

减少所有因素

  

尖锐

这是一个特定的滤镜,用于比较周围的像素以找到边缘。这不只是玩颜色的问题。

BTW:您应该添加值的上限。即Math.min(255,Math.max(0,value))

答案 2 :(得分:0)

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

enter image description here

算法源代码如下所示。方法 getAttribute()获取用户传递的参数(红色,绿色,蓝色)。方法 getIntComponent0 getIntComponent1 getIntComponent2 获取每个颜色通道(红色,绿色和蓝色)。 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);
        }
    }
}