6.5 Video Sampling + Physics

 

import processing.video.*;
import gab.opencv.*;

import fisica.*;

FWorld world;
FPoly poly;


//list to store the contours for each object
ArrayList contours;
ArrayList   rigid;

///declare OpenCV object
OpenCV opencv;

//video capture opject
Capture cam;

PGraphics image_holder;
PImage bwim,conim;

int w = 960;
int h = 720;


void setup() {
  size(960, 720);

  String[] cameras = Capture.list();

  // The camera can be initialized directly using an 
  // element from the array returned by list():
  //you can also set a custom resolution
  cam = new Capture(this, 960,720,cameras[0]);
  cam.start();   
  
  image_holder = createGraphics(w, h);
  
  Fisica.init(this);

  world = new FWorld();
  world.setGravity(0, 100);
}

void draw() {
  
  //check to see if the camera is available 
  //before you read an image ftom it
  if (cam.available() == true) {
    cam.read();
  }
  
  
  //load the image into the image holder 
  //this allows you to analyze the image even
  //if you are not displaying it
  image_holder.beginDraw();
  image_holder.image(cam, 0, 0);
  image_holder.endDraw();
 
  //initialize the OpenCv object and use the capture
  //object to retrive the image form the camera
  opencv = new OpenCV(this, cam);
  opencv.loadImage(cam);
  
  opencv.gray();
  bwim = opencv.getSnapshot();
  opencv.threshold(90);
  conim = opencv.getSnapshot();

  contours = opencv.findContours();
  
  //display the various image adjustments from OpenCV
  image(image_holder, 0, 0);

  
  noFill();
  
  //the outer loop gets each contour from the list
  //of detected contours
  rigid = new ArrayList ();
  for(int i = 0; i < contours.size(); i++){

    Contour contour = contours.get(i);
    
    strokeWeight(3);
    stroke(0, 255, 0);
   
    
    //this sets the resolution of the polygon
    //that descirbes each contourbefore you
    //put the points into a list for the individual contour
    contour.setPolygonApproximationFactor(8);
    ArrayList points = contour.getPolygonApproximation().getPoints();
    
    stroke(0);
    strokeWeight(3);
    
    //check to see if the contour is the border of the image
    if(contour.area() < (w*h)-100){
      
      ///draw the polygon
      
      poly = new FPoly();
      poly.setFill(255,0);
      poly.setStroke(255,0);
      poly.setStatic(true);

      for(int j = 0; j < points.size(); j++){
        PVector pt = points.get(j);
        poly.vertex(pt.x,pt.y);
      }
      rigid.add(poly);
      world.add(poly);
      poly = null;
      
    
    }
  }
  
  world.draw();
  world.step();
  
  for(int i = 0; i < rigid.size(); i++){ 
    FPoly b = rigid.get(i); 
    world.remove(b); 
  } 
} 

void mouseClicked() { 
  float sz = random(20,50); 
  FCircle b = new FCircle(sz); 
  b.setPosition(mouseX, mouseY); 
  b.setVelocity(random(-100,100), -40);
  b.setRestitution(0.7); 
  b.setDamping(0.01); 
  b.setNoStroke(); 
  b.setFill(random(255), random(255), random(255)); world.add(b); 
} 

void contactEnded(FContact c) { 
  if (!c.getBody1().isStatic()) { 
    FCircle b = (FCircle)c.getBody1(); 
    if (b.getSize()>10) {
      b.setSize(b.getSize()*0.98);
    }
  } 

  if (!c.getBody2().isStatic()) {
    FCircle b = (FCircle)c.getBody2();
    if (b.getSize()>10) {
      b.setSize(b.getSize()*0.98);
    }
  }
}