ICMVideo

ITP HELP | RecentChanges | Preferences

If you are working on a pc and connecting to live video...

you may need this additional driver for your camera: http://www.abstractplane.com/

Table of Contents

Remove background from still image.

PImage myImage;

void setup(){
  size(640,480);
  myImage = loadImage("http://www.google.com/images/logo.gif");
}

void draw(){
  background(0);
    for(int row=0; row<myImage.height; row=row+1) { //for each row
    for(int col=0; col<myImage.width; col=col+1) { //for each column
      //get the color of this pixels
      //find pixel in linear array using formula: pos = row*rowWidth+column
      int locationInArray = row*myImage.width+col;
      color pix = myImage.pixels[locationInArray]; 
      //find the difference
     if (red(pix) > 100 && green(pix) > 100 && blue(pix) > 100){
       myImage.pixels[locationInArray] = 0;
     }
     
    }
  }

  image(myImage,mouseX,mouseY);
}


Simple Tint Video

import processing.video.*; 

Capture video; 


void setup(){
  video = new Capture(this, 320, 240, 12); //initiate the video, resolution and frame rate
  size(320, 240); //give you Processingwindow a size
}

void captureEvent(Capture camera) 
{ 
//this means that a new frame has come in
  camera.read(); 
} 


void draw(){
    tint(mouseX,mouseY,0);
    //video is the image coming in from the video
    image(video,0,0);
}


Look for Edges

import processing.video.*; 

Capture video; 
int threshold = 25;
PImage edges;


void setup(){
  video = new Capture(this,640, 480, 30); //initiate the video, resolution and frame rate
  size(640, 480); //give you Processingwindow a size
  edges = new PImage( width,height); //create another image besides the video image for showing the edge
}

void captureEvent(Capture camera) 
{ 
  camera.read(); 
} 


void draw(){

  //edges = new PImage( width,height);
  for(int row=1; row<video.height-1; row=row+1) { //for each row
    for(int col=1; col<video.width-1; col=col+1) { //for each column
      //notice these start at 1 and leave off the last pixel so you don't overrun the array with your + 1 and -1

      int right = video.pixels[(row)*video.width+col+1];
      int left = video.pixels[(row)*video.width+col-1];
      int diff = (int) dist(red(right),green(right),blue(right), red(left), green(left), blue(left));   


      if (diff> threshold){
        edges.pixels[row*video.width+col] = color(255,0,0);  
        //change the pixels in the other one so you don't infect the one you are checking
      }
      else{
        edges.pixels[row*video.width+col] = color(0,0,0);  
      }
    }
  }
  edges.updatePixels();
  image(edges,0,0); 
}


void keyPressed(){  //conviences for changing variables while you are the applet is running
  if (keyCode ==38) { //up arrow
    threshold++;
  }
  else if(keyCode == 40){ //down arrow
    threshold--;
  }
  println("key: " + keyCode   + " Threshold: " + threshold);
}




Control Movie


import processing.video.*; 
Movie myMovie; 
 
void setup() { 
  size(200, 200); 
  framerate(30); 
  myMovie = new Movie(this, "http://itp.nyu.edu/~dbo3/icm/choke.mov"); 
//  myMovie.play(); 
} 
void draw() { 

  image(myMovie, 0, 0); 
} 
 
void mouseMoved() { 
  myMovie.jump(mouseX*(myMovie.duration()/width)); 
    myMovie.read(); 
} 


Bouncing Balls Paint Video

import processing.video.*; 

Capture video; 
Ball[] allBalls = new Ball[500];
int arrayPositionForNewBall = 0;

void setup(){
  video = new Capture(this, 320, 240, 12); //initiate the video, resolution and frame rate
  size(320, 240); //give you Processingwindow a size
  noStroke();
}

void captureEvent(Capture camera) 
{ 
//this means that a new frame has come in
  camera.read(); 
} 

color getVideoPixelColor(int _x, int _y){
  //width * number of row + number of column is the way you convert from 
  //two x and y coordinates into the array which is really just a long linear list of coordinates
  color pixColor = video.pixels[_y*video.width  +  _x];
  return pixColor;
}
void draw(){
    for (int i = 0; i < allBalls.length; i++){
      Ball thisBall = allBalls[i];
      if (thisBall != null){
        thisBall.doEverything();
      }
    }

}

