Navigation

Friday, 9 January 2015

Tracking three colours with Processing

This tutorial explains the coding and algorithm to track three colours (red, green and blue) with Processing Language.



import processing.video.*;

Capture video;

void setup() {
  size(640, 480);
  // Uses the default video input, see the reference if this causes an error
  video = new Capture(this, width, height);
  video.start();  
  noStroke();
  smooth();

}

void draw() {
  if (video.available()) {
    video.read();
    image(video, 0, 0, width, height); // Draw the webcam video onto the screen
    
    int pixelRed = 0;   //value of red pixel
    int pixelRed_g = 0;
    int pixelRed_b = 0;
    int redX = 0;           // X-coordinate of the red video pixel
    int redY = 0;           // Y-coordinate of the red video pixel
    int reddest = 0;        // the brightest red pixel in the video
    int reddest_g = 64;
    int reddest_b = 64;
    
    int pixelGreen = 0;  //value of green pixel
    int pixelGreen_r = 0;
    int pixelGreen_b = 0;
    int greenX = 0;          // X-coordinate of the green video pixel
    int greenY = 0;          // Y-coordinate of the green video pixel
    int greenest = 0;        // the brightest green pixel in the video
    int greenest_r = 64;
    int greenest_b = 64;

    int pixelBlue = 0;  //value of blue pixel
    int pixelBlue_r = 0;
    int pixelBlue_g = 0;
    int blueX = 0;          // X-coordinate of the blue video pixel
    int blueY = 0;          // Y-coordinate of the blue video pixel
    int bluest = 0;         // the brightest blue pixel in the video
    int bluest_r = 64;
    int bluest_g = 64;
    
    float brightestValue = 0; // Brightness of the brightest video pixel
    // Search for the brightest pixel: For each row of pixels in the video image and
    // for each pixel in the yth row, compute each pixel's index in the video
    video.loadPixels();
    
    int index = 0;
    for (int y = 0; y < video.height; y++) {
      for (int x = 0; x < video.width; x++) {
        // Get the color stored in the pixel
        color pixelValue = video.pixels[index];
        
        // Extract the red, green, and blue components of the current pixel's color
        int currR = (pixelValue >> 16) & 0xFF;
        int currG = (pixelValue >> 8) & 0xFF;
        int currB = pixelValue & 0xFF;
      
        // If that value is red, find the brightest pixel and store its (x,y) location 
        
        if ( (currR > 128) && (currG < 64) && (currB < 64) ) {
          pixelRed = currR; pixelRed_g = currG; pixelRed_b = currB;
          if ((pixelRed > reddest) && (pixelRed_g < reddest_g) && (pixelRed_b < reddest_b)) {
            reddest = pixelRed; reddest_g = pixelRed_g; reddest_b = pixelRed_b;
            redY = y;
            redX = x;
          }
        }
        
        
        // If that value is green, find the brightest pixel and store its (x,y) location
        if ( (currR < 64) && (currG > 64) && (currB < 64) ) {
          pixelGreen = currG; pixelGreen_r = currR; pixelGreen_b = currB;
          if ((pixelGreen > greenest) && (pixelGreen_r < greenest_r) && (pixelGreen_b < greenest_b)) {
            greenest = pixelGreen; greenest_r = pixelGreen_r; greenest_b = pixelGreen_b;
            greenY = y;
            greenX = x;
          }
        }
        
        // If that value is blue, find the brightest pixel and store its (x,y) location
        
        if ( (currR < 64) && (currG < 64) && (currB > 128) ) {
          pixelBlue = currB; pixelBlue_r = currR; pixelBlue_g = currG;
          if ((pixelBlue > bluest) && (pixelBlue_r < bluest_r) && (pixelBlue_g < bluest_g)) {
            bluest = pixelBlue; bluest_r = pixelBlue_r; bluest_g = pixelBlue_g;
            blueY = y;
            blueX = x;
          }
        }
        
        index++;
      }
    }
    
    // Draw a red circle at the red pixel
    fill(255, 0, 0, 255);
    ellipse(redX, redY, 20, 20);
    
    // Draw a green circle at the green pixel
    fill(0, 255, 0, 255);
    ellipse(greenX, greenY, 20, 20);
    
    // Draw a blue circle at the blue pixel
    fill(0, 0, 255, 255);
    ellipse(blueX, blueY, 20, 20);
    
  }
}

No comments:

Post a Comment