Using Noise in Video Input
in
Core Library Questions
•
2 years ago
Hi all,
I am trying to use a webcam to detect movement. The code I have now generates a black screen, and when the viewer moves in front of the webcam, the parts of their body that move turn white on a frame-by-frame basis (meaning only when the viewer is moving, the white frames don't stay there).
I am trying to generate abstract shapes based on the viewer's movement through the noise() function, but do not know the best way of going about this. Here is the code I have so far (partially taken from Daniel Shiffman's book, and partially from alexkwolfe.com:
// From Daniel Shiffman's Processing - Chaper 16
// Class Code for Wednesday April 6
import processing.video.*;
Capture video;
PImage previousFrame;
float diff, threshold;
int loc;
float yOffset = 0;
float noiseScale=0.02;
float noiseVal;
float perlinStep = 0.00;
float STEP = 0.02;
void setup()
{
size( 640, 480, P3D );
video = new Capture( this, width, height, 30 );
previousFrame = createImage( video.width, video.height, RGB );
threshold = 120;
rectMode( CENTER );
textAlign( CENTER, CENTER );
textSize( 14 );
}
void draw( )
{
makeWaves();
yOffset = ((yOffset+1)%height);
if (video.available())
{
previousFrame.copy( video, 0, 0, video.width, video.height,
0, 0, video.width, video.height );
previousFrame.updatePixels( );
video.read( );
} else {
}
loadPixels();
video.loadPixels();
previousFrame.loadPixels();
for( int x = 0; x < video.width; x++)
{
for( int y = 0; y < video.height; y++)
{
loc = x + y*video.width;
color current = video.pixels[loc];
color previous = previousFrame.pixels[loc];
float r1 = red(current);
float g1 = green(current);
float b1 = blue(current);
float r2 = red(previous);
float g2 = green(previous);
float b2 = blue(previous);
diff = dist( r1, g1, b1, r2, g2, b2 );
// this colors the part that moves from one frame to another
if ( diff > threshold )
{
pixels[loc] = color( 255 );
}
else
{
//pixels[loc] = color( 0 );
}
} // end inner for loop
} // end outer for loop
updatePixels();
}
void makeWaves()
{
background(255);
// background(0,0,30); //refresh background to erase previous pixel draw
//move vertically down the screen
for(float y=0; y < (height); y=y+1.5)
{
perlinStep = perlinStep + STEP;
noiseVal = noise( (perlinStep+y)*noiseScale,
(perlinStep+y)*2*noiseScale,
y*noiseScale);
//step through perlin noise space to get distinct waves
for(int x=0; x<(width/2); x=x+1)
{
// generate the perlin value
noiseVal = noise( (perlinStep+x)*noiseScale,
(perlinStep+y)*2*noiseScale,
y*noiseScale);
stroke(255-(noiseVal*255), 10);
point(mouseX + x*2, mouseY +(yOffset+(mouseY+noiseVal*80)), y*noiseVal*5);
}
}
}
Thanks for any help you can give!
1