- »» Turning Moving Objects into Moving Invisible Objects in Real Time [December 13th, 2005]
-


This processing application allows a user to interact with live video by clicking on a moving image object, making it appear as a moving invisible object. Code exerpts of interest:
——————————————————————————————————-
import processing.opengl.*;
import processing.video.*;Capture video;
/*
changes in this version: moved the arraylist from a local scope in void DRAW to a global scope in init and setup so accessible
*/
int similarityThreshold = 240;
int[] background;
boolean lookingForChange = false; //using the same example to show tracking change and tracking foreground
//the only difference is how often you grab the reference background frame (every frame for finding change)
ArrayList fgRects; //init fgrects arrayvoid setup(){
video = new Capture(this, 320, 240, 24); //initiate the video, resolution and frame rate
size(320, 240); //give you Processingwindow a size
background = (int[]) video.pixels.clone();//saves a snapshot video
fgRects = new ArrayList();//find and populate array list with fgrects
}void captureEvent(Capture camera)
{
camera.read();
}void draw(){
//ArrayList fgRects = new ArrayList(); Original placementfor(int row=0; row
int fgPixelsInARow = 0;
for(int col=0; col
//get the color of this pixels
//find pixel in linear array using formula: pos = row*rowWidth+column
int offsetInBigArray = row*video.width+col;
color thisPixel = video.pixels[offsetInBigArray];
color bgPixel = background[offsetInBigArray];
//check all the target objects
float diff = abs(red(bgPixel) – red(thisPixel)) + abs(green(bgPixel) – green(thisPixel)) + abs(blue(bgPixel) – blue(thisPixel));
if (diff > similarityThreshold ){
boolean foundAHome = false;//do not include in this fgrect
for (int i = 0; i< fgRects.size(); i++){ //go see if it should be attached to any of the other rectangles
ForegroundRect thisFG = (ForegroundRect) fgRects.get(i);
if (thisFG.isNear(col,row)){
thisFG.includeItInRect(col,row);
foundAHome = true;
break; //no need to look through the rest
}
}
if(foundAHome == false){ //if it did not fit with any of the other rects, make a new foreground rect
ForegroundRect thisFG = new ForegroundRect( ) ;
thisFG.includeItInRect(col,row);
fgRects.add(thisFG);
/* — trying to determine if new rect intersects (is contained inside) any other rects and removes it if true
another loop to check if smaller rects are overlapping, remove, rect intersect—-java
for (int j = fgRects.size()-1; j >=0; j++){
ForegroundRect thisFG2 = (ForegroundRect) fgRects.get(j);
for (int k=0; k
if (thisFG2.intersects(k)) {
fgRects.remove(j);
}
}
}
*/
}
}
}//for every column
}//for every row
// if (lookingForChange) background = (int[]) video.pixels.clone(); //use the previous frame as the reference frame
image(video,0,0); //draw the video, this might be optional
for (int i = fgRects.size()-1; i >= 0; i–){ //go see if it should be attached to any of the other rectangles
ForegroundRect thisFG = (ForegroundRect) fgRects.get(i);
if (thisFG.gone()) {
fgRects.remove(i);
}
else {
if (thisFG.isInviso()) { //this is to be invisible
thisFG.drawIt(); // draw the background pixels to make it disappear
}
else {//if it is not to be invisible
// if invisibleflag == true then do fill for thisFG else just
thisFG.drawOuter(); //just draw outline and keep live video pixels
}
}
}}
void keyPressed(){ //conviences for changing variables while you are the applet is running
if (keyCode == UP) { //up arrow
similarityThreshold++;
}
else if(keyCode == DOWN){ //down arrow
similarityThreshold–;
}
else if (key == 99 || key== 67){ //the lower or capital letter c
lookingForChange = ! lookingForChange;
}
else if (keyCode == SHIFT) {
background = (int[]) video.pixels.clone(); //create a new reference frame
}
println(”key: ” + key + ” threshold:” + similarityThreshold + ” lookingForChange:” + lookingForChange );
}void mousePressed(){
println (”hi”);
for (int k = 0; k< fgRects.size(); k++){ //go see if it should be attached to any of the other rectangles
ForegroundRect thisFG = (ForegroundRect) fgRects.get(k);
if (thisFG.isNear(mouseX,mouseY)){
thisFG.setInviso();
println (”iflag = ” + thisFG.isInviso());
break;
}
else {
println (”iflag2 = ” + thisFG.isInviso());
}
}
}class ForegroundRect {
Rectangle myRect;
Rectangle nearRect;
int reach = 50;
boolean invisoFlag = false;boolean isNear(int _x, int _y){
return (nearRect.contains(_x,_y));
}boolean gone() {
if (myRect == null) return true;
else return false;
}boolean isInviso() {
return invisoFlag;
}void setInviso() {
// if inviso does not exist, set it for first time??? no..
invisoFlag = ! invisoFlag;
}void includeItInRect(int _x, int _y){
if (myRect == null) myRect = new Rectangle(_x,_y,1,1); //if this is the first thing you found make a new rect
myRect.add(_x,_y);
nearRect = new Rectangle(myRect.x-reach,myRect.y-reach, myRect.width+2*reach, myRect.height+2*reach);
}void drawOuter(){
if ( myRect != null){
stroke(1);
noFill();
rect(myRect.x, myRect.y, myRect.width, myRect.height);
}
myRect = null; //collapse the rect again
}void drawIt(){
if ( myRect != null){
//noFill();
//rect(myRect.x, myRect.y, myRect.width, myRect.height);//——-
for (int colB = myRect.x; colB<(myRect.width + myRect.x); colB++){//using x,y,w,h from big rect to go thru each pixel and draw a rect the size of pixel and fill with background pixel color, draw it a
// d keep going, until inner rect is filled.
for (int rowA = myRect.y; rowA<(myRect.height + myRect.y); rowA++){int myoffset = (video.width + colB) + rowA*video.width;
if (myoffset >= (video.width * video.height)) {
myoffset = (video.width * video.height) – 1;
}
color c = background[myoffset];//color it with background pixels
pushMatrix(); //here we refer to the background matrix
//rectMode(CENTER);
fill(c);
noStroke();
rect(colB,rowA,1,1); //draw that mini rect to be filled with background pixel color
//point(colB,rowA);
popMatrix(); //move thru the array of values in the little rectangle}
}
//———-
}
myRect = null; //collapse the rect again
}}
» Projects
-
Flash-imation « Previous Next » Looking Box

Feed Me