Tracking video pixel

edited March 2014 in How To...

Hello,

I am trying to have a ball fall on the person(dark pixel) in the video, stop there, and follow that dark video pixel. I am not sure if it's even possible. I succeeded at having the ball stop on top of my head in the video and fall further when my head is moved. I need the ball to stay on my head though, in the same place of the head where it fell. I am trying to go through the video pixels again, look for a dark pixel and if the absolute distance between my ballX,ballY is <2, then jump my ball to that new dark pixel. I have not succeeded in this yet. My code is below. I would really appreciate your advice. Thank you.

import processing.video.*;

Capture video;

int ballY; 
int ballX; 
int threshold; 
int darkX = 0; // X-coordinate of the dark(below threshold) video pixel
int darkY = 0; // Y-coordinate of the dark(below threshold) video pixel
ArrayList<Float> sharedYs;
boolean darkPixelFound = false;

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();
  ballX=200; 
  ballY=0; 
  threshold = 60; 
  sharedYs = new ArrayList<Float>();
}

void draw() {

if (video.available()) {
    video.read();
    image(video, 0, 0, width, height); // Draw the webcam video onto the screen
    // 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;

       // look for dark pixel   
        for (int y = 0; y < video.height; y++) {
          for (int x = 0; x < video.width; x++) {
            int pixelValue = video.pixels[index];
            float pixelBrightness = brightness(pixelValue);
             if (pixelBrightness < threshold) { 
                darkY = y; 
                darkX = x;
                fill(255, 204, 0, 128);
                ellipse(darkX, darkY, 10, 10);
                if(ballY == darkY && ballX == darkX){
                  ballY = darkY;
                  ballX=darkX; 
                  darkPixelFound = true;
                }
              } //  if (pixelBrightness < threshold)
             index++;             
          } // for (int x = 0;
        } // for (int y = 0;

    fill(255,0,0); 
    ellipse(ballX,ballY,20,20);
    if(!darkPixelFound) {
      ballY+=2;
    } 
    else if (darkPixelFound) { 
        // is ballX== darkX && ballY == darkY? If so keep the ball where it is. 
        if(ballX== darkX && ballY == darkY){
          ballY = darkY;
          ballX=darkX; 
        }
        **else{
          int i=0; 
          for (int h = 0; h < video.height; h++) {
          for (int w = 0; w < video.width; w++) {
            int pixelValue = video.pixels[i];
            float pixelBrightness = brightness(pixelValue);
             if (pixelBrightness < threshold && abs(w-ballX) < 2 && abs(h-ballY)<2){ 
               ballX = w; 
               ballY = h; 
             }
           }
          }

          }**
     }
     else{
      darkPixelFound = false; 
     }

  } // if (video.available())
} // draw

Answers

Sign In or Register to comment.