We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
Page Index Toggle Pages: 1
Create pattern using webcam (Read 2512 times)
Create pattern using webcam
Apr 18th, 2009, 9:03am
 
I am fairly new to processing, however I have some experience with Java so I am able to understand the code to some extent.

I am looking to create a changing pattern on the screen using variables such as movement and maybe light from a webcam feed.

From looking at some examples and tutorials I know it is possible to get a web cam working (which I am able to do) with processing and to create some nice visual patterns.

I am looking for some help to bring it together, maybe get some variables from the webcam that I can use?

Any help would be great. Thanks.
Re: Create pattern using webcam
Reply #1 - Apr 19th, 2009, 10:23am
 
Here you go.

Here's  some  frame differencing.

should get the ball rolling on the movement triggering you're looking for.


import processing.video.*;

int numPixels;
int[] previousFrame;
Capture video;

void setup() {
 size(640, 480); // Change size to 320 x 240 if too slow at 640 x 480
 // Uses the default video input, see the reference if this causes an error
 video = new Capture(this, width, height, 24);
 numPixels = video.width * video.height;
 // Create an array to store the previously captured frame
 previousFrame = new int[numPixels];
 loadPixels();
}

void draw() {
 if (video.available()) {
   // When using video to manipulate the screen, use video.available() and
   // video.read() inside the draw() method so that it's safe to draw to the screen
   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
   for (int i = 0; i < numPixels; i++) { // For each pixel in the video frame...
     color currColor = video.pixels[i];
     color prevColor = previousFrame[i];
     // Extract the red, green, and blue components from current pixel
     int currR = (currColor >> 16) & 0xFF; // Like red(), but faster
     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);
     // Add these differences to the running tally
     movementSum += diffR + diffG + diffB;
     // Render the difference image to the screen
     pixels[i] = color(diffR, diffG, diffB);
     // The following line is much faster, but more confusing to read
     //pixels[i] = 0xff000000 | (diffR << 16) | (diffG << 8) | diffB;
     // Save the current color into the 'previous' buffer
     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();
     println(movementSum); // Print the total amount of movement to the console
   }
 }
}

Re: Create pattern using webcam
Reply #2 - Apr 20th, 2009, 2:37am
 
Hi Liam,

Thanks for your help. The code you have given is a good start but it isnt creating the effect I wanted to achieve.

What I would like to create would be something similar to the 'Hsvspace' example (examples/libraries/video(capture)). In this example the webcam is shown in the bottom corner and on the rest of the stage a visualization is created.

Is it possible to use what you gave me and alter it so that the movement draws a simple 2d pattern/shape on the screen every time there is movement.

Thanks.
Re: Create pattern using webcam
Reply #3 - May 2nd, 2009, 8:23am
 
Hello sir,can you show me the specification for those syntax rules such like "(prevColor >> 16) & 0xFF;" and so on?Your demostration fascinated me indeed.Thank you.
Page Index Toggle Pages: 1