我正确调整图像亮度吗?

时间:2013-11-28 22:58:25

标签: java image-processing language-agnostic processing

我正在尝试做一些我希望很简单的事情:如果源图像的整体亮度超过某个阈值,则更改结果图像的整体亮度。

到目前为止我的逻辑非常简单:

  1. 获得目标最大值之间的差异。亮度和来源 图像整体亮度
  2. 规范差异
  3. 如果差值大于0,则使用归一化亮度差 'dimm'/在结果像素上减去亮度
  4. 我有一个用Processing /(java)编写的非常简单的'runnable'原型来说明这个想法,但编程语言不太重要

    import java.util.Arrays;
    PImage normal,dimmed;
    
    int s  = 40;//preview scale factor
    int w  = 8;//pixels width
    int h  = 8;//pixels height
    int np = w*h;//total number of pixels 
    int b = 255;//current pixel brush brightness
    float nb = 0;//normal image brightness
    float mb = 64;//max brightness
    float db = 0;//dimmed image brightness
    
    void setup(){
      noSmooth();fill(127);
      size(w*s*2,h*s);
      normal = createImage(w,h,RGB);
      dimmed = createImage(w,h,RGB);
    }
    void draw(){
      image(normal,0,0,w*s,h*s);
      image(dimmed,w*s,0,w*s,h*s);
      text("original brightness: "+nb+" max: " + mb +"\ndimmed brightness: " + db,10,15);
    }
    void mouseDragged(){
      if(mouseX < w*s){
        normal.set(mouseX/s,mouseY/s,color(b));
        //average brightness
        nb = getBrightness(normal);
        //dimm if needed
        float bd   = (nb-mb)/255.0;//normalized brightness difference
        println(nb-mb+"/"+bd);
        for(int i = 0 ; i < np; i++)
          dimmed.pixels[i] = color(brightness(normal.pixels[i]) - (bd > 0 ? bd * 255.0 : 0));//copy brightness and subtract
        dimmed.updatePixels();
        //done dimming, compare
        db = getBrightness(dimmed);
      }
    }
    void keyPressed(){
      if(key == '-' && b > 0) b--;
      if(key == '=' && b < 255) b++; 
      if(key == ' ') {Arrays.fill(normal.pixels,0);normal.updatePixels();}
    }
    float getBrightness(PImage img){
      float ab = 0;
      for(int i = 0 ; i < np; i++) ab += brightness(img.pixels[i]);
      return (ab/np);
    }
    

    我的逻辑是否有意义,还是应采取不同的方法?我也正确地在代码中应用该逻辑。我有点怀疑,因为有时候产生的图像的整体亮度高于目标/最大亮度,我不知道为什么。

    任何提示?

2 个答案:

答案 0 :(得分:0)

如果你想改变亮度,你可以使用tint(b)一个参数:

  • b = 255(原始图片)

  • b = 0(暗图)

在代码中,您只需更改draw()

即可
void draw(){
  image(normal,0,0,w*s,h*s);
  tint(b);
  image(dimmed,w*s,0,w*s,h*s);     
}

答案 1 :(得分:0)

部分问题可能是如何处理Processing中的颜色。 这是一个修改器版本,只使用一个2d数组的int(亮度值):

int s  = 40;//preview scale factor
int w  = 8;//pixels width
int h  = 8;//pixels height
int np = w*h;//total number of pixels 
int b = 255;//current pixel brush brightness
float nb = 0;//normal image brightness
float mb = 64;//max brightness
float db = 0;//dimmed image brightness

int[][] normal,dimmed;

void setup(){
  noSmooth();fill(127);
  size(w*s*2,h*s);
  normal = new int[w][h];
  dimmed = new int[w][h];
}
void draw(){
  background(0);//clear
  for(int y = 0; y  < h ; y++){
    for(int x = 0; x < w ; x++){
      fill(normal[x][y]);
      rect(x*s,y*s,s,s);//draw a scaled pixel representation
      fill(dimmed[x][y]);
      rect((x*s)+(w*s),y*s,s,s);
    }
  }
  fill(127);text("original brightness: "+nb+" max: " + mb +"\ndimmed brightness: " + db,10,15);
}
void mouseDragged(){
  if((mouseX > 0 && mouseX < w*s)&&(mouseY > 0 && mouseY < h*s)){
    normal[mouseX/s][mouseY/s] = b;
    //average brightness
    nb = getBrightness(normal);
    //dimm if needed
    float bd   = (nb-mb)/255.0;//normalized brightness difference
    println(nb-mb+"/"+bd);

    for(int y = 0; y  < h ; y++)
      for(int x = 0; x < w ; x++)
        dimmed[x][y] = normal[x][y] - (int)(bd > 0 ? bd * 255.0 : 0);
    //done dimming, compare
    db = getBrightness(dimmed);
  }
}
void keyPressed(){
  if(key == '-' && b > 0) b--;
  if(key == '=' && b < 255) b++; 
  if(key == ' ') {
    for(int y = 0; y  < h ; y++)
      for(int x = 0; x < w ; x++)
        normal[x][y] = 0;
  }
}
float getBrightness(int[][] img){
  float ab = 0;
  for(int y = 0; y  < h ; y++)
    for(int x = 0; x < w ; x++)
      ab += img[x][y];
  return (ab/np);
}