Color tracking using opencv in processing

edited February 2018 in Kinect

hey, I´m trying to track 2 different color blobs on a monotone background color in a live webcam feed. I'm having ifficulties figuring out how to go about this. Basicly It needs to mark the two different blobs with an overlay of some sort. later I need to figure out how to let them interact with each other but thats a problem for another day. any tips/ example code?

Answers

  • /**
     * Brightness Tracking 
     * by Golan Levin. 
     *
     * Tracks the brightest pixel in a live video signal. 
     */
    
    
    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 brightestX = 0; // X-coordinate of the brightest video pixel
        int brightestY = 0; // Y-coordinate of the brightest video pixel
        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
            int pixelValue = video.pixels[index];
            // Determine the brightness of the pixel
            float pixelBrightness = brightness(pixelValue);
            // If that value is brighter than any previous, then store the
            // brightness of that pixel, as well as its (x,y) location
            if (pixelBrightness > brightestValue) {
              brightestValue = pixelBrightness;
              brightestY = y;
              brightestX = x;
            }
            index++;
          }
        }
        // Draw a large, yellow circle at the brightest pixel
        fill(255, 204, 0, 128);
        ellipse(brightestX, brightestY, 200, 200);
      }
    }
    
  • Instead of tracking the brightness, you can modify the above to locate the most-color-est (for both of your two colors) pixels.

Sign In or Register to comment.