0

Image to be manipulated, hoping to identify each white dot on each picture with a counter

PImage blk; 
void setup() {
  size(640, 480);   
  blk=loadImage("img.png");
}  

void draw () {
  loadPixels();
  blk.loadPixels();
  int i = 0;
  for (int x = 0; x < width; x++) {
    for (int y = 0; y < height; y++) {
      int loc = x+y*width; 
      pixels [loc] = blk.pixels[loc];
      if (blk.pixels[loc] == 0) {
        if (blk.pixels [loc]+1 != 0) {
          i++;
        }
      }

      float r = red(blk.pixels[loc]);
      float g = green(blk.pixels[loc]);
      float b = blue(blk.pixels[loc]); 

      pixels [loc] = color(r, g, b);
    }
  }
  System.out.println (i);
  updatePixels();
}

The main problem is within my if statement, not sure to approach it logically.

3
  • So... you want to count the white pixels, nothing more? Just making sure I get it.
    – laancelot
    Commented Jun 14, 2020 at 19:26
  • I hope to change the color of the pixels to represent statistics affecting the black community but first I want to only get the specific white pixels. (BTW The white pixels are from an Xbox 360 infrared camera). Commented Jun 14, 2020 at 19:46
  • Thank you for being willing to help me! Commented Jun 14, 2020 at 20:52

1 Answer 1

0

I'm unsure where this is exactly going, but I can help you find the white pixels. Here, I just counted 7457 "white" pixels (then I turned them red so you can see where they are and adjust the threshold if you want to get more or less of them):

Modified image

Of course, this is just a proof of concept which you should be able to adapt to your needs.

PImage blk; 
void setup() {
  size(640, 480);   
  blk=loadImage("img.png");
  blk.loadPixels();

  int whitePixelsCount = 0;

  // I'm doing this in the 'setup()' method because I don't need to do it 60 times per second
  // Once it's done once I can just use the image as modified unless you want several
  // different versions (which you can calculate once anyway then store in different PImages)
  for (int i = 0; i < blk.width * blk.height; i++) {
    float r = red(blk.pixels[i]);
    float g = green(blk.pixels[i]);
    float b = blue(blk.pixels[i]);

    // In RGB, the brightness of each color is represented by it's intensity
    // So here I'm checking the "average intensity" of the color to see how bright it is
    // And I compare it to 100 since 255 is the max and I wanted this simple, but you can 
    // play with this threshold as much as you like
    if ((r+g+b)/3 > 100) {
      whitePixelsCount++;
      // Here I'm making those pixels red so you can see where they are.
      // It's easier to adjust the threshold if you can see what you're doing
      blk.pixels[i] = color(255, 0, 0);
    }
  }

  println(whitePixelsCount);
  updatePixels();
}  

void draw () {
  image(blk, 0, 0);
}

In short (you'll read this in the comments too), we count the pixels according to a threshold we can adjust. To make things more obvious for you, I colored the "white" pixels red. You can lower or raise the threshold according to what you see this way, and once you know what you want you can get rid of the color.

There is a difficulty here, which is that the image isn't "black and white", but more greyscale - which is totally normal, but makes things harder for what you seem to be trying to do. You'll probably have to tinker a lot to get to the exact ratio which interests you. It could help a lot if you edited the original image in GiMP or another image software which lets you adjust contrast and brightness. It's kinda cheating, but it it doesn't work right off the bat this strategy could save you some work.

Have fun!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.