Motion Detection

edited May 2016 in Library Questions

What's wrong with my code. When I run the code, it pops out a grey windows nothing inside there. The camera should be activated and detect motion in the screen

import processing.video.*; 
int numPixels;
int[] previousFrame;
Capture video;

void setup(){
  size (640, 480);
  video = new Capture (this, width, height);
  numPixels = video.width * video.height;
  previousFrame = new int[numPixels];
  video.start();

}

void draw() {
  if (video.available()){
    video.read();
    video.loadPixels();
    int movementSum=0;
    loadPixels();

    for (int i = 0; i < numPixels; i++){
      color currColor = video.pixels[i];
      color prevColor = previousFrame[i];

      int currR = (currColor >> 16) & 0xFF;
      int currG = (currColor >> 8) & 0xFF;
      int currB = currColor & 0xFF;

      int prevR = (currColor >> 16) & 0xFF;
      int prevG = (currColor >> 8) & 0xFF;
      int prevB = currColor & 0xFF;

      int diffR = abs(currR - prevR);
      int diffG = abs(currG - prevG);
      int diffB = abs(currB - prevB);

      movementSum += diffR + diffG + diffB;
      pixels[i] = color(diffR,diffG,diffB);
      previousFrame[i] = currColor;
    }

   if (movementSum > 0){
     updatePixels();
     println(movementSum);
   }
  }
}

Answers

  • I think if you add

    video.start();

    in setup(), it should work.

    Kf

  • Same. Added video.start(). Nothing happens.

  • You have fallen into the trap of cut and paste. Lines 30-31 need to be changed to make use of the prevColor otherwise there will never be any change.

  • Answer ✓

    I got interested in this and had a go fixing this.

    import processing.video.*; 
    int numPixels;
    Capture video;
    int[] prevFrame;
    // The lower the number the greater the sensitivity
    int sensitivity = 64;
    
    void setup() {
      size (640, 480);
      video = new Capture (this, width, height);
      numPixels = video.width * video.height;
      prevFrame = new int[numPixels];
      video.start();
    }
    
    void draw() {
      if (video.available()) {
        video.read();
        image(video, 0, 0);
        // Stect motion
        video.loadPixels();
        loadPixels();
    
        for (int i = 0; i < numPixels; i++) {
          int currColor = video.pixels[i];
          int prevColor = prevFrame[i];
    
          int currR = (currColor >> 16) & 0xFF;
          int currG = (currColor >> 8) & 0xFF;
          int currB = currColor & 0xFF;
    
          int prevR = (prevColor >> 16) & 0xFF;
          int prevG = (prevColor >> 8) & 0xFF;
          int prevB = prevColor & 0xFF;
    
          int diffR = abs(currR - prevR);
          int diffG = abs(currG - prevG);
          int diffB = abs(currB - prevB);
    
          int change = diffR + diffG + diffB;
          if (change > sensitivity) {
            pixels[i] = color(diffR, diffG, diffB);
          }
        }
        updatePixels();
        arrayCopy(video.pixels, 0, prevFrame, 0, numPixels);
      }
    }
    
  •     `import processing.video.*;
    
        Capture video;
        PImage image; //create an image :
        float threshold = 25;
    
        void setup(){
          size(640,480);
          video = new Capture(this,640,480,30);
          println(video.list());
          video.start();
    
          image  = createImage(640,480,RGB);//put the size static 
    
    
        }
    
        void captureEvent(Capture video)
        {
    
         //copy the video every time when new frame is available 
         image.copy(video,0,0,video.width,video.height,0,0,image.width,image.height);
         image.updatePixels();
         video.read(); 
        }
    
        void draw(){
          int count=0;
          float avgX =0 ;
          float avgY=0 ;
    
         video.loadPixels();
         loadPixels();
          for(int i =0;i<video.width;i++)
          {for(int j =0;j<video.height;j++){
            // find the color from current video and copy image:
            int loc = i+j*video.width;
    
            int currentPixels  =video.pixels[loc];
            int red = currentPixels << 16 & 0xFF;
            int green = currentPixels << 8 & 0xFF;
            int blue = currentPixels & 0xFF;
    
    
            int prevPixels = image.pixels[loc];
            int red1 = prevPixels << 16 & 0xFF;
            int green1 = prevPixels << 8 & 0xFF;
            int blue1 = prevPixels & 0xFF;
    
            // once we got this find the distance b/w two :
    
            float dist =  calculateDistance(red,green,blue,red1,green1,blue1);
            //thresholdChecking
            if(dist > threshold*threshold){
              avgX += i;
              avgY += j;
              count++;
    
             pixels[loc] = color(255); 
            }else{
    
              pixels[loc] = color(0);
            }
    
    
    
          }
          }
         updatePixels(); 
    
         if(count >  20){
          avgX = avgX / count;
          avgY  = avgY / count;
    
    
    
         }
    
           fill(0,0,255);
          ellipse(avgX,avgY,20,20);
    
        }
    
    
    
        float calculateDistance(float x1,float y1,float z1,float x2,float y2,float z2){
          return (x2-x1)*(x2-x1) + (y2-y1)*(y2-y1) +(z2-z1)*(z2-z1);
    
        }
    
    
    
        `
    

    You can smooth the motion by using the lerp function

Sign In or Register to comment.