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.
IndexProgramming Questions & HelpPrograms › Drawing brightness tracking coordinates
Page Index Toggle Pages: 1
Drawing brightness tracking coordinates (Read 1453 times)
Drawing brightness tracking coordinates
Mar 18th, 2010, 9:43am
 
Hey everyone,

First post here and I've been working with Daniel Shiffman's book and it has been great so far.

I took one of the examples that came with the processing ide by golan levin called brightness tracking and wanted to take its brightestX and brightestY variables and draw a line of all these coordinates as they change over time like one would to with the pmouseX/Y using line.

I tried to recapture the brightestX/Y variables into oldX and oldY variables so that I could call them again as so:

Code:
line(oldX, oldY, brightestX, brightestY); 



but it did not seem to work.

here is the full code with where i declared the oldX/Y variables (maybe they are in the wrong place?)

Code:
/**
* Brightness Tracking
* by Golan Levin.
*
* Tracks the brightest pixel in a live video signal.
*/


import processing.video.*;

Capture video;



void setup() {
size(640, 480); // Change size to 320 x 240 if too slow at 640 x 480
// Uses the default video input, see the reference if this causes an error
video = new Capture(this, width, height, 30);
noStroke();
smooth();
}

void draw() {
if (video.available()) {
video.read();
image(video, 0, 0, width, height); // Draw the webcam video onto the screen
int brightestX = 0; // X-coordinate of the brightest video pixel
int brightestY = 0; // Y-coordinate of the brightest video pixel
int oldX = brightestX;
int oldY = brightestY;
float brightestValue = 0; // Brightness of the brightest video pixel
// Search for the brightest pixel: For each row of pixels in the video image and
// for each pixel in the yth row, compute each pixel's index in the video
video.loadPixels();
int index = 0;
for (int y = 0; y < video.height; y++) {
for (int x = 0; x < video.width; x++) {
// Get the color stored in the pixel
int pixelValue = video.pixels[index];
// Determine the brightness of the pixel
float pixelBrightness = brightness(pixelValue);
// If that value is brighter than any previous, then store the
// brightness of that pixel, as well as its (x,y) location
if (pixelBrightness > brightestValue) {
brightestValue = pixelBrightness;
brightestY = y;
brightestX = x;
}
index++;
}
}

stroke(2);
line(oldX, oldY, brightestX, brightestY);
}
}


please let me know if you guys need more info and i will provide it. i looked up some previous topics and the oldX method was what i found

thanks!

Re: Drawing brightness tracking coordinates
Reply #1 - Mar 18th, 2010, 10:55am
 
At a guess I would say this isn't working as oldX and oldY need to be global variables.... otherwise they cannot persist from frame to frame....

Also, you would need to set them to equal brightestX, brightestY right at the end of draw, after you have used them to draw with...otherwise oldX and brightestX are always going to be the same (same with y). You will just get a dot.

Also.... probably need to set the strokeWeight higher as otherwise the line might be really hard to spot (is this what you meant by stroke(2) which actually sets the stroke colour - to almost black).
Re: Drawing brightness tracking coordinates
Reply #2 - Mar 18th, 2010, 11:34am
 
thanks for the help, that seemed to have pushed me in the right direction.

i set oldX/Y as global variables and then set them to equal to brightestX/Y which started doing what you said, it created a dot and had it move around the brightest spots without drawing a continuous line.

i'm pretty new at this but it seems like oldX/Y is identical to brightestX/Y so it fails to draw a continuous line. maybe i need to set oldX/Y as the previous values of brightestX/Y? if so how would i accomplish this?

regarding the stroke(2) yes thats exactly what happened heh

here is the new code:

Code:
/**
* Brightness Tracking
* by Golan Levin.
*
* Tracks the brightest pixel in a live video signal.
*/
int oldX = 0;
int oldY = 0;

import processing.video.*;

Capture video;

void setup() {
 size(640, 480); // Change size to 320 x 240 if too slow at 640 x 480
 // Uses the default video input, see the reference if this causes an error
 video = new Capture(this, width, height, 30);
 noStroke();
 smooth();
}

