Converting from Capture Video to Movie

Hello all, I have created a camera motion detection and particle trail sketch, which works fine by comparing the pixel colour of the current to previous frame, but I am now trying to convert it so that it works with movies rather than live video. Thus I am replacing all the Capture/Video references with Movie/movie and so on. However, I am still getting error messages and for the life of me I can’t figure out what else I need to change to make it work. I am not very experienced with processing though I am very eager and excited about it, so some feedback towards the right direction would be really appreciated:

Firstly, this is part of the working webcam sketch that actually works:

import processing.video.*;
import java.util.Iterator;

Capture video;
ParticleSystem ps;

// Previous Frame
PImage prevFrame;

// degree of sensitivity to motion detection. 
float threshold;

 float avgX = 0;
 float avgY = 0;
 float motionX = 0;
 float motionY = 0;
 int count = 0;

void captureEvent(Capture video){
  // Save previous frame for motion detection!!
  prevFrame.copy(video, 0, 0, video.width, video.height, 0, 0, video.width, video.height); // Before we read the new frame, we always save the previous frame for comparison!
  prevFrame.updatePixels();  
  
  if(video.available()){
    video.read();
  }
}

void setup() {  
  size(640,480);
  // Pulling the display's density dynamically
  pixelDensity(displayDensity());
  
  video = new Capture(this, width, height); //set up video
  video.start();
  
  ps = new ParticleSystem(new PVector(width/2, height-60)); //create the particle system
  prevFrame = createImage(video.width, video.height, RGB);
}

void draw(){
  video.loadPixels();
  prevFrame.loadPixels();
  
  image(video, 0, 0);
  
  count = 1;
  avgX = 0;
  avgY = 0;
  threshold = 97;
  
  // Begin loop to walk through every pixel
  for (int x = 0; x < video.width; x = x + 4 ) {
    for (int y = 0 ; y < video.height; y = y + 4 ) {

      int loc = x + y*video.width;            // Step 1, what is the 1D pixel location
      color current = video.pixels[loc];      // Step 2, what is the current color
      color previous = prevFrame.pixels[loc]; // Step 3, what is the previous color
      
      // Step 4, compare colors (previous vs. current)
      float r1 = red(current); 
      float g1 = green(current); 
      float b1 = blue(current);
      float r2 = red(previous); 
      float g2 = green(previous); 
      float b2 = blue(previous);
      float diff = dist(r1, g1, b1, r2, g2, b2);

Now this is what I’ve changed it to, but it gives errors:

import processing.video.*;
import java.util.Iterator;

Movie movie;
ParticleSystem ps;

// Previous Frame
PImage prevFrame;

// degree of sensitivity to motion detection. 
float threshold;

 float avgX = 0;
 float avgY = 0;
 float motionX = 0;
 float motionY = 0;
 int count = 0;

void movieEvent(Movie movie){
  // Save previous frame for motion detection!!
  prevFrame.copy(movie, 0, 0, movie.width, movie.height, 0, 0, movie.width, movie.height); // Before we read the new frame, we always save the previous frame for comparison!
  prevFrame.updatePixels();  
  
  if(movie.available()){
    movie.read();
  }
}

void setup() {  
  size(640,480);
  // Pulling the display's density dynamically
  pixelDensity(displayDensity());
  
  movie = new Movie(this, "testmovie.mov"); //set up video
  movie.play();
  
  ps = new ParticleSystem(new PVector(width/2, height-60)); //create the particle system
  prevFrame = createImage(movie.width, movie.height, RGB);
}

void draw(){
  movie.loadPixels();
  prevFrame.loadPixels();
  
  image(movie, 0, 0);
  
  count = 1;
  avgX = 0;
  avgY = 0;
  threshold = 97;
  
  // Begin loop to walk through every pixel
  for (int x = 0; x < movie.width; x = x + 4 ) {
    for (int y = 0 ; y < movie.height; y = y + 4 ) {

      int loc = x + y*movie.width;            // Step 1, what is the 1D pixel location
      color current = movie.pixels[loc];      // Step 2, what is the current color
      color previous = prevFrame.pixels[loc]; // Step 3, what is the previous color
      
      // Step 4, compare colors (previous vs. current)
      float r1 = red(current); 
      float g1 = green(current); 
      float b1 = blue(current);
      float r2 = red(previous); 
      float g2 = green(previous); 
      float b2 = blue(previous);
      float diff = dist(r1, g1, b1, r2, g2, b2);

I get “ArrayIndexOutOfBoundsException: 0” at Step 3.

Thank you in advance for all your help!

1 Like

Well it seems that an array is not initialized with the size you use it. It would help if you added the right location, where the error is happening. What is Step 3 in your case? Edit: Now I saw your comments in the code, but please add proper code tags.

But I assume that your previous frame is not initialized with the correct size. Have you checked if the movie has the right width and height after initiailisation? Using the following code in setup is maybe the problem, because the movie has not seen any frame and so the PImage variables are not set yet.

prevFrame = createImage(movie.width, movie.height, RGB);

Try to move this into the draw call and init the preFrame if it is not already set.

Btw please use proper code tags for the code you paste in here.

2 Likes