Java 这个值显然给了我一个零?
作业要求:“将setLuminance方法重命名为blend,并让它拍摄两张图片作为参数:前景和背景。同时对前景和背景的像素进行排序(假设它们的大小完全相同)修改前景图片像素的亮度。使用背景图片对应像素的亮度作为目标,而不是使用第a部分中的恒定目标亮度。其他一切都可以保持不变。” 这正是我所做的,除非我在getLuminance函数中没有sClip(为了获取三个颜色通道值的平均值),输出将在加载图片之前停止三分之一的时间,因为它表示存在算术问题,不能除以零 我现在的输出是答案的90% 我相信我已经遵循了每一条准则,所以我非常困惑Java 这个值显然给了我一个零?,java,image,image-manipulation,pixels,brightness,Java,Image,Image Manipulation,Pixels,Brightness,作业要求:“将setLuminance方法重命名为blend,并让它拍摄两张图片作为参数:前景和背景。同时对前景和背景的像素进行排序(假设它们的大小完全相同)修改前景图片像素的亮度。使用背景图片对应像素的亮度作为目标,而不是使用第a部分中的恒定目标亮度。其他一切都可以保持不变。” 这正是我所做的,除非我在getLuminance函数中没有sClip(为了获取三个颜色通道值的平均值),输出将在加载图片之前停止三分之一的时间,因为它表示存在算术问题,不能除以零 我现在的输出是答案的90% 我相信
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
公共亮度(){
画面向日葵,大地;
向日葵=新图片();
地球=新图片();
显示=新的PictureDisplay(向日葵);
display.waitForUser();
混合(向日葵、泥土);
display.close();
系统出口(0);
}
专用亮度(像素p){
int r;//像素的红色值
int g;//像素的绿色值
int b;//像素的蓝色值
int v;//平均值
r=p.getRed();
g=p.getGreen();
b=p.getBlue();
v=sClip((r+g+b)/3);
返回v;
}
私人空间融合(图向日葵、图地球){
像素s,e;
int r;//像素的红色值
int g;//像素的绿色值
int b;//像素的蓝色值
while(sunflower.hasNext()){
s=向日葵。下一个();
e=地球。下一个();
r=s.getRed();
g=s.getGreen();
b=s.getBlue();
s、 设置(剪辑((int)(r*(获取亮度(e))/获取亮度(s)));
s、 设置绿色(clip((int)(g*(getLuminance(e))/getLuminance(s)));
s、 setBlue(clip((int)(b*(getLuminance(e))/getLuminance(s));
}
}
专用int剪辑(int val){
如果(val我认为你应该改变:
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);
}