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.

Color Tracker I

Februar 29, 2012

Dieses Processing Programm versucht einer Farbfläche zu folgen die zuvor durch einen Klick festgelegt wurde.
import processing.video.*;
import java.awt.Rectangle;

int zone = 3;
int treshold = 20;

Capture video_pre;
PImage video;
color trackColor;
ArrayList matches;
ArrayList trackers;
boolean active = false;
Tracker bigestTracker;

void setup() {
  size(640,480);
  video_pre = new Capture(this,width,height,"IIDC FireWire Video",15);
  video = new PImage(width,height);
  trackColor = color(255,0,0);
  smooth();
}

void draw() {
  if (video_pre.available()) {
    video_pre.read();
    video_pre.loadPixels();
    video.loadPixels();
    mirror();
    video.updatePixels();
    if(active) {
      trackColor();
      image(video,0,0);
      drawTracker();
    } else {
      image(video,0,0);
    }
  }
}

void trackColor()
{
  matches = new ArrayList();

  for (int x = 0; x < video.width; x ++ ) {
    for (int y = 0; y < video.height; y ++ ) {
      int loc = x + y*video.width;
      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);
      float d = dist(r1,g1,b1,r2,g2,b2);

      if (d <= treshold) {
        matches.add(loc);
      }
    }
  }

  trackers = new ArrayList();

  for(int i=0; i < matches.size(); i++) {

    int loc = (Integer)matches.get(i);
    PVector pos = toCord(loc);
    boolean found = false;

    for(int ii=0; ii < trackers.size(); ii++) {
      Tracker tracker = (Tracker)trackers.get(ii);
      if(tracker.eatPixel(pos)) {
        found = true;
        break;
      }
    }

    if(!found) {
      trackers.add(new Tracker(pos));
    }
  }

  for(int i=0; i < trackers.size(); i++) {
    Tracker tracker = (Tracker)trackers.get(i);
    for(int ii=i+1; ii < trackers.size(); ii++) {
      Tracker tracker2 = (Tracker)trackers.get(ii);
      if (tracker.eatTracker(tracker2)) {
        trackers.remove(ii);
      }
    }
  }

  if (trackers.size() > 0) {
    bigestTracker = (Tracker)trackers.get(0);
  }

  for(int i=0; i < trackers.size(); i++) {
    Tracker tracker = (Tracker)trackers.get(i);
    if (tracker.size() > bigestTracker.size()) {
      bigestTracker = tracker;
    }
  }

}

PVector toCord(int loc)
{
  int y = int(loc/video.width);
  int x = loc-video.width*y;
  return new PVector(x,y);
}

void drawTracker()
{
  noStroke();
  fill(255,0,0);
  ellipseMode(CENTER);
  ellipse(bigestTracker.midX(),bigestTracker.midY(),35,35);
  stroke(0,0,255);
}

void drawRects()
{
  for(int i=0; i < trackers.size(); i++) {
    Tracker tracker = (Tracker)trackers.get(i);
    noFill();
    stroke(255,0,0);
    rect(tracker.r.x,tracker.r.y,tracker.r.width,tracker.r.height);
    stroke(0,0,255);
  }
}

void mouseReleased() {
  int loc = mouseX + mouseY*video.width;
  trackColor = video.pixels[loc];
  active = true;
}

void mirror() {
  for (int i = 0; i < video_pre.width; i++) {
     for (int j = 0; j < video_pre.height; j++) {
       video.pixels[j*video_pre.width+i] = video_pre.pixels[(video_pre.width -i-1)+j*video_pre.width];

     }
   }
}

class Tracker
{
  Rectangle r;
  Rectangle r2;

  Tracker(PVector pos)
  {
    r = new Rectangle(int(pos.x),int(pos.y),1,1);
    r2 = new Rectangle(r.x,r.y,r.width,r.height);
    r2.grow(zone,zone);
  }

  boolean eatPixel(PVector pos)
  {
    if(r.contains(pos.y,pos.y)) {
      return true;
    }
    else if(r2.contains(pos.x,pos.y)) {
      r.add(pos.x,pos.y);
      r2 = new Rectangle(r.x,r.y,r.width,r.height);
      r2.grow(zone,zone);
      return true;
    }
    return false;
  }

  boolean eatTracker(Tracker tracker)
  {
    if (r.intersects(tracker.r) || tracker.r.intersects(r)) {
      r.add(tracker.r);
      return true;
    }
    return false;
  }

  double size() {
    return r.getWidth()*r.getHeight();
  }

  int midX() {
    return r.x + r.width/2;
  }

  int midY() {
    return r.y + r.height/2;
  }
}