Loading...
Logo
Processing Forum
Im trying to check for motion in specific areas. The code below doesnt work but Im unsure why, any help would be greatly appreciated. Basically Im comparing the live video to a still image. If there is a difference above or below the X/Y used to be compared (via dist function) to a hotspot. The main problem seems to be getting the pixelY variable... (im using code adapted from a sketch on openprocessing)

Any ideas would be appreciated!

import processing.video.*;
Capture video;
int th = 140;
PImage room; //this is the image that is used check the differences with the live image
 int node1x = 40;
int node1y = 40;
int radius = 40;
void setup(){
  
  size(640,480);
  video = new Capture(this, width, height, 25); //here begins a new sessions of capturing of the images from the webcam
  room = loadImage("room.png");
video.start();
//noStroke();
smooth();
}

 
 
void draw(){
  background(255);
  if (video.available()) {
  video.read();
}

int myX = 0;
int myY = 0;
      loadPixels();

     int pixelX = -1, pixelY = -1;
     

     video.loadPixels(); //fill an array of pixels from the current captured image
         room.loadPixels(); //fill an array of pixels from the current captured image

     for(int i=0; i<video.height*video.width; i++){ //read every element of the pixels array
      
        pixelX = i % width;                                                            //get the real pixel X position
        
        if( pixelX != width ) {
          pixelY = ( i / width ) +1;
        } else {
          pixelY = ( i / height );
        }  //get the real pixel Y position

if(brightness(video.pixels[i]) < brightness(room.pixels[i])-th || brightness(video.pixels[i]) > brightness(room.pixels[i])+th ) {
myX = pixelX;
myY = pixelY;
}
     
} // END VIDEO SCAN LOOP
updatePixels();

if (dist(myX, myY, node1x, node1y) < radius) {
  //VISUAL FEEDBACK
  ellipseMode(CENTER);
  fill(255, 0, 0);
  ellipse(node1x, node1y, 20, 20);
}
  println(myX + "   " + myY);
// END
}