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
Brightness Threshold and video display (Read 1179 times)
Brightness Threshold and video display
Apr 5th, 2009, 9:22am
 
Hey,

I've been working with brightness thresholding, trying to make an installation where users can 'draw' with light onto a mirrored projection of themselves. I have the drawing part down, but I can't seem to make the captured video of the user visible.
Any suggestions?!

Heres the code so far:
*best tested in a darker room using a small light source ( tiny flash light, glow stick etc..)
import processing.video.*;

color black = color(0,2);
color white = color(255,0,125);
int numPixels;
Capture video;

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

void draw() {
 if (video.available()) {
   video.read();
     //image(video, 0, 0);
   video.loadPixels();
   int threshold = 240; // 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;
     //  pixels[i] = video.pixels[i]; // 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(mouseX, mouseY);
   float 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
   }
 
 }
}

Re: Brightness Threshold and video display
Reply #1 - Apr 18th, 2009, 5:00pm
 
i have tried the code out and by putting updatePixels(); out of the if statement it seems to work all right. i was able to see myself at least Smiley i suppose that was the problem, right?

try deleting the updatePixels() inside if(){...} and putting it in this region:

[...]
  else { // Otherwise,
    fill(white); // set the fill to white
  }
 
}
updatePixels(); //put it here; it should work out
}
Re: Brightness Threshold and video display
Reply #2 - Apr 14th, 2010, 4:35am
 
I am doing a similar project and am having a similar problem. I am making a sketch that will allow the user to 'draw' via moving their body in view of a webcam.
I am overlaying the brightness threshold capture over a live video feed so the user can see themselves. The brightness threshold feed leaves a trail after movement so the user is 'drawing'. So far so good. However, the live feed is also leaving a trail which I do not want to happen.
Originally I did which is why the code is there but now when I take out the showImage.blend(...)..., the live feed stops showing up on the screen. I have tried changing the filter and interchanging the various video feeds but nothing doing. Either the live feed disappears completely or it leaves a trail.

Here's the code -

//----- brightness threshold adapted from Processing Learning Tutorials
//----- http://processing.org/learning/libraries/brightnessthresholding.html
//----- by Golan Levin


import processing.video.*;

color black = color(0,1);
color white = color(255);
int numPixels;

Capture video;

PImage liveFeed; //live feed from webcam
PImage showImage; //needed to display feed to user
//PImage = Datatype for storing images. Processing can display .gif, .jpg, .tga, and .png images.
//using this means we can export out images of what's on screen so the trail will be visible unlike in movie classes


void setup() {
 size(800,600);
 strokeWeight(5);
 video = new Capture(this, width, height, 30); //width, height, frameRate
 liveFeed = createImage(width, height, RGB);  //width/height = defined in SIZE
 showImage = createImage(width, height, RGB); //need RGB to define as a PImage and not image
 
 numPixels = video.width * video.height;
 noCursor();
 smooth();
}

void draw() {
   frameRate(30);
   if (video.available()) {
     video.read();
     video.loadPixels();
       
     //-----flip video feed AND IT WORKS!!!
     
     int increment = 0;
     
     for (int y = 0; y < video.height; y++) {
       for (int x = video.width - 1; x >= 0 ; x--) {
         liveFeed.pixels[increment] = video.pixels[y*width + x];
         increment++;
       }
     }    
   
     //-----threshold
   
   
   int threshold = 200; // 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(liveFeed.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();
   
     //displays image on screen
     tint(255,20);//alpha of showImage
     image(showImage, 0, 0); //image, x co-ord, y co-ord

     //showImage.filter(GRAY); //other filters still don't make it appear!!!
     showImage.blend(liveFeed, 0, 0, width, height, 0, 0, width, height, LIGHTEST);
//swapping liveFeed, showImage & video doesn't work!!!
     //(src image,x,y,width,height,x co-ord destinations upper left corner,y co=ord destinations upper left corner, MODE)
     //Copies a pixel or rectangle of pixels using different blending modes
     
   }//end o video available

}//end o draw


Any help would be greatly appreciated!
Page Index Toggle Pages: 1