void draw() {
 if (video.available()) {
   video.read();
   image(video, 0, 0, width, height); // Draw the webcam video onto the screen
   int brightestX = 0; // X-coordinate of the brightest video pixel
   int brightestY = 0; // Y-coordinate of the brightest video pixel
   float brightestValue = 0; // Brightness of the brightest video pixel
   // Search for the brightest pixel: For each row of pixels in the video image and
   // for each pixel in the yth row, compute each pixel's index in the video
   video.loadPixels();
   int index = 0;
   for (int y = 0; y < video.height; y++) {
for (int x = 0; x < video.width; x++) {
 // Get the color stored in the pixel
 int pixelValue = video.pixels[index];
 // Determine the brightness of the pixel
 float pixelBrightness = brightness(pixelValue);
 // If that value is brighter than any previous, then store the
 // brightness of that pixel, as well as its (x,y) location
 if (pixelBrightness > brightestValue) {
   brightestValue = pixelBrightness;
   brightestY = y;
   brightestX = x;
 }
 index++;
}
   }
   oldX = brightestX;
   oldY = brightestY;

   stroke(255,0,0);
   strokeWeight(10);
   line(oldX, oldY, brightestX, brightestY);
 }
}


thanks for the help!
Re: Drawing brightness tracking coordinates
Reply #3 - Mar 18th, 2010, 2:09pm
 
You got it exactly right....

oldX = brightestX;
oldY = brightestY;

These need to be right at the end of draw.....after you draw the line.
Re: Drawing brightness tracking coordinates
Reply #4 - Mar 19th, 2010, 7:30am
 
thanks giles. i moved the oldX/Y variables at the very end and i'm able to get it to draw lines but for a really brief period of time. the previous coordinates i guess arent there anymore. it doesn't do more than two points currently

i guess it would help to let you guys know that what i'm trying to accomplish is to use video to get a basic drawing of the captured movement of bright spots on a simple white background with black lines.

thanks again for all the help
Re: Drawing brightness tracking coordinates
Reply #5 - Mar 19th, 2010, 7:57am
 
Then don't draw the video..... comment this out:

image(video, 0, 0, width, height);
Re: Drawing brightness tracking coordinates
Reply #6 - Mar 19th, 2010, 8:27am
 
perfect. i think thats it. thanks much

Code:
/**
Modification of Golan Levin's Brightness Tracking
*/
int oldX = 0;
int oldY = 0;

import processing.video.*;

Capture video;

void setup() {
 size(640, 480); // Change size to 320 x 240 if too slow at 640 x 480
 // Uses the default video input, see the reference if this causes an error
 background(255,255,255);
 video = new Capture(this, width, height, 30);
 noStroke();
 smooth();
}

void draw() {
 if (video.available()) {
   video.read();
   //image(video, 0, 0, width, height); // Draw the webcam video onto the screen
   int brightestX = 0; // X-coordinate of the brightest video pixel
   int brightestY = 0; // Y-coordinate of the brightest video pixel
   float brightestValue = 0; // Brightness of the brightest video pixel
   // Search for the brightest pixel: For each row of pixels in the video image and
   // for each pixel in the yth row, compute each pixel's index in the video
   video.loadPixels();
   int index = 0;
   for (int y = 0; y < video.height; y++) {
for (int x = 0; x < video.width; x++) {
 // Get the color stored in the pixel
 int pixelValue = video.pixels[index];
 // Determine the brightness of the pixel
 float pixelBrightness = brightness(pixelValue);
 // If that value is brighter than any previous, then store the
 // brightness of that pixel, as well as its (x,y) location
 if (pixelBrightness > brightestValue) {
   brightestValue = pixelBrightness;
   brightestY = y;
   brightestX = x;
 }
 index++;
}
   }
   
   stroke(1);
   strokeWeight(1);
   line(oldX, oldY, brightestX, brightestY);
   oldX = brightestX;
   oldY = brightestY;
 }
}
Re: Drawing brightness tracking coordinates
Reply #7 - May 3rd, 2010, 5:08pm
 
sorry to revive this thread but i had two last minor things i'm trying to do.

when the sketch runs, i notice the line start from the top and draw to the area where the brightest pixel is. is there any way to make it so that it just starts from the brightest pixel? I set brightestX and Y something other than 0,0 but it didn't seem to work. any suggestions would be welcome.

also, i had set up a keypressed function to clear out the sketch and make the background white but i was wondering if there is any way to make this timed, for example every 10 minutes the keypressed function runs? a full sketch refresh would work too (although i didn't find anything regarding that in the reference section)

thanks and sorry if these are pretty amateur questions
Re: Drawing brightness tracking coordinates
Reply #8 - May 4th, 2010, 1:11am
 
I haven't fully read the thread (not really a syntax question, will move it to Programs) so I won't answer the first question.
As for a periodic task, it is simple (and often asked...): take the current value of millis() and when diff of that value and current millis() one is above a threshold, do your action.
Page Index Toggle Pages: 1