May 2012
M T W T F S S
« Jul «-»  
 123456
78910111213
14151617181920
21222324252627
28293031  

Leaving Chris World?

Why not bring back a souvenir?

Archives

Motion Seeding!


Motion Seeding Program Download


First sketch of the idea... Wha? What do you draw in class?
Flowers?
You can see how I show the programs as boxes getting squased into mine.
And I write a delightful idea down. "Turn the CAM on itself" Feedback anyone?

It's called motion seeding because you grow the
organisms by the motion in front of your web cam.
This program doesn't work as an applet due to applets not
having access to your webcam.... least this one doesn't.
So what gives eh? Offer that thing up! Or.. download the zip
and run it yourself! I'm told it will work on Windows but I have no
idea. If someone gets it to run.. leave a comment. ('Not' a flying brick through my window okay?)

The trick was to combine Conway's game of life as written by
by Mike Davis with Frame Difference by Golan Levin. I have them to thank and if luck
its okay that I post this up here. It is in the spirit of processing so I'm sure they both won't mind. I have to talk about this because 95 percent of the program is their code. I'm just the guy whom said... Hey.. wouldn't it be cool create life as I moved? And so 5 to 6 hours later I was moving and cellular life was appearing. ... It was a great moment.

...

The idea dawned on me while I was sitting in a financial management class. In fact, now that I think about it lots of ideas dawn on me when I'm sitting in a business class. I suppose it was more because I was reviewing various processing.org programs earlier that morning and it just happened to be swimming around in my mind at the time. You should have seen the moment when it just clicked and I realized what was possible. You would have seen me go wide eyed and stare off into space while looking straight forward past a fellow jabbering about statistical business risk.

Never before have I gotten so much by simply clicking together two pieces of code I knew it was to be something fun and playful. I knew it would be delightful before I even set out to make it happen. I'm just glad I had it down before the day was over. If I can't finish before my day is up the project has problems.

Game of Life + DiffFraming = :)

Its hard to recall the code story after the fact as is often the case with the heated moments of debugging and code ballet. Let's just say I've spent a great deal of time messing up an image index, a threshold and killing millions of virtual one celled creatures. Oh, and I had to write another mock up of the Game of life to make sure my pixels were in fact doing something.

In the end, it all worked and I was delighted at the result.

So, breakout your webcam and play!
Don't worry the program isn't going
to take over anything.

Thank you Jon Skulski for introducing me
to this wondrous processing! processing.org
Visit Jon at jonskulski.net

He has a fabulous particle system...

http://jonskulski.net/processing/ParticleSys/

And thank you processing.org
for making coding fun again!
Thanks to you my programs can see!

Don't be mad Golan Levin and Mike Davis I'm telling everyone I basically
just squished your programs together and added this section.

 
        	// Most of my changes where right in here...
 
                // Rather than render set live cells to the motion pixels
                if( (diffR + diffG + diffB) > threshold) // threshold
                {
                    // int y = (int)(i/width);
                    // int x = (int)(i - y);
 
                    if( x < width - 3 && y < height )
                    {
                        if( int(random(0,5)) < 2)
                        {
                            world[x][y][0] = 1;   // set cell live sparsely increase life?
                            world[x+1][y][0] = 1; // create stable spiners at random intervals
                            world[x+2][y][0] = 1;
                        }
 
                    }
                }
 

Now that I see it from this angle I can tell you each part had
its eccentricities. The threshold value decides how much motion occurs before a
cell is created. This value determines how slowly you can move your hand with out
disturbing the creatures. The int y = (int)(i/width); ?? Lets not talk about that sad chapter in my life.

if( int(random(0,5)) < 2) is strange right? Turns out you choke Conways creatures if you
over populate so I have to be more sparse with my points. Hence, there's a random function inside
of a conditional. Strange, eh?

And finally that world business isn't just setting a single cell. I made sure to create enough
cells next to each other so that they would live on. Not sure just how effective it is in the final product but it worked swimingly in my second little Life program and I brought it over.

That in short.. was 5 hours.
Would you like to be a computer scientist now?

... mmm this thing needs a reset button..
that should be quick right?

For those whom care the rest of the code is here.

 
 
/**
    Motion Seeding
    Seed the cells with the motion from the camera.
 
    The trick is to combine Conways game of life as written by
    by Mike Davis with Frame Difference by Golan Levin
 
    Version 1
    Adapted by Chris B Stones
    welcometochrisworld.com
    February 29, 2008
 
 **/
 
import processing.video.*;
 
//Threshold of how much motion until
//Life Cells are created
int threshold = 180;
 
