Willkommen auf unserem Seminar-Blog

Immer auf dem aktuellen Stand bleiben

Dieser Seminar-Blog befindet sich noch im Aufbau und wird in den kommenden Tagen entsprechend verfeinert.

Member Login

Lost your password?

Registration is closed

Sorry, you are not allowed to register by yourself on this site!

You must either be invited by one of our team member or request an invitation by email at viad.info {at} zhdk {dot} ch.

Motion Tracking

Februar 27, 2012

Nach dem Einlesen der Videodaten in Processing galt es sich mit Color-Tracking vertraut zu machen. Ich habe einen Color-Tracking Punkt integriert, dieser wird mittels farbigen Stickers auf dei Nase geklebt. Im Illustrator habe ich verschieden Schnäuze, Augenbraune und Hüte gezeichnet, die mit den Tasten a, s und d geswitched werden können.
import processing.video.*;

// Object variables -------------------------------------
Capture video;
PShape[] s;
PShape[] a;
PShape[] d;
int indexA = 0;
int indexS = 0;
int indexD = 0;

color trackColor; 

// Setup ------------------------------------------------
void setup() {
  size(320*2,240*2);
  
  video = new Capture(this,width,height,"IIDC FireWire Video",15);
  // Start off tracking for red
  trackColor = color(255,0,0);
  
  // load Shape
  s = new PShape[4];
  s[0] = loadShape("schautzA2.svg");
  s[1] = loadShape("schautzA1.svg");
  s[2] = loadShape("schautzA3.svg");
  s[3] = loadShape("schautzA4.svg");
  
  a = new PShape[2];
  a[0] = loadShape("augenW2.svg");
  a[1] = loadShape("augenW1.svg");
   
  d = new PShape[3];
  d[0] = loadShape("hutD1.svg");
  d[1] = loadShape("hutD2.svg");
  d[2] = loadShape("hutD3.svg");
  shapeMode(CENTER);
  smooth();
}

// Draw --------------------------------------------------
void draw() {
  // Capture and display the video
  if (video.available()) {
    video.read();
  }
  video.loadPixels();
  //pushMatrix();
  //translate(width, 0);
 // scale(-1,1);
  image(video,0,0); 
 // popMatrix();
 

  // Before we begin searching, the "world record" for closest color is set to a high number that is easy for the first pixel to beat.
  float worldRecord = 500; 

  // XY coordinate of closest color
  int closestX = 0;
  int closestY = 0;

  // Begin loop to walk through every pixel
  for (int x = 0; x < video.width; x ++ ) {
    for (int y = 0; y < video.height; y ++ ) {
      int loc = x + y*video.width;
      // What is current color
      color currentColor = video.pixels[loc];
      float r1 = red(currentColor);
      float g1 = green(currentColor);
      float b1 = blue(currentColor);
      float r2 = red(trackColor);
      float g2 = green(trackColor);
      float b2 = blue(trackColor);

      // Using euclidean distance to compare colors
      float d = dist(r1,g1,b1,r2,g2,b2); // We are using the dist( ) function to compare the current color with the color we are tracking.

      // If current color is more similar to tracked color than
      // closest color, save current location and current difference
      if (d < worldRecord) {
        worldRecord = d;
        closestX = x;
        closestY = y;
      }
    }
  }

  if (worldRecord < 10) { 
    // Draw a circle at the tracked pixel
    shape(s[indexS], closestX, closestY, 380, 380);
    shape(a[indexA], closestX, closestY, 380, 380);
    shape(d[indexD], closestX, closestY, 380, 380);
  }
}

// Mouse Pressed -------------------------------------------
void mousePressed() {
  // Save color where the mouse is clicked in trackColor variable
  int loc = mouseX + mouseY*video.width;
  trackColor = video.pixels[loc];
}

void keyPressed()
{
  if(key == 's')
  {
  indexS++;
  }
    if(indexS == s.length) 
    {
      indexS = 0;
  }
  
   if(key == 'a')
  {
  indexA++;
  }
    if(indexA == a.length) 
    {
      indexA = 0;
  }
  
     if(key == 'd')
  {
  indexD++;
  }
    if(indexD == d.length) 
    {
      indexD = 0;
  }
}