Tag Archive for 'image manipulation'

Detecting Motion.

After taking a little time to research how other people were creating motion detection algorithms I decided to go with using the difference blend mode to detect what was changing from frame to frame. The first few tests I ran were very efficient but the results were not very accurate. I created a previous and next pixel array that stored the two images’ data and then blended the two images together into a new pixel array.

Now that I had all the pixel data I took a closer look at the convolution filter section in the Processing book. Convolution filters enhance certain features in an the image based on a multi-dimensional kernel array that is used to manipulate the pixels. After a little work and some experimenting I found a kernel that enhanced the motion changes in the image.

The problem now was that this process was very processor intensive and wouldn’t allow for more features to be added to the program. I shrank the camera capture dimensions and ran the algorithm on the new pixel data. Then I just scaled the final pixel data to fit the screen as it was drawn. This change took the load on the processor down from 50% to 27% while keeping the results of the motion detection.

Here are the Results (application zip): Motion Detection 01

import processing.video.*;
Capture cam;
PImage prev;
PImage cur;
int count;
// convolution kernal
float[][] kernal = { { -1, -2, -1 },
                     { 0, 0, 0 },
                     { 1, 2, 1 } };

void setup() {

  size(640, 480);
  count = 0;
  // If no device is specified, will just use the default.
  cam = new Capture(this, 320, 240);
  frameRate(24);
  // create previous and current images =====
  prev = createImage(width/2, height/2, RGB);
  cur = createImage(width/2, height/2, RGB);
  prev.loadPixels();
  cur.loadPixels();
}

void draw() {

  if (cam.available() == true) {
     cam.read();
    // capture every third frame into the previous image
    // this can be changed to get more drastic results =
    if(count > 2)
    {
      arraycopy(cur.pixels, prev.pixels);
      count = 0;
    }
    count ++;
    cam.loadPixels();
    cur.pixels = cam.pixels;
    prev.updatePixels();
    cur.updatePixels();

    // calculate the difference in the 2 frames ==================
    cur.blend(prev,0,0,width/2,height/2,0,0,width/2,height/2, DIFFERENCE);
    cur.loadPixels();

    // create a new image to store the pixel data in =================
    PImage eImg = createImage(320, 240, RGB);

    // run convolution filter =============
    // from book : Processing pg. 360 =====
    for(int y = 1; y < 240-1; y++)
    {
      for(int x =1; x < 320-1; x++)
       {
          float sum = 0;
          for(int ky = -1; ky <= 1; ky ++)
          {
            for(int kx= -1; kx <= 1; kx ++)
            {
              // calc next pixel =========
              int pos = (y+ky)*320 + (x+kx);
              float val = red(cur.pixels[pos]);
              sum += kernal[ky+1][kx+1]+ val;
            }
          }
          eImg.pixels[y*320 + x] = color(sum);
       }
    }
     eImg.updatePixels();
     // scale image up to fill th screen ===========
     scale(2,2);

     // draw manipulated data to the screen =========
     image(eImg, 0,0);

     // thresh hold filter converts to black and white only ====
     filter(THRESHOLD, .95);
  }
}