Basic Question: Detecting Black Pixels in a Picture.

edited August 2016 in Questions about Code

Hello. I'm new to processing and I decided to start with a simple script: something that allows me to identify black pixels in a picture. I have the code included below and as a checker I included the print command, just to see if the coordinates were moving forward (for loops).

Nothing is printing in my console and nothing is showing in my sketch, what am I doing wrong? Thanks in advance

void setup(){
  PImage myImage = loadImage("test.jpg");
  loadPixels();
  size(myImage.width,myImage.height);

}

void draw(){
  for (int i = 0; i < width; i++) {
    for (int j = 0; j < height; i++){
      color c = get(i,j);
      if (c == color(0)){
        point(i,j);
        print(i+","+j);
      }
    }
  }
}
Tagged:

Answers

  • Answer ✓

    Nevermind, I found the error, problem was I was using i++ for both width and height, I made some changes to the code:

    void setup(){
      size(1920,1080);
      background(255);
      color(0);
    }
    
    void findBlack(){
      PImage myImage = loadImage("test.jpg");
      loadPixels();
      int count =0;
      for (int i = 0; i < width; i++) {
        for (int j = 0; j < height; j++){
    
          print("("+i+","+j+")");
          color c = myImage.get(i,j);
          if (c == color(0)){
    
           rect(i,j,3,3,1);
           saveFrame(count+".png");
           count++;
    
          }
        }
      }
    }
    
    void draw(){
      findBlack();
    
    }
    
  • Answer ✓

    Warning: calling println too much can cause real problems

    Draw is being called up to 60 times a second. You probably don't want this. So use noLoop()

  • Post the changed code. I think there are some serious inefficiencies in your code still, based on the YouTube clip.

  • Also you don't want to load the image OR calculate color(0) Or for every pixel. Try this

    PImage myImage ;
    
    void setup(){
      size(1920,1080);
      myImage = loadImage("test.jpg");  
      background(255);
      color(0);
    }
    
    void findBlack(){
      loadPixels();
      int count =0;
      int fc = color (0);
      for (int i = 0; i < width; i++) {
        for (int j = 0; j < height; j++){
    
          print("("+i+","+j+")");
          color c = myImage.get(i,j);
          if (c == fc){
    
           rect(i,j,3,3,1);
           saveFrame(count+".png");
           count++;
          }
        }
      }
    }
    
    void draw(){
      findBlack();
    }
    
  • Thanks Quark, I'll try that!

    Koogs, Here's my final code, before Quark's comments:

    void setup(){
      size(1920,1080);
      background(255);
      color(0);
    }
    
    void findBlack(){
      PImage myImage = loadImage("test.jpg");
      loadPixels();
      int count =0;
      for (int i = 0; i < width; i=i+3) {
        for (int j = 0; j < height; j=j+3){
    
          color c = myImage.get(i,j);
          if (c == color(0)){
    
           rect(i,j,3,3,1);
           saveFrame(count+".png");
           count++;
    
          }
        }
      }
    }
    
    void draw(){
      findBlack();
    
    }
    
  • edited August 2016

    Also, before reading your previous comments, I went ahead and did some more changes. Now, instead of replacing black pixels with a rect, I'm replacing pixels with fonts and using the original image's color as reference.

        void setup(){
          size(736,732);
          background(255);
          PFont font;
          font = loadFont("CourierNewPS-BoldMT-10.vlw");
          textFont(font);
        }
    
        void findBlack(){
          PImage myImage = loadImage("test.jpg");
          loadPixels();
          int count =0000;
            char batman[] = {'H', 'A', '!', 'H', 'A', '!', 'H', 'A', '!', '*','*'};
                int co = 0;
          for (int j = 0; j < width; j=j+7) {
            for (int i = 0; i < height; i=i+7){
    
    
              color c = myImage.get(i,j);
              if (c != color(255)){
               fill(c);
               textSize(10); 
               text(batman[co],i,j);
               if (co == 10){
                 co = 0;
               }
               else {
                 co++;
               }
               saveFrame("Batman"+count+".png");
               count++;
    
              }
            }
          }
        }
    
        void draw(){
          findBlack();
          noLoop();
        } 
    

    The result: https://drive.google.com/file/d/0BwTV1gIcOedwLXdPUzE0cUQyWUU/view?usp=sharing

Sign In or Register to comment.