使用图像可视化处理:像素颜色阈值

问题描述 投票:0回答:1

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();
}

主要问题在我的if语句中,不确定是否要逻辑地处理它。

processing visualization
1个回答
0
投票

我不确定这到底要去哪里,但是我可以帮助您找到白色像素。在这里,我只计算了7457个“白色”像素(然后我将它们变成红色,以便可以看到它们的位置并调整阈值,如果要获得更多或更少的像素):

Modified image

当然,这只是概念证明,您应该能够适应自己的需求。

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);
}

简而言之(您也将在评论中阅读此内容),我们根据可以调整的阈值对像素进行计数。为了使事情对您来说更明显,我将“白色”像素着色为红色。您可以根据所看到的方式降低或提高阈值,一旦知道了所需的内容,就可以摆脱颜色的限制。

这里有一个困难,那就是图像不是“黑白”的,而是更大的灰度-这是完全正常的,但是会使您似乎想做的事情变得更困难。您可能必须进行大量修改才能获得您感兴趣的确切比例。如果您在GiMP或其他可以调整对比度和亮度的图像软件中编辑了原始图像,则可能会很有帮助。这有点作弊,但这种策略不能立即起作用。[[可以为您节省了一些工作。

玩得开心!
© www.soinside.com 2019 - 2024. All rights reserved.