这个值显然给了我一个零?

时间:2015-11-09 05:33:52

标签: java image image-manipulation pixels brightness

分配要求:“将setLuminance方法重命名为混合,并将两张图片作为参数:前景和背景。同时排序前景和背景的像素(假设它们的大小完全相同)前景图片像素的亮度。不像A部分那样使用恒定的TARGET亮度,而是使用背景图片的相应像素的亮度作为目标。其他所有部分都保持不变。“

这正是我所做的,除非我在getLuminance函数中没有sClip(以获取三个颜色通道值的平均值),输出将在加载图片之前停止三分之一,因为它说有一个算术问题,不能除以零。

现在我的输出是答案的90%。

My output is

and this is the given answer

我相信我遵循了每一条准则,所以我很困惑

public Luminance() {
    Picture sunflower, earth;
    sunflower = new Picture();
    earth = new Picture();
    display = new PictureDisplayer(sunflower);
    display.waitForUser();
    blend(sunflower, earth);
    display.close();
    System.exit(0);
}

private int getLuminance (Pixel p) {
   int   r;  // red value of pixel
   int   g;  // green value of pixel
   int   b;  // blue value of pixel
   int   v;  // average
   r = p.getRed();
   g = p.getGreen();
   b = p.getBlue();
   v = sClip((r + g + b)/3);
   return v;
}


private void blend(Picture sunflower, Picture earth) {
    Pixel s,e;
    int   r;  // red value of pixel
    int   g;  // green value of pixel
    int   b;  // blue value of pixel

    while ( sunflower.hasNext() ) {
        s = sunflower.next();
        e = earth.next();
        r = s.getRed(); 
        g = s.getGreen(); 
        b = s.getBlue();
        s.setRed(clip ((int) (r * (getLuminance(e))/getLuminance(s))));
        s.setGreen(clip ((int) (g * (getLuminance(e))/getLuminance(s))));
        s.setBlue(clip ((int) (b * (getLuminance(e))/getLuminance(s))));    
        }
}

private int clip(int val){
    if (val <=255){
        return val;
    }
    else {
        return 255;
    }
}
private int sClip (int val){
    if (val == 0){
        return 1;
    }
    else {
        return val;
    }
}

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {Luminance r = new Luminance();
    // TODO code application logic here

1 个答案:

答案 0 :(得分:0)

我认为你应该改变:

s.setRed(clip ((int) (r *(getLuminance(e))/getLuminance(s))));

例如:

if(getLuminance(s)!=0){
    s.setRed(clip ((int) (r * (getLuminance(e))/getLuminance(s))));
}
else{
    s.setRed(255);
}