how to get RGB values of an image
in
Programming Questions
•
2 years ago
Hi!
I am trying get the rgb values of an image and to display them like the PS curves.
I found this code(1.code) below somewhere. The problem here is that, when i run the code in draw() modus the lines moving, but it shouldnt because its a static picture and the rgb values are still the same... and i wanna use it later for to get the values of a movie.
Anyway, how to get the total red value of an image? If i am using a loop and extract the red value it seems just to take the value from the very last pixel of the image (width,height), but it does not count every pixel of the whole image together... (2. code)
I am trying get the rgb values of an image and to display them like the PS curves.
I found this code(1.code) below somewhere. The problem here is that, when i run the code in draw() modus the lines moving, but it shouldnt because its a static picture and the rgb values are still the same... and i wanna use it later for to get the values of a movie.
Anyway, how to get the total red value of an image? If i am using a loop and extract the red value it seems just to take the value from the very last pixel of the image (width,height), but it does not count every pixel of the whole image together... (2. code)
- //1.CODE
- PImage img;
- void setup(){
- size(200, 200);
-
- img = loadImage("girl.png");
- }
- int[] histBright = new int[256];
- int[] histR = new int[256];
- int[] histG = new int[256];
- int[] histB = new int[256];
-
-
- void draw(){
- image(img, 0, 0);
- for (int i = 0; i < img.width; i++) {
- for (int j = 0; j < img.height; j++) {
- int bright = int(brightness(get(i, j)));
- int r = int(red(get(i, j))); // gets values
- int g = int(green(get(i, j)));
- int b = int(blue(get(i, j)));
- histBright[bright]++; //??
- histR[r]++;
- histG[g]++;
- histB[b]++;
- }
- }
-
- int histBrightMax = max(histBright);
- int histRMax = max(histR);
- int histGMax = max(histG);
- int histBMax = max(histB);
-
-
- stroke(255);
- for (int i = 0; i < img.width; i += 3) {
- int which = int(map(i,0, img.width, 30, 255));
- int yBright = int(map(histBright[which], 0, 1500, img.height, 0));
- int yR = int(map(histR[which], 0, 1000, img.height, 0));
- int yG = int(map(histG[which], 0, 1000, img.height, 0));
- int yB = int(map(histB[which], 0, 1000, img.height, 0));
- stroke(255);
- line(i, img.height, i, yBright);
- stroke(255,0,0);
- line(i+1, img.height, i+1, yR);
- stroke(0,255,0);
- line(i+2, img.height, i+2, yG);
- stroke(0,0,255);
- line(i+3, img.height, i+3, yB);
-
- }
- }//1.CODE END
- //2.CODE
- PImage img;
- float r,g,b;
- float X,Y;
-
- void setup() {
- size(243,202);
- img = loadImage("girl.png");
- frameRate(12);
- smooth();
- }
-
- void draw() {
- loadPixels();
-
- // We must also call loadPixels() on the PImage since we are going to read its pixels.
- img.loadPixels();
- for (int y = 0; y < height; y++ ) {
- for (int x = 0; x < width; x++ ) {
- int loc = x + y*width;
- // The functions red(), green(), and blue() pull out the three color components from a pixel.
- r = red(img.pixels [loc]);
- g = green(img.pixels[loc]);
- b = blue(img.pixels[loc]);
-
- // Image Processing would go here
- // If we were to change the RGB values, we would do it here, before setting the pixel in the display window.
-
- // Set the display pixel to the image pixel
- pixels[loc] = color(r,g,b);
- }
- }
- updatePixels();
- stroke(0);
- color LOC = get(width-1, height-1);
- float R = red(LOC);
- float G = green(LOC);
- float B = blue(LOC);
- X = (random(1,width-1));
- Y = (random(1,height-1));
- color LOC2 = get((int)X,(int)Y);
- float R2 = red(LOC2);
- float G2 = green(LOC2);
- float B2 = blue(LOC2);
- // from loop
- rect(10,0, 5,r); rect(15,0, 5,g); rect(20,0, 5,b);
- // last pixel of image(width,height)
- rect(30,0, 5,R); rect(35,0, 5,G); rect(40,0, 5,B);
- // random pixel
- rect(50,0, 5,R2); rect(55,0, 5,G2); rect(60,0, 5,B2);
- noStroke();
- ellipse(X,Y,3,3);
- }
- //2.CODE END
1