Tag Archive for 'proce55ing'

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);
  }
}

Brownian Movement Gets Moving.


Brownian Lines from Corey Hankey on Vimeo

Its been a while but I decided it was time to get back to it. After taking some time to read more from the processing book I discovered that processing renders at the a specified frame rate. Unlike flash which will skip to keep up with the processor, processing renders can be saved out in an image sequence similar to a 3d program like Maya or Cinema4D.

This concept gave me some inspiration to create more complex animations using the saveFrame() method. I started off by collecting my colors from images in my photo library containing things that I enjoyed like my wife or the great outdoors. I used a for loop to create 2000 lines on the stage at when the program was started and then grabbed a brew while processing rendered out the 300 frames that I specified.

I have included a few vimeo videos that I posted but…… they just don’t do the animation justice so I posted a quicktime of the blue render here: Brownian Quicktime.
Vimeo version

Screen shots:
Color from my wifes portrait.
Brownian Lines Animation

Colors from a day of skiing:
Brownian Lines Get Moving

Getting things moving.

Brownian Movement

Brownian Movement

Brownian Movement

I recently purchased the processing book from Ben Fry and Casey Reas and started to get more familiar with the syntax in processing. With this knowledge I was able to begin to expand on my recent Brownian Movement experiments. This time I am not using an additive stage, but instead I am clearing and redrawing the stage on every cycle to allow the particles to have a more distinct motion.

This is pretty basic example but the motion and the particle grouping turned out better than I expected so I figured I would share my results. I will clean the code up a bit and post it at a later date.

See it in action: Brownian Movement in Motion