Using a Pixels X and Y Coordinates

Hi,

I'm fairly new to programming and I'm currently having some trouble using the pixels[] array. I've written a small formula which works on paper, but requires singular pixel's X and Y coordinates to work.

In essence, I'm trying to gather all pixels X and Y-coordinates and weigh them according to their darkness.

Extracting the average brightness (and reversing it) works just fine:

loadPixels(); for (int i = 0; i < pixels.length; i++) { bri += map(brightness(pixels[i]), 0, 100, 1, 0); } updatePixels();

But when I try to get the X-coordinate and weigh it using this method...

for (int x = 0; x < width; x++) { for (int y = 0; y < height; y++) { pix += (x + 1) * map(brightness(pixels[i]), 0, 100, 1, 0); } updatePixels(); }

It only works with the first X-line of pixels, and even then, it outputs either 0s or other numbers I can't place.

I suspect that I'd have to write something around x + y * width, but I can't quite wrap my head around it.

To summarise: I need every pixel's X and Y-coordinates and that same pixel's brightness.

Thank you for reading! Please post your thoughts; and if I'm missing something extremely obvious as per usual, please point it out!

Tagged:

Answers

  • NOTE: You need to call updatePixels only if you modify the content of the pixel array.

    NOTE: Are you aware of the colorMode()? https://processing.org/reference/colorMode_.html You can select your brightness value to range from 0 to 1 by using colorMode(HSB,1,1,1); and then you don't need to rely on the mapping function.

    NOTE: To format your code in the forum, select your code and hit ctrl+o.

    I need every pixel's X and Y-coordinates and that same pixel's brightness

    Same pixel brightness as defined in your first code block?

    Wouldn't something like this work for you, so to make things simpler and more clear? Code below.

    Kf

    void setup() {
      size(540, 540); 
      stroke(0);
      fill(255);
    }
    
    void draw() {
      background(0,0,255);
      loadPixels();
      float bri =0;
      for (int x = 0; x < width; x++) {
        for (int y = 0; y < height; y++) {      
          bri += brightness(get(x,y));  
          //ANOTHER option:  bri += brightness(pixels[y*width + x]);
        }
      }
    }
    
  • Thanks for the fast response!

    My color mode is (HSB, 100), to get 100 shades of grayscale, the mapping reverses it to be from 1 to 0; 0, being the most bright.

    Thanks for the formatting tip! This was my first post here, I'll use it in the future.

    bri += brightness(get(x,y));

    works just the same as:

    bri += map(brightness(pixels[i]), 0, 100, 1, 0);

    What I really need now, is to access every pixel's X-coordinate.

    for example in a three-by-three pixel grid the x values go like this:

    1 2 3

    1 2 3

    1 2 3

    but in the pixel array there's only one dimension and it goes like this:

    0 1 2

    3 4 5

    6 7 8

    Can I somehow transform this into the X value?

  • Answer ✓

    The way you are calling the loops, you are accessing your matrix by columns. if you do this instead:

    for (int y = 0; y < height; y++) {
      float bri=0;
      for (int x = 0; x < width; x++) {          
          bri += brightness(get(x,y));        
        }
      }
    

    Then bri will hold the values for the whole row and reading one row at the time.

    Kf

  • But when I try to get the X-coordinate and weigh it using this method...

    for (int x = 0; x < width; x++) {
      for (int y = 0; y < height; y++) {
       pix += (x + 1) * map(brightness(pixels[i]), 0, 100, 1, 0);
      }
      updatePixels();
    }
    

    What's i here? If you're iterating over the pixels array using x and y then you can't index it using i like that. At least not without more code.

    Search for 'convolution matrix'

  • for example in a three-by-three pixel grid the x values go like this:

    1 2 3
    1 2 3
    1 2 3
    

    No, they go

    0 1 2
    0 1 2
    0 1 2
    

    x % width will get you the column index. But if you use kf's nested loops you already have x.

  • Post the whole code. It's hard to tell what you're doing from snippets.

  • Thanks for all of the input; turns out I was misunderstanding how the nested loop works. After actually getting it (and understanding that I had the X-coordinates all along), I adapted Kf's second comment like this (wex being the weighted value for each pixel):

    loadPixels();
    
      for (int y = 0; y < height; y++) {
        for (int x = 0; x < width; x++) {
    
          wex += (x + 0.5) * map(brightness(get(x, y)), 0, 100, 1, 0);
        }
      }
    

    Now everything works fine, thanks guys!

Sign In or Register to comment.