Trails on a webcam image that detects motion

Hello, sorry for the vague title, couldn’t know how to describe it in a sentence.

Here is the problem I’m facing;
I have this piece of code which detects any kind of motion and paints those pixels to white:

float dg;
float red;
float green;
float blue;
int rad =3;

import processing.video.*;

Capture video;
Capture cam;
PImage prev;

float threshold;

float motionX = 0;
float motionY = 0;

float lerpX = 0;
float lerpY = 0;

void setup() {
  size(640, 360);
  String[] cameras = Capture.list();
  printArray(cameras);
  video = new Capture(this, cameras[3]);
  video.start();
  prev = createImage(640, 360, RGB);

  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, cameras[0]);
    cam.start();
  }      
}

void captureEvent(Capture video) {
  prev.copy(video, 0, 0, video.width, video.height, 0, 0, prev.width, prev.height);
  prev.updatePixels();
  video.read();
}

void draw() {
  video.loadPixels();
  prev.loadPixels();
  image(video, 0, 0);

  threshold = 20;

  int count = 0;

  float avgX = 0;
  float avgY = 0;

  loadPixels();
  for (int x = 0; x < video.width; x++ ) {
    for (int y = 0; y < video.height; y++ ) {
      int loc = x + y * video.width;
      color currentColor = video.pixels[loc];
      float r1 = red(currentColor);
      float g1 = green(currentColor);
      float b1 = blue(currentColor);
      color prevColor = prev.pixels[loc];
      float r2 = red(prevColor);
      float g2 = green(prevColor);
      float b2 = blue(prevColor);

      float d = distSq(r1, g1, b1, r2, g2, b2); 

      if (d > threshold*threshold) {
      
        avgX += x;
        avgY += y;
        count++;
        pixels[loc] = color(255);
      } else {
        pixels[loc] = color(0);
      }
    }

  }

  if (count > 200) { 
    motionX = avgX / count;
    motionY = avgY / count;
  }

  lerpX = lerp(lerpX, motionX, 0.1); 
  lerpY = lerp(lerpY, motionY, 0.1); 

  fill(255, 0, 255);
  strokeWeight(2.0);
  stroke(0);
 

  if (cam.available() == true) {
    cam.read();
  }
  
  updatePixels();
  
  //rectangle hack
  //fill(0,10);
  //rect(0,0,width,height);
}

float distSq(float x1, float y1, float z1, float x2, float y2, float z2) {
  float d = (x2-x1)*(x2-x1) + (y2-y1)*(y2-y1) +(z2-z1)*(z2-z1);
  return d;
}

Now what I want to have is to have motion trails that are slowly fading after the movement. I assumed this would work if I would increase the threshold in draw (this detects how sensitive it is to motion) and add a rectangle hack with 10 alpha at the end of draw but it didn’t work.

I would appreciate any sort of help and sorry if any red herring is involved on the code above!

Best wishes and thanks.

Hi unknowplayer,

I tried to run your code a code a really weird result (I was standing still so no motion at all):

WeirdResult

Also I’m not sure to get what you are trying to do but I think it is really close to the effect in this thread: Framerate in live video delay project : Ed Tannenbaum, Recollections

You might find some interesting informations here.

1 Like

Hello, thanks for the reply.

Actually, it takes me a few times to stop the sketch and re-run again. It simply does start as pure glitch when you first run it, just stop it and press play again. It works for me…

Thanks for the link, will check it out!