drmo
YaBB Newbies
Offline
Posts: 18
Re: help with simple video effect
Reply #4 - Dec 10th , 2008, 6:37pm
this should do the trick: import processing.video.*; // set circle size int circleSize = 10; // set variables int cols, rows; // set variable for capture device Capture video; float timeMS=0; //stores the time in milliseconds at which the last frame was updated float interval = 2500; //update interval in milliseconds i.e. 2.5 seconds float[] snap01; //stores colour of last frame update float[] snapDisplay; //stores colour of interpolate be color col = color(0, 0, 0, 75); //make this global as well so it's easy to change void setup() { size(640, 480, P3D); frameRate(30); cols = width / circleSize; rows = height / circleSize; //you can allocated global variables inside any function, but you only want to do this once //so allocate them here snap01 = new float[cols*rows]; snapDisplay = new float[cols*rows]; colorMode(RGB, 255, 255, 255, 100); video = new Capture(this, width, height, 12); smooth(); } void draw() { //---------- UPDATE ------------- if(millis()-timeMS > interval) { //we dont need to read video evertime we do a draw but only once //everytime we want to update, i.e. every 2.5 seconds (or whatever interval is) if(video.available()) { video.read(); //this now calls a function keeping draw smaller and more readable UpdateSnap(video); timeMS = millis(); //reset time variable } } Interpolate(); //---------- DRAW -------------- background(255); DrawCircles(); //delay(5000); //avoid delays - this basically stops the program for 2.5 //sec so you can't do anything else (like interpolating!) } void Interpolate() { float blendSpeed = 0.05; for(int i = 0; i < snap01.length; i++) { snapDisplay[i] = (1.0-blendSpeed) * snapDisplay[i] + blendSpeed * snap01[i]; } } void DrawCircles() { for (int i = 0; i < cols; i++) { for (int j = 0; j < rows; j++) { int x = i*circleSize; int y = j*circleSize; ellipseMode(CENTER); fill(col); noStroke(); //ellipse(x+circleSize/2, y+circleSize/2, circleSize*(255-snap01[i+cols*j] )/255, circleSize*(255-snap01[i+cols*j])/255); ellipse(x+circleSize/2, y+circleSize/2, circleSize*(255-snapDisplay[i+cols*j] )/255, circleSize*(255-snapDisplay[i+cols*j])/255); } } } void UpdateSnap(PImage img) { //since 'video' is also an image (i.e. the Video class derives from PImage - so it can do all the things PImage does and more) //we can just treat it as that here since we don't need to call any of the video specific functions img.loadPixels(); for (int i = 0; i < cols; i++) { for (int j = 0; j < rows; j++) { int x = i*circleSize; int y = j*circleSize; int loc = x + y*img.width; float r = red(img.pixels[loc]); float g = green(img.pixels[loc]); float b = blue(img.pixels[loc]); snap01[i+cols*j] = 0.3*r + 0.59*g + 0.11*b; } } img.updatePixels(); //the inverse of load pixels - i think you forgot this, but it doesn't seem to be required unless the image is a display }