IPCapture & Motion Sensing

edited February 2014 in Library Questions


I'm trying to create a sketch that can do a simple motion sensing (frame comparison basically) using the IPCapture Library. The sketch works with the regular Processing Video library and my computer internal webcam, but once I try to make it work with an IP Cam, it gives me errors such as:

ArrayIndexOutOfBoundsException: 0 New frame size: 640x480

And highlights this line of code:

      color previous = prevFrame.pixels[loc]; // Step 3, what is the previous color

After that, I have to force quit the program and it will give me this warning:

Could not run the sketch (Target VM failed to initialize). Make sure that you haven't set the maximum available memory too high. For more information, read revisions.txt and Help ? Troubleshooting.

Any help will be appreciated! I'm using Processing 2.0.2. Please see code below ~



import ipcapture.*;
//import processing.video.*;
//Capture video;

IPCapture cam;

PImage prevFrame;
// How different must a pixel be to be a "motion" pixel
float threshold = 50;

void setup() {
  //removed the http:// before the cam address as the forum is replacing it for a link::
  cam = new IPCapture(this, "", "", "");
    prevFrame = createImage(cam.width,cam.height,RGB);

  // this works as well:

  // cam = new IPCapture(this);
  // cam.start("url", "username", "password");

  // It is possible to change the MJPEG stream by calling stop()
  // on a running camera, and then start() it with the new
  // url, username and password.

void draw() {
  if (cam.isAvailable()) {
    prevFrame.copy(cam,0,0,cam.width,cam.height,0,0,cam.width,cam.height); // Before we read the new frame, we always save the previous frame for comparison!



  // Begin loop to walk through every pixel
  for (int x = 0; x < cam.width; x ++ ) {
    for (int y = 0; y < cam.height; y ++ ) {

      int loc = x + y*cam.width;            // Step 1, what is the 1D pixel location
      color current = cam.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);

      // Step 5, How different are the colors?
      // If the color at that pixel has changed, then there is motion at that pixel.
      if (diff > threshold) { 
        // If motion, display black
        pixels[loc] = color(0);
      } else {
        // If not, display white
        pixels[loc] = color(255);


void keyPressed() {
  if (key == ' ') {
    if (cam.isAlive()) cam.stop();
    else cam.start();


  • Did you ever sort this out? I'm trying to do the same thing and getting the same issue. Thanks.

  • i try to download this ipcapture from the add manager and is not there , why is that ? i thought thats where you go to downooad things auto

Sign In or Register to comment.