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
counting pixels (Read 1188 times)
counting pixels
May 20th, 2009, 5:18am
 
hello, i am very new to the processing world and consequently I am not sure about different commands and ways of doing things in this environment. anyways, for a uni project, I need to measure the width and height of a persons silhouette. I've got the silhouette down, but I have no idea how I can count the height or width. I am happy to settle with even just counting how many white pixels are in the image.

Here's my code so you know what i'm talking about:

import processing.video.*;

color black = color(0);
color white = color(255);
int numPixels;
Capture video;
int tall;
int wide;
int tallestPixel;
PShape body;


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();
   video.loadPixels();
   int threshold = 20; // 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();
   }
   
 }

int i = 0;
void keyPressed() {
//freezes frame ready to count white pixels
 if (key == ' ') {
   noLoop();
     
   }
   updatePixels();
 }


any help would be greatly appreciated.
thanks!
Re: counting pixels
Reply #1 - May 21st, 2009, 11:28am
 
ok, lots of stuff in this question Smiley

first of all turning your video feed to b/w will not automatically find people's silhouette; the easiest way to look for [human shapes] inside an image would be to use haar cascades (opencv will do the dirty job if you decide to go this way).

if, on the other side, you did set up your capture environment so that thresholding will do the trick, you still have to face an ugly beast of computer vision: the blobs. the linked wikipedia page will teach you the needed theory; anyway you don't strictly have to code your own blob searching routine: again opencv has its own blob detection internal library, and in the library page of the processing site you'll find also an alternative.

finally, if you decide to simply count white pixels, you're almost done: simply increment a variable while you're thresholding. start by initializing you counter at the beginning of draw()
Code:

int whitecounter = 0;

then push the increment line in your for cycle
Code:

if (pixelBrightness > threshold) { // If the pixel is brighter than the
      pixels[i] = white; // threshold value, make it white
      whitecount++;
    }


this way whitecounter will store the amount of white pixels in each frame
Re: counting pixels
Reply #2 - May 21st, 2009, 7:50pm
 
hey naus3a, thanks so much for your reply! I tried playing around with the blob detection within the openCV library, but processing crashes for some reason (this was before I added noLoop() though...) anyways, I have decided to just count the white pixels, its all I really need to get my idea going

thanks again!!
Re: counting pixels
Reply #3 - May 22nd, 2009, 12:24am
 
you're welcome; if your sketch crashes with opencv try to control:

a) that the opencv installation is ok (ie try the examples); it can be a pain sometimes

b) that you're webcam is installed correctly

have fun:)
Page Index Toggle Pages: 1