terça-feira, 23 de junho de 2015

Exercício 24: Histograma Dos Canais RGB

O código a seguir apresenta os histogramas dos canais rgb de uma imagem, o sódigo é uma adaptação de um código desenvolvido em aula e as adaptações foram armazenar os dados de todos os canais, calcular o tom de maxima aparição em todos os canais, e claro mostrar efetivamente o histograma de cada canal.

Código-Fonte:

int[] tonsr = new int[256];
int[] tonsg = new int[256];
int[] tonsb = new int[256];
PImage img;
int max=1,maxg=1,maxb=1;

void setup(){
 
  size(1000,600);
  background(0);
  //float t=0, mediaT,somaT=0;
 
  int pos,t,g,b;
  img = loadImage("akito.jpg");
  image(img,0,0);
  for(int x=0;x<250;x++){
    for(int y=0;y<231;y++){
      pos=y*250+x;
      t=(int)red(img.pixels[pos]);
      g=(int)green(img.pixels[pos]);
      b=(int)blue(img.pixels[pos]);
      tonsr[t] +=1;
      tonsg[g] +=1;
      tonsb[b] +=1;
     
    }
  }
    for(int i=0;i<tonsr.length;i++){
    println("tom "+i+" = "+tonsr[i]);
  }
   //mediaT = somaT/(640*480);
   max = maxPos(1);
   maxg = maxPos(2);
   maxb = maxPos(3);
  println("Tom "+max+" valor "+tonsr[max]);
  criaHist();
  /*
  float v,SomaV=0;
  for(int x=0;x<640;x++){
    for(int y=0;y<480;y++){
      pos=y*640+x;
      t=red(img.pixels[pos]);
      v= pow(t-mediaT,2);
      SomaV+=v;
     
    }
  }
 
  float media = SomaV/(640*480);
  println("Média de Tons é ",media);
*/
}

int maxPos(int cor){
  int n=0;
  int pos2=0;
  if(cor == 1){
  for(int b=0;b<tonsr.length;b++){
    if(tonsr[b]>=n){
    n=tonsr[b];
    pos2=b;
    }
 
  }
 
  }else if(cor == 2){
    for(int b=0;b<tonsg.length;b++){
    if(tonsg[b]>=n){
    n=tonsg[b];
    pos2=b;
    }
  }
  }else if(cor == 3){
    for(int b=0;b<tonsb.length;b++){
    if(tonsb[b]>=n){
    n=tonsb[b];
    pos2=b;
    }
  }}
 
 
  return pos2;

}

void criaHist(){
 
  float x=5; 
 
  max = maxPos(1);
  for(int i=0;i<tonsr.length;i++){
    println("maximor "+tonsr[max]);
    fill(220,0,0);
    noStroke();
    rect(x,height-((tonsr[i]*100)/tonsr[max]),1,height);
    x+=1;
 
  }
 
 
 
  x+=50;
   maxg = maxPos(2);
  for(int i=0;i<tonsg.length;i++){
    println("maximog "+tonsg[maxg]);
    fill(0,220,0);
    noStroke();
    rect(x,height-((tonsg[i]*100)/tonsg[maxg]),1,height);
    x+=1;
 
  }
 
  x+=50;
   maxb = maxPos(3);
  for(int i=0;i<tonsb.length;i++){
   
    println("tonb"+tonsb[i]+"maximob "+tonsb[maxb]);
    fill(0,0,220);
    noStroke();
    rect(x,height-((tonsb[i]*100)/tonsb[maxb]),1,height);
    x+=1;
 
  }
 
 
}

void draw(){
    //println("X: "+x);
    //background(0);
    //criaHist();

    //image(img,0,0);
 
}

Nenhum comentário:

Postar um comentário