int numPixels;
int[] previousFrame;
Capture video;
 
//Life Varibles
int sx, sy;
float density = 0.00;
int[][][] world;
 
void setup()
{
    size(320, 240); // Larger Size = Much Slower
    video = new Capture(this, width, height, 24);
    numPixels = video.width * video.height;
    previousFrame = new int[numPixels];
    loadPixels();
 
    // Set Up Life
    frameRate(20); // I pushed from 12 to 20
    sx = width;
    sy = height;
    world = new int[sx][sy][2];
    stroke(255);
 
    // Set random cells to 'on'
    for (int i = 0; i < sx * sy * density; i++)
    {
        world[(int)random(sx)][(int)random(sy)][1] = 1;
    }
 
}
 
void draw()
{
    background(0);
 
    if (video.available())
    {
        video.read();        // Read the new frame from the camera
        video.loadPixels();  // Make its pixels[] array available
 
        int movementSum = 0; // Amount of movement in the frame
 
        int i = 0;
        for (int x = 0; x < sx; x=x+1)
        {
 
            for (int y = 0; y < sy; y=y+1)
            {
                i = y*sx + x; // Convert to index
 
                color currColor = video.pixels[i];
                color prevColor = previousFrame[i];
 
                // Extract the red, green, and blue components from current pixel
                int currR = (currColor >> 16) & 0xFF;
                int currG = (currColor >> 8 ) & 0xFF;
                int currB = currColor & 0xFF;
 
                // Extract red, green, and blue components from previous pixel
                int prevR = (prevColor >> 16 ) & 0xFF;
                int prevG = (prevColor >> 8 ) & 0xFF;
                int prevB = prevColor & 0xFF;
 
                // Compute the difference of the red, green, and blue values
                int diffR = abs(currR - prevR);
                int diffG = abs(currG - prevG);
                int diffB = abs(currB - prevB);
 
                movementSum += diffR + diffG + diffB;
 
                // Render the difference image to the screen
                // This makes the ghostly image in the background
                pixels[i] = color(diffR, diffG, diffB);
 
                // Most of my changes where right in here...
 
                // Rather than render set live cells to the motion pixels
                if( (diffR + diffG + diffB) > threshold) // threshold
                {
                    // int y = (int)(i/width);
                    // int x = (int)(i - y);
 
                    if( x < width - 3 && y < height )
                    {
                        if( int(random(0,5)) < 2)
                        {
                            world[x][y][0] = 1;   // set cell live sparsely increase life?
                            world[x+1][y][0] = 1; // create stable spiners at random intervals
                            world[x+2][y][0] = 1;
                        }
 
                    }
                }
 
                previousFrame[i] = currColor;
            }
 
            // To prevent flicker from frames that are all black (no movement),
            // only update the screen if the image has changed.
            if (movementSum > 0)
            {
                updatePixels();
            }
 
        }
    }
 
    // Life Code Begins
 
    // Drawing and update cycle
    for (int x = 0; x < sx; x=x+1)
    {
        for (int y = 0; y < sy; y=y+1)
        {
            //if (world[x][y][1] == 1)
            // Change recommended by The.Lucky.Mutt
            if ((world[x][y][1] == 1) || (world[x][y][1] == 0 && world[x][y][0] == 1))
            {
                world[x][y][0] = 1;
                point(x, y);
            }
 
            if (world[x][y][1] == -1)
            {
                world[x][y][0] = 0;
            }
            world[x][y][1] = 0;
        }
    }
 
    // Birth and death cycle
    for (int x = 0; x < sx; x=x+1)
    {
        for (int y = 0; y < sy; y=y+1)
        {
            int count = neighbors(x, y);
 
            if (count == 3 && world[x][y][0] == 0)
            {
                world[x][y][1] = 1;
            }
 
            if ((count < 2 || count > 3) && world[x][y][0] == 1)
            {
                world[x][y][1] = -1;
            }
        }
    }
}
 
//Count the number of adjacent cells 'on'
int neighbors(int x, int y)
{
    return world[(x + 1) % sx][y][0] +
    world[x][(y + 1) % sy][0] +
    world[(x + sx - 1) % sx][y][0] +
    world[x][(y + sy - 1) % sy][0] +
    world[(x + 1) % sx][(y + 1) % sy][0] +
    world[(x + sx - 1) % sx][(y + 1) % sy][0] +
    world[(x + sx - 1) % sx][(y + sy - 1) % sy][0] +
    world[(x + 1) % sx][(y + sy - 1) % sy][0];
}
 

You must be logged in to post a comment.