Adjusting stroke color based on color of brightest pixel
in
Core Library Questions
•
11 months ago
Hey guys,
So I have been in the works of developing a mod to the Brightness Tracking code in the Examples section under Libraries > Video (as a point of reference as to where I started). The goal is to have the brightest pixel tracked and draw a line based on that pixels color. I have everything set now so that a line is drawn as it tracks whatever is the brightest pixel, but the color is hard coded. The problem is that I am not sure how to drive the stroke color based on that tracked pixel. Any help would be appreciated.
Here is what I have so far.
Here is what I have so far.
- int oldX = 0;
- int oldY = 0;
- import processing.video.*;
- Capture video;
- void setup() {
- size(320, 240); // 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 (0);
- 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 (0,255,0);
- noFill();
- float r,g,b;
- int x,y,x2,y2;
- color c;
- float bright;
- for (y = 0; y<video.height; y++)
- {
- for (x=0; x<video.width; x++)
- {
- c = video.get(x,y);
- bright = brightness(c);
- if (bright > 110)
- {
- strokeWeight (3);
- line(oldX, oldY, brightestX, brightestY);
- oldX = brightestX;
- oldY = brightestY;
- }
- }
- }
- }
- }
1