void mousePressed(){
  //make a new ball everytime you click, store it in the next place in the array
  if (arrayPositionForNewBall >= allBalls.length){
    arrayPositionForNewBall = 0;
  }
  allBalls[arrayPositionForNewBall] = new Ball(mouseX,mouseY);
  arrayPositionForNewBall++;

}


class Ball{ 


  int xpos, ypos;
  float xspeed = 2.0;  // Speed of the shape
  float yspeed = 2.2;  // Speed of the shape
  int xdirection = 1;  // Left or Right
  int ydirection = 1;  // Top to Bottom
  int ballSize = 4;


  Ball(int _startx, int _starty){
    xpos = _startx;
    ypos = _starty;
  }


  void doEverything(){
    incrementPosition();
    checkWalls();
    drawMe();
  }


  void incrementPosition(){
    // Update the position of the shape
    xpos = xpos + int( xspeed * xdirection );
    ypos = ypos + int( yspeed * ydirection );
  }


  void checkWalls(){
    if (xpos > width-ballSize || xpos < 10) {
      xdirection *= -1;
    }
    if (ypos > height-ballSize || ypos < 10) {
      ydirection *= -1;
    }
  }



  void drawMe(){
    // Draw the shape
      color colorOfThisPixelInVideo = getVideoPixelColor(xpos,ypos);
    fill( colorOfThisPixelInVideo);
    
    ellipse(xpos, ypos, ballSize, ballSize);
  }

} 


One Pixel Most Like the Color You Are Looking For

import processing.video.*; 

Capture video; 


float targetRed = 255.0; //set some numbers for the target you are chasing
float targetGreen = 0.0;
float targetBlue = 0.0;

void setup(){
  video = new Capture(this, 320, 240, 12); //initiate the video, resolution and frame rate
  size(320, 240); //give you Processingwindow a size
}

void captureEvent(Capture camera) 
{ 
  camera.read(); 
} 


void draw(){

  float worldRecord = 1000.0; //intialize the worldrecord
  int xFound = 0; // initialize the location of the red tracking ball
  int yFound = 0;

  for(int row=0; row<video.height; row=row+1) { //for each row
    for(int col=0; col<video.width; col=col+1) { //for each column
      //get the color of this pixels
      //find pixel in linear array using formula: pos = row*rowWidth+column
      color pix = video.pixels[row*video.width+col]; 
      //find the difference
     int diff = (int) dist(targetRed,targetGreen,targetBlue, red(pix), green(pix), blue(pix));
 
      if (diff< worldRecord){ // if this is closest to our target color
        worldRecord = diff;
        yFound = row; //mark the spot for drawing it later
        xFound = col;
      }
    }
  }
  image(video,0,0); //draw the video, this might be optional
  //after all the pixels have been tested, draw the winner
  fill(255,0,0);
  ellipse(xFound, yFound, 10, 10);

}

void mousePressed(){
  //allow the target color to be changed 
  color pix = video.pixels[mouseY*video.width+mouseX];
  targetRed = red(pix); //get the color of the pixel they clicked on
  targetGreen = green(pix);
  targetBlue = blue(pix);

}



Finding a Color Rect

import processing.video.*; 

Capture video; 


float targetRed = 255.0; //set some numbers for the target you are chasing
float targetGreen = 0.0;
float targetBlue = 0.0;
  int similarityThreshold = 30;

void setup(){
  video = new Capture(this, 320, 240, 12); //initiate the video, resolution and frame rate
  size(320, 240); //give you Processingwindow a size
}

void captureEvent(Capture camera) 
{ 
  camera.read(); 
} 


void draw(){


  Rectangle myRect = null;
      for(int row=0; row<video.height; row++) { //for each row
        for(int col=0; col<video.width; col++) { //for each column
          //get the color of this pixels
          //find pixel in linear array using formula: pos = row*rowWidth+column
          color pix = video.pixels[row*video.width+col];
          //find the difference
          int diff = (int) dist(targetRed,targetGreen,targetBlue, red(pix), green(pix), blue(pix));
          if (diff < similarityThreshold){
    if (myRect == null) myRect = new Rectangle(col,row,1,1); //if this is the first thing you found make a new rect
            myRect.add(col,row);
          }
        }
      }
      image(video,0,0); //draw the video, this might be optional
      
      fill(255,0,0);
      if (myRect != null){
        rect(myRect.x, myRect.y, myRect.width, myRect.height);
      }

}

