Loading...
Logo
Processing Forum

Level color program

in Programming Questions  •  4 months ago  
Hello everyone I am a student of Communication Sciences in Cagliari, Italy. I'm new to the forum and I could use help with this exercise for an examination of the faculty:


Write a sketch that shows the effect of the number of color levels on the quality
image. To simulate the decrease in the number of levels, the sketch must introduce a
separation between one level and the next one. To achieve this, you must perform the
following steps in the following order:
1. Write the function (LeggiImmagine) which reads an image suggesting the user
window to select the file that contains it and charge it in an object PImage. the
function has no arguments and returns as a result the object Pimage.
2. As from the start of the first operation, the sketch must read the image
(by invoking the function defined in point 1) and display it.
3. Write a function (ConvertiLivelli (n, m)) that takes as parameters two numbers
integers between 2 and 256. The function transforms the original image in which each color
key is a number between 0 and (n-1), in an image in which each fundamental color is
a number between 0 and (m-1). To do so we must make the following proportion:
livello_di_colore livello_di_colore_originale * = n / m
You must do this for all levels of color intensity of all pixels in the image.
4. Write the function (Levels ()) that applies twice the function defined in ConvertiLivelli
point 3 consecutive twice: the first time with n = 3 and m = 256, the second to values
reversed, n = 256 and m = 3.
5. When the user presses any key on the sketch displays the modified image with the
Levels function defined in section 4 above if the original image was displayed
and vice versa displays the original image if the image was displayed previously
changed.

for now I could only write this:

PImage immagineSorgente;
PImage immagineDestinazione;

void LeggiImmagine() {
  // carico l'immagine da disco e creo l'oggetto PImage immagineSorgente 
  String nomeImmagineSorgente=selectInput("Scegli l'immagine da elaborare");
  immagineSorgente = loadImage(nomeImmagineSorgente);
  size(immagineSorgente.width, immagineSorgente.height);
  // mostro l'immagine caricata
  image(immagineSorgente, 0, 0);
}

void setup() {
  LeggiImmagine();
}



void keyPressed() {
  immagineDestinazione = CovertiLivelli PImage (immagineSorgente);
  // la mostro
  image(immagineDestinazione, 0, 0);
  save("ConvertiLivelli.jpg");
}


  
void draw() {
  
}

PImage ConvertiLivelli(PImage immagineSorgente) {

  // immagine elaborata - stesse dimensioni dell'immagine in ingresso
  // image_out verrà restituita al programma chiamante
  PImage immagineDestinazione =createImage(imageSorgente.width, imageSorgente.height, RGB);
  
  int locazionelineare;  // questa variabile conterrà la posizione (lineare) del pixel
  
  color NuovoColoreDelPixel;
  
  float r; // questa variabile conterrà il valore del canale 'rosso' per il singolo pixel
  float g; // questa variabile conterrà il valore del canale 'verde' per il singolo pixel
  float b; // questa variabile conterrà il valore del canale 'blu' per il singolo pixel 
  // le variabili r, g, b servono solo se voglio accedere ai singoli canali colore
  
  // devo accedere (in lettura)  ai pixel dell'oggetto PImage immagineSorgente
  immagineSorgente.loadPixels();
  
  // devo accedere (in scrittura)  ai pixel dell'oggetto PImage immagineDestinazione
  immagineDestinazione.loadPixels();
  
  for (int y = 0; y < height; y++ ) { 
    for (int x = 0; x < width; x++ ) {
      
      // calcolo la posizione (lineare) del pixel
      locazionelineare = x + y*width;
      
      // QUI INSERISCO IL CODICE CHE ESAMINA IL PIXEL PRESENTE IN
      // immagineSorgente.pixels[locazionelineare]
      // E DETERMINA IL NUOVO VALORE DEL PIXEL
      r = red(immagineSorgente.pixels [locazionelineare]);
      g = green(immagineSorgente.pixels [locazionelineare]);
      b = blue(immagineSorgente.pixels [locazionelineare]);
      
      // ... e poi ricreo il colore
      NuovoColoreDelPixel=color (r,g,b);
      
      // copio il colore del pixel dalla variabile all'oggetto PImage immagineDestinazione
      immagineDestinazione.pixels[locazionelineare] = NuovoColoreDelPixel;
    }
  }
  
  // ora devo aggiornare l'immagine in immagineDestinazione.
  immagineDestinazione.updatePixels();
  
  // image_out viene restituita al programma chiamante
  return image_out;
}


thank you in advance

Replies(4)



you named 5 steps

how far are you? Which step can't you do?

Also it's hard because you don't write in English in your program


step 5
For step 5 please make a flag (with global scope) that tells showOriginalImage
true or false.
  • in draw() if showOriginalImage show original, else show the modified image (see below)
  • in keyPressed() just say
    Copy code
    1. showOriginalImage = !showOriginalImage;


step 1
(Not necessary: Also for step 1 make a flag imageHasBeenLoadedOk)



At the end of setup()
Your setup().....
Copy code
  1. immagineDestinazione = ConvertiLivelli (immagineSorgente);


Your draw()
Your draw() could be this...
Copy code
  1. if (showOriginalImage) {
  2.   image(immagineSorgente, 0, 0);
  3. }
  4. else
  5. {
  6.   image(immagineDestinazione, 0, 0);
  7. }



The program I wrote is definitely all wrong! I would develop all the steps! Better if starting from the beginning! Unfortunately I do not know move at all well with this programming language!
Thanks in advance.
I have corrected the functions named in Italian. Now are indicated in bold.

Write a sketch that shows the effect of the number of color levels on the quality
image. To simulate the decrease in the number of levels, the sketch must introduce a
separation between one level and the next one. To achieve this, you must perform the
following steps in the following order:
1. Write the function ( ReadImage) which reads an image suggesting the user
window to select the file that contains it and charge it in an object PImage. the
function has no arguments and returns as a result the object Pimage.
2. As from the start of the first operation, the sketch must read the image
(by invoking the function defined in point 1) and display it.
3. Write a function ( ConvertLevels (n, m)) that takes as parameters two numbers
integers between 2 and 256. The function transforms the original image in which each color
key is a number between 0 and (n-1), in an image in which each fundamental color is
a number between 0 and (m-1). To do so we must make the following proportion:
level_color = original_level_color *  n / m
You must do this for all levels of color intensity of all pixels in the image.
4. Write the function ( Levels ()) that applies twice the function defined in ConvertLevels
point 3 consecutive twice: the first time with n = 3 and m = 256, the second to values
reversed, n = 256 and m = 3.
5. When the user presses any key on the sketch displays the modified image with the
Levels function defined in section 4 above if the original image was displayed
and vice versa displays the original image if the image was displayed previously
changed.




Hello renzone,

ok, I see your program above. I don't think, the steps are all wrong.

I also gave a lot of advise.
Please build my advice in your program, then tell us what doesn't work and post your new entire program.

Thank you!

Did you read the reference and tutorials on http://processing.org ?


Greetings, Chrisir