Unwanted randomization

edited November 2014 in Library Questions

My partner and I are currently working on a high school project that requires motion tracking with a camera. We are fairly new to coding, just for reference. So far, the motion tracking is pretty good, however we're having trouble with lines that appear in areas of the screen that don't respond to the camera. We are using an example from the flob-25 library (downloaded from s373) called flob_parts, but have changed it up a little for our project. We are not using a kinect, we are using opengl with the flob library instead of opencv. The webcam is built into a mac.

We think it has to do with:

force = random(0.5)<0.1?random(-0.72,0.2): random(-2,2);

But we're not sure and would like to know how to draw only in response to the user's movement.

Thank you!

Here's the code:

/**

  flob part sys example
  atypical particle system influenced by blobs positions
  each particle has its own force towards all blobs
  André Sier 2010

*/

import processing.opengl.*;
import processing.video.*;
import s373.flob.*;

Capture video;   
Flob flob;       
ArrayList blobs = new ArrayList(); 
PImage videoinput;

PSys psys;

int tresh = 5;//12;   // adjust treshold value here or keys t/T
int fade = 15;
int om = 1;
int videores=128;
String info="";
PFont font;
float fps = 60;
int videotex = 0; //case 0: videotex = videoimg;//case 1: videotex = videotexbin; 
//case 2: videotex = videotexmotion//case 3: videotex = videoteximgmotion;

void setup() {
  background(0);
  size(1024,512,OPENGL);
  frameRate(fps);
  rectMode(CENTER);
  // init video data and stream
  video = new Capture(this, 320,240, (int)fps);  
  video.start();

  videoinput = createImage(videores, videores, RGB);

  flob = new Flob(this,videores, videores, width, height);

  flob.setThresh(tresh).setSrcImage(videotex)
  .setBackground(videoinput).setBlur(0).setOm(1).
  setFade(fade).setMirror(true,false);

  font = createFont("monaco",16);
  textFont(font);

  psys = new PSys(15);
  stroke(255);
  strokeWeight(3);
}

void draw() {

  if(video.available()) {
     video.read();
     videoinput.copy(video, 0, 0, 320, 240, 0, 0, videores, videores);
     blobs = flob.calc(flob.binarize(videoinput));
  }

  //I just disabled the code from displaying the image
 // image(flob.getSrcImage(), 0, 0, width, height);

  rectMode(CENTER);

  int numblobs = blobs.size();
  stroke(255,25);
  for(int i = 0; i < numblobs; i++) {
    ABlob ab = (ABlob)flob.getABlob(i); 
    psys.touch(ab);
  }
  stroke(random(255),random(255), random(255));//color
  psys.go();
  psys.draw();
}

float drag = 0.9547;
class Part {
  float x, y, vx, vy, ax, ay;
  float px,py,force;

  Part() {
    x = width;
    y = height;
    vx = height;
    vy = width;
    force = random(0.5)<0.1?random(-0.72,0.2): random(-2,2);
    px = +x;
    py = y;
  }
  void go() {
    vx += ax;
    vy += ay;
    vx *= drag;
    vy *= drag;
    px = x;
    py = y;
    x+=vx;
    y+=vy;
    ax = 0;
    ay = 0;
    bounds();
  }

  void bounds(){
    boolean c = false;
    if(x>width){
      x-=width;
      c = true;
    }
    if(x<0){
      c = true;
      x+=width;
    }
    if(y>height){
      c = true;
      y-=height;
    }
    if(y<0){
      y+=height;
      c = true;
    }
    if(c){
      px = x;
      py = y;
    }
  }

  void draw() {
    line(px,py,x,y);
  }
  void touch(ABlob ab) {
    float dx = ab.cx - x;
    float dy = ab.cy - y;
    float d = sqrt(dx*dx+dy*dy);
    if(d > 0 && d < 150) {
      d = 5.0f/d * force;
      dx *= d;
      dy *= d;
      ax += dx;
      ay += dy;
    }
  }
}


class PSys {
  Part p[];

  PSys (int num) {
    p = new Part[num];
    for(int i=0;i<p.length;i++)
      p[i] = new Part();
  }

  void go() {    
    for(int i=0; i<p.length;i++)
      p[i].go();
  }
  void draw() {
    for(int i=0; i<p.length;i++)
      p[i].draw();
  }
  void touch(ABlob ab) {
    for(int i=0; i<p.length;i++)
      p[i].touch(ab);
  }
}
Sign In or Register to comment.