void mousePressed(){
  //allow the target color to be changed 
  color pix = video.pixels[mouseY*video.width+mouseX];
  targetRed = red(pix); //get the color of the pixel they clicked on
  targetGreen = green(pix);
  targetBlue = blue(pix);

}

  void keyPressed(){
   if (keyCode == 38){
     similarityThreshold++;
     }else   if (keyCode == 40){
     similarityThreshold--;
     }
    println("New Threshold: " + similarityThreshold);
  }

Finding a Multiple Color Rects

import processing.video.*; 

Capture video; 

 ArrayList targets = new ArrayList();
 int threshold = 30;


void setup(){
  video = new Capture(this, 320, 240, 12); //initiate the video, resolution and frame rate
  size(320, 240); //give you Processingwindow a size
}

void captureEvent(Capture camera) 
{ 
  camera.read(); 
} 


void draw(){


   for(int row=0; row<video.height; row++) { //for each row
        for(int col=0; col<video.width; col++) { //for each column
          //get the color of this pixels
          //find pixel in linear array using formula: pos = row*rowWidth+column
          color thisPixel = video.pixels[row*video.width+col];
          //check all the target objects
          for (int whichTarget =0; whichTarget < targets.size(); whichTarget++){
            Target thisTarget = (Target) targets.get(whichTarget);
            if (thisTarget.isSimilar(thisPixel)){
              thisTarget.includeItInRect(col,row);
            }
          }//for every target color
        }//for every column
      }//for every row
      image(video,0,0); //draw the video, this might be optional

    
      for (int whichTarget =0; whichTarget < targets.size(); whichTarget++){
        Target thisTarget = (Target) targets.get(whichTarget);
        thisTarget.drawIt();
      }


}

 void mousePressed(){
    if (video != null) { //allow the target color to be changed
      color thisPixel = video.pixels[mouseY*video.width+mouseX];
      Target thisTarget = new Target(red(thisPixel),green(thisPixel),blue(thisPixel));
      targets.add(thisTarget);
    }

  }
  void keyPressed(){
   if (keyCode == 38){
     threshold++;
     }else   if (keyCode == 40){
     threshold--;
     }
  println("New threshold: " + threshold);
  }

  class Target {
    float targetRed ; //set some numbers for the target you are chasing
    float targetGreen;
    float targetBlue ;
    Rectangle myRect;
    

    Target(float _red, float _green, float _blue){
      targetRed = _red; //set some numbers for the target you are chasing
      targetGreen = _green;
      targetBlue = _blue;
    }

    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);
    }

    boolean isSimilar(color thisPixel){
    int diff = (int) dist(targetRed,targetGreen,targetBlue, red(thisPixel), green(thisPixel), blue(thisPixel));   
   if (diff < threshold){
        return true;
      }
      return false;
    }

    void drawIt(){
      if ( myRect != null){
        fill(targetRed,targetGreen,targetBlue);
        rect(myRect.x, myRect.y, myRect.width, myRect.height);
      }
      myRect = null; //collapse the rect again
    }

  }

Finding a Foreground Rect

import processing.video.*; 

Capture video; 

 boolean newFrame = false;
  ForegroundRect fg = new ForegroundRect();
  int similarityThreshold = 150;
  int[] background;
  boolean lookingForChange = false;  //usiign the same example to show tracking change and tracking forground
  //the only difference is how often you grab the reference background frame (every frame for finding change)




void setup(){
  video = new Capture(this, 320, 240, 12); //initiate the video, resolution and frame rate
  size(320, 240); //give you Processingwindow a size
    background = (int[]) video.pixels.clone();

}

void captureEvent(Capture camera) 
{ 
  camera.read(); 
} 


