tracking pixels motion and color in video: issue with array of pixels

edited February 2015 in Questions about Code

I just start working with pixels and I don't understand really how all this works, I found some code to help me start the project (track movement based on differencing colors and add a particle at a single position with a tracked color) but now I'm stuck. If you could, please, give me some keys to continue with this code.

Basically, I want to track the colors of a pixels' moving object on a still background, and then, add particles at the position of that pixels with its particular colors.

` import processing.video.*; import toxi.geom.*; import toxi.physics2d.*; import toxi.physics2d.behaviors.*;

int NUM_P = 1000;            // number of particles displayed
//float closestDiff = 5000; 
// int []movX=0;
// int []movY=0;
int[]previousFrame;          // video buffer
Movie mov;
VerletPhysics2D physics;
//color trackColor;

void setup() {
  size(640, 360, P2D);
  smooth();
//  trackColor = color(255, 0, 0);

mov = new Movie(this, "Sanstitre2.mp4");
mov.loop();
mov.frameRate(10);

int numPixels = mov.width*mov.height;
previousFrame = new int[numPixels];
loadPixels();

physics=new VerletPhysics2D();
physics.addBehavior(new GravityBehavior(new Vec2D(0, -0.15f)));
}

void movieEvent(Movie m) {
  m.read();
}

void draw() {
  //mov.play();
  image(mov, 0, 0, width, height);
  mov.loadPixels();

  int movementSum = 0;
for (int x = 0; x < mov.width; x ++ ) {
  for (int y = 0; y < mov.height; y ++ ) {
  int loc = x + y*mov.width;
  color currentColor = mov.pixels[loc];
  color prevColor = previousFrame[loc];

  //extract rgb component from current color
  float currR = (currentColor >> 16) & 0xFF;
  float currG = (currentColor >> 8) & 0xFF;
  float currB = currentColor & 0xFF;

  // extract rgb from previous pixel
  float prevR = (prevColor >> 16) & 0xFF;
  float prevG = (prevColor >> 8) & 0xFF;
  float prevB = prevColor & 0xFF;

  // Save the current color into the 'previous' buffer
  previousFrame[loc] = currentColor;

  // is current color = previous color ?
  float f = dist(prevR, prevG, prevB, currR, currG, currB);

  //compare colors
  float d = dist(prevR, prevG, prevB, trackR, trackG, trackB);

  /* if some pixels'colors change more than 50 {
   1.get the color of the pixels concerned
   2.get position of the pixels concerned
   3.add a particle at the pixels concerned location for each frame
   */
}
}
if (physics.particles.size() < NUM_P) {
  addParticle();
}

if (physics.particles.size()>100) {
  removeParticle();
}

physics.update();

for (VerletParticle2D p : physics.particles) {
  strokeWeight(5);
  stroke(trackColor, 50);
   point(p.x, p.y);
 }
  if (movementSum>0) {
    updatePixels();
  //  println(movementSum);
  }
}
/*void mousePressed() {
 // Save color where the mouse is clicked in trackColor variable
 int loc = mouseX + mouseY*mov.width;
 trackColor = mov.pixels[loc];
 }
 */
void addParticle() {
  VerletParticle2D p = new VerletParticle2D(Vec2D.randomVector().scale(1).addSelf(closestX, closestY));
  physics.addParticle(p);
}
void removeParticle() {
  physics.particles.remove(physics.particles.size()-80);
}

`

Sign In or Register to comment.