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
Thresholds (Read 332 times)
Thresholds
Apr 15th, 2009, 8:03am
 
We are trying to create a program where ellipses are bouncing freely and when the camera detects a person, the ellipses form to that shape. We are using the Brightness Thresholding code but the ellipses aren't acting the way we would like. If anyone has any suggestions on how to make the ellipses move freely within the black or white spaces that would be welcome.

This is our main code:

import processing.video.*;
float testBrightness;
color black = color(0);
color white = color(255);
int numPixels;
Capture video;
Ball [] bouncing= new Ball[4];
//Ball [] bouncing2= new Ball[2];
void setup() {
 size(640, 480); // Change size to 320 x 240 if too slow at 640 x 480
 strokeWeight(5);
 // 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;
 noCursor();
 smooth();
for(int i=0; i<bouncing.length; i++){
bouncing[i]= new Ball(125, 125, 125,random(0,640),random(0,480),random(3,5), random(4,6));

 }

}

void draw() {
 if (video.available()) {
   video.read();
   video.loadPixels();
   int threshold = 127; // Set the threshold value
   float pixelBrightness; // Declare variable to store a pixel's color
   // Turn each pixel in the video frame black or white depending on its brightness
   loadPixels();
   for (int i = 0; i < numPixels; i++) {
     pixelBrightness = brightness(video.pixels[i]);
     if (pixelBrightness > threshold) { // If the pixel is brighter than the
       pixels[i] = white; // threshold value, make it white
     }
     else { // Otherwise,
       pixels[i] = black; // make it black
     }
   }
   updatePixels();
   // Test a location to see where it is contained. Fetch the pixel at the test
   // location (the cursor), and compute its brightness
  /*int testValue = get(xpos, ypos);
   testBrightness = brightness(testValue);
   if (testBrightness > threshold) { // If the test location is brighter than
     fill(black); // the threshold set the fill to black
   }
   else { // Otherwise,
     fill(white); // set the fill to white
   }
  ellipse(mouseX, mouseY, 20, 20);
   */  for(int i=0; i<bouncing.length; i++){
bouncing[i].move();
bouncing[i].display();
//bouncing2[i].move();
//bouncing2[i].display();
 }
}
 
}

This is the class to go with the above code:

class Ball {
 
 float r;
 float g;
 float b;
 int xpos;
int ypos;
 int xspeed;
 int yspeed;
 float state;
int xdirection;  
int ydirection;  

Ball(float tempr, float tempg, float tempb, float tempxpos, float tempypos, float tempxspeed, float tempyspeed){
  r=tempr;
  g=tempg;
  b=tempb;
  xpos=int(tempxpos);
  ypos=int(tempypos);
  xspeed=int(tempxspeed);
  yspeed=int(tempyspeed);
  state=0;
  xdirection = 1;  
  ydirection = 1;

}

void display(){
 noStroke();
 fill(r,g,b);
   ellipse(xpos,ypos,8,8);
}



void move(){
xpos = xpos + ( xspeed * xdirection );
 ypos = ypos + ( yspeed * ydirection );
/*  
 if (xpos > width-4 || xpos < 0) {
   xdirection *= -1;
 }
 if (ypos > height-4 || ypos < 0) {
   ydirection *= -1;
 }
*/

int testValue = get(xpos, ypos);
int tv2 = get(xpos+9, ypos);
int tv3 = get(xpos+9, ypos);
int tv4 = get(xpos-9, ypos);
int tv5 = get(xpos-9, ypos);
   testBrightness = brightness(testValue);
   println (testBrightness);
   if (testBrightness > 127)  { // If the test location is brighter than the threshold set the fill to black
    xdirection *= -1;
    ydirection *= -1;
   }
     

 //} else { // Otherwise,
  //   fill(white); // set the fill to white
  // xdirection *= 1;
  // ydirection *= 1;
 //}
if (xpos > width-4 || xpos < 0) {
   xdirection *= -1;
 }
 if (ypos > height-4 || ypos < 0) {
   ydirection *= -1;
 }

 }
}


Thanks!

- Amelia and Jessica
Page Index Toggle Pages: 1