void draw(){

 for(int row=0; row<video.height; row++) { //for each row
        for(int col=0; col<video.width; col++) { //for each column
          //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
          int diff = (int) dist(red(bgPixel),green(bgPixel),blue(bgPixel), red(thisPixel), green(thisPixel), blue(thisPixel));   

           if (diff > similarityThreshold){
            fg.includeItInRect(col,row);
          }
        }//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

      fg.drawIt();



}
  void keyPressed(){  //conviences for changing variables while you are the applet is running
    if (keyCode ==38) { //up arrow
      similarityThreshold++;
    }else if(keyCode == 40){ //down arrow
      similarityThreshold--;
    }else if (key == 99 || key== 67){ //the lower or capital letter c
      lookingForChange = ! lookingForChange;
    }
    println(" threshold:" + similarityThreshold + " lookingForChange:" + lookingForChange );
  }

 void mousePressed(){
    background = (int[]) video.pixels.clone();  //create a new reference frame
  }



 class ForegroundRect {

    Rectangle myRect;
    ForegroundRect(){

    }

    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);
    }

    void drawIt(){
      if ( myRect != null){
        noFill();
         stroke(255,255,0);
        rect(myRect.x, myRect.y, myRect.width, myRect.height);
      }
      myRect = null; //collapse the rect again
    }

  }

Finding Multiple Foreground Rects

import processing.video.*; 

Capture video; 

  int similarityThreshold = 255;
  int[] background;
  boolean lookingForChange = false;  //usiign the same example to show tracking change and tracking forground
  //the only difference is how often you grab the reference background frame (every frame for finding change)




void setup(){
  video = new Capture(this, 320, 240, 12); //initiate the video, resolution and frame rate
  size(320, 240); //give you Processingwindow a size
    background = (int[]) video.pixels.clone();

}

void captureEvent(Capture camera) 
{ 
  camera.read(); 
} 


void draw(){

 ArrayList fgRects = new ArrayList();

      for(int row=0; row<video.height; row++) { //for each row
        int fgPixelsInARow = 0;

        for(int col=0; col<video.width; col++) { //for each column
          //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
        int diff = (int) dist(red(bgPixel),green(bgPixel),blue(bgPixel), red(thisPixel), green(thisPixel), blue(thisPixel));   

          if (diff > similarityThreshold  ){
            boolean foundAHome = false;
            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
              ForegroundRect thisFG =  new ForegroundRect( ) ;
              thisFG.includeItInRect(col,row);
              fgRects.add(thisFG);
            }
          }
        }//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 = 0; i< fgRects.size(); i++){  //go see if it should be attached to any of the other rectangles
        ForegroundRect thisFG =  (ForegroundRect) fgRects.get(i);
        thisFG.drawIt();
      }


}
  void keyPressed(){  //conviences for changing variables while you are the applet is running
    if (keyCode ==38) { //up arrow
      similarityThreshold++;
    }else if(keyCode == 40){ //down arrow
      similarityThreshold--;
    }else if (key == 99 || key== 67){ //the lower or capital letter c
      lookingForChange = ! lookingForChange;
    }
    println(" threshold:" + similarityThreshold + " lookingForChange:" + lookingForChange );
  }

 void mousePressed(){
    background = (int[]) video.pixels.clone();  //create a new reference frame
  }



  class ForegroundRect {

    Rectangle myRect;
    Rectangle nearRect;
    int reach = 5;

    boolean isNear(int _x, int _y){
      return (nearRect.contains(_x,_y));
    }

    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 drawIt(){
      if ( myRect != null){
        noFill();
        stroke(255,255,0);
        rect(myRect.x, myRect.y, myRect.width, myRect.height);
      }
      myRect = null; //collapse the rect again
    }

  }

Finding Skin

import processing.video.*;

Capture video;
float redLower = .35f;
float redUpper = .55f;
float greenLower = .26f;
float greenUpper = .35f;


void setup(){
 video = new Capture(this, 320, 240, 30); //initiate the video, resolution and frame rate
 size(320, 240); //give you Processingwindow a size
}


void captureEvent(Capture camera)
{
 camera.read();
}



void draw(){

 for(int row=0; row<video.height; row++) { //for each row

   for(int col=0; col<video.width; col++) { //for each column
     //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];

     
     //trade in rbb for a percentage red and percentage green so brightness falls out of it
     float total = red(thisPixel) + green(thisPixel) + blue(thisPixel);
     float normalizedRed =  red(thisPixel)/total;
     float normalizedGreen = green(thisPixel)/total;

     if (normalizedRed < redUpper && normalizedRed > redLower && normalizedGreen < greenUpper && normalizedGreen > greenLower){
       //if it is skin colored paint it red
       video.pixels[offsetInBigArray] = color(255,0,0);
     }
   }//for every column
 }//for every row
 image(video,0,0); //draw the video, this might be optional
}

