echo effect on live video

edited May 2014 in Questions about Code

I want to create a "echo" effect over a live video from a webcam, my idea is to save some video frames with a PGraphics array and then displaying them with different levels of transparency.

My code is:

import processing.video.*;

Capture cam;
int counterFrame=0;     //counter for the frames to grab
int delay=60;                //number of frames to be repeated
PGraphics f[]=new PGraphics[delay+1];      //array of the stored frames 

void setup() {
  size(640, 480);
  frameRate(30);

//initializing the webcam
  String[] cameras = Capture.list();
  if (cameras.length == 0) {
    println("There are no cameras available for capture.");
    exit();
  } 
  else {
    println("Available cameras:");
    for (int i = 0; i < cameras.length; i++) {
      println(cameras[i]);
    }  
    cam = new Capture(this, 640, 480, "HP Webcam [2 MP Macro]", 30);
    cam.start();
  }

//initializing the array of PGraphics
  for (int i=0; i<delay+1;i++) {
    f[i]= createGraphics(width, height);
  }
}

void draw() {
//I grab the frames necessary to begin the "echo" effect
  if (counterFrame<delay) {
    if (cam.available()) {
      cam.read();
      f[counterFrame].beginDraw();
      f[counterFrame].image(cam, 0, 0);
      f[counterFrame].endDraw();
      counterFrame++;
    }
  }

//once i have the necessary frames, i begin the effect
  if (counterFrame==delay) {
//update the array discarding the older frame
    for (int i=0;i<delay;i++) {
      f[i].copy(f[i+1], 0, 0, f[i+1].width, f[i+1].height, 0, 0, f[i+1].width, f[i+1].height);   
    }
//saving the last frame
    cam.read();
    f[delay].beginDraw();
    f[delay].image(cam, 0, 0);
    f[delay].endDraw();
//display the frames from the newer one  to the oldest with a decreasing trasparency
    for (int i=delay-1;i>=0;i--){
      int alpha=(i*90/delay)+5;
      tint(255, alpha);
      image(f[i], 0, 0);
    }
  }
}

when i run this code i display only one frame and it didn't update. Where am i wrong?

Answers

Sign In or Register to comment.