void keyPressed(){  //conviences for changing variables while you are the applet is running
 //different cameras will need different numbers
 if (keyCode ==38) { //up arrow
   redUpper = redUpper + .01f;
   redLower = redLower + .01f;
 }
 else if(keyCode == 40){ //down arrow
   redUpper = redUpper - .01f;
   redLower = redLower - .01f;
 }
  else if (keyCode ==37) { //up arrow
   greenUpper = greenUpper + .01f;
   greenLower = greenLower + .01f;
 }
 else if(keyCode == 39){ //down arrow
   greenUpper = greenUpper - .01f;
   greenLower = greenLower - .01f;
 }
 println("key: " + keyCode  );
}

Finding Skin Rects


import java.awt.Rectangle;
import processing.video.*;


Capture video;
float redLower = .35f;
float redUpper = .55f;
float greenLower = .26f;
float greenUpper = .35f;
ArrayList patchesOfSkin = new ArrayList();



void setup(){
 video = new Capture(this, 320, 240, 12); //initiate the video, resolution and frame rate
 size(320, 240); //give you Processingwindow a size
}


void captureEvent(Capture camera)
{
 camera.read();
}



void draw(){

 ArrayList patchesOfSkin = new ArrayList();

 for(int row=0; row<video.height; row++) { //for each row

   for(int col=0; col<video.width; col++) { //for each column
     //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];


     //trade in rbb for a percentage red and percentage green so brightness falls out of it
     float total = red(thisPixel) + green(thisPixel) + blue(thisPixel);
     float normalizedRed =  red(thisPixel)/total;
     float normalizedGreen = green(thisPixel)/total;


     if (normalizedRed < redUpper && normalizedRed > redLower && normalizedGreen < greenUpper && normalizedGreen > greenLower){
       video.pixels[offsetInBigArray] = color(255,0,0);
       boolean foundAHome = false;
       for (int i = 0; i< patchesOfSkin.size(); i++){  //go see if it should be attached to any of the other rectangles
         SkinRect thisSkin =  (SkinRect) patchesOfSkin.get(i);
         if (thisSkin.isNear(col,row)){
           thisSkin.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
         SkinRect thisSkin =  new SkinRect( ) ;
         thisSkin.includeItInRect(col,row);
         patchesOfSkin.add(thisSkin);
       }


     }
   }//for every column
 }//for every row
 image(video,0,0); //draw the video, this might be optional
 for (int i = 0; i< patchesOfSkin.size(); i++){  //go see if it should be attached to any of the other rectangles
   SkinRect thisSkin =  (SkinRect) patchesOfSkin.get(i);
   thisSkin.drawIt();
 }



}
void keyPressed(){  //conviences for changing variables while you are the applet is running
 if (keyCode ==38) { //up arrow
   redUpper = redUpper + .01f;
   redLower = redLower + .01f;
 }
 else if(keyCode == 40){ //down arrow
   redUpper = redUpper - .01f;
   redLower = redLower - .01f;


 }
  else if (keyCode ==37) { //up arrow
   greenUpper = greenUpper + .01f;
   greenLower = greenLower + .01f;
 }
 else if(keyCode == 39){ //down arrow
   greenUpper = greenUpper - .01f;
   greenLower = greenLower - .01f;


 }
 println("key: " + keyCode  );
}


class SkinRect {


 Rectangle myRect;
 Rectangle nearRect;
 int reach = 5;


 boolean isNear(int _x, int _y){
   return (nearRect.contains(_x,_y));
 }


 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 drawIt(){
   if ( myRect != null){
     noFill();
     stroke(255,255,0);
     rect(myRect.x, myRect.y, myRect.width, myRect.height);
   }
   myRect = null; //collapse the rect again
 }


}


ITP HELP | RecentChanges | Preferences
This page is read-only | View other revisions
Last edited November 14, 2007 9:22 am (diff)
Search:
To EDIT, You have to enter an ADMINISTRATOR password (guess) in Preferences. And refresh