Locating the brightest spot on webcam
in
Core Library Questions
•
2 years ago
Hi, I'm working on some code to find the brightest spot on a webcam so that I can trace a double pendulum and plot its position vs time and then angle vs angle.
I've gotten the program to locate the brightest pixel through code found somewhere on the forums (but that is unreliable because it keeps moving around), I don't understand how to extend it so that it tells me the brightest chunk. Rather than finding the brightest pixel, it should find the brightest 5x5 for instance.
Here's what I have:
import processing.video.*;
Capture video;
lightPoint point1;
lightPoint point2;
int DATA_SIZE = 120;
float theta1;
float theta2;
float[] angle1 = new float[DATA_SIZE];
float[] angle2 = new float[DATA_SIZE];
void setup() {
size(1280, 480);
video = new Capture(this, 640, 480, 30);
smooth();
frameRate(28);
//noiseDetail(2);
//noiseSeed(System.currentTimeMillis());
//Create a light point to visualise from the class lightPoint
point1 = new lightPoint(0.0, 0.0, 0);
point2 = new lightPoint(0.0, 0.0, 0);
}
void draw() {
if (video.available()) {
video.read();
background(255);
fill(#005599);
image(video, 0, 0, 640, 480); // Draw the webcam video onto the screen
fill(100,0,0);
text(angle1[DATA_SIZE-1]*180/3.14159, 660, 20);
fill(0,100,0);
text(angle2[DATA_SIZE-1]*180/3.14159, 660, 40);
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
int nextbrightestX = 0 ; // all values to help track the 2nd brightest points in the area.
int nextbrightestY = 0 ;
float nextbrightestValue = 0 ;
// 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;
int radius = 180 ;// closest placement of 2 different brightest spots.
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) && (distance(x, brightestX, y, brightestY) > radius))
{
brightestValue = pixelBrightness;
brightestY = y;
brightestX = x;
point1.x = brightestX;
point1.y = brightestY;
}
else if ((pixelBrightness > nextbrightestValue) && (distance(x, brightestX, y, brightestY) > radius))
{
nextbrightestValue = pixelBrightness;
nextbrightestY = y;
nextbrightestX = x;
point2.x = nextbrightestX;
point2.y = nextbrightestY;
}
index++;
}
}
fill(100,0,0);
ellipse(320, 300, 17, 17);
fill(0,100,0);
ellipse(320, 400, 17, 17);
theta1=abs( atan( (point1.x-320) / (point1.y-300) ));
theta2=abs( atan( (point2.x-320) / (point2.y-400) ));
// Display the lightPoint objects
fill(100,0,0);
point1.display();
fill(0,100,0);
point2.display();
for (int i = 1; i < DATA_SIZE; i++)
{
stroke(100,0,0);
line((i * 5) + 640, (float)((angle1[i - 1]*100))+200, (i + 1) * 5 + 640, (float)(angle1[i]*100)+200);
angle1[i - 1] = angle1[i];
stroke(0,100,0);
line((i * 5) + 640, (float)(angle2[i - 1]*100)+200, (i + 1) * 5 + 640, (float)(angle2[i]*100)+200);
angle2[i - 1] = angle2[i];
println(i) ;
}
angle1[DATA_SIZE - 1] = theta1;
angle2[DATA_SIZE - 1] = theta2;
}
}
float[] values = new float[width];
float distance(int xval, int xvalo, int yval, int yvalo)
{
return (sqrt ( ((float)(xval) - (float)(xvalo))*((float)(xval) - (float)(xvalo)) + ((float)(yval) - (float)(yvalo))* ((float)(yval) - (float)(yvalo)) ) ) ;
}
class lightPoint {
float x = 0.0; // X-coordinate of the brightest video pixel
float y = 0.0; // Y-coordinate of the brightest video pixel
// float brightestValue = 0; // Brightness of the brightest video pixel
boolean selected = false;
int index;
lightPoint(float xpos, float ypos, int num) {
x = xpos;
y = ypos;
index = num;
selected = false;
}
void display() {
//fill(204,102,0);
ellipse(x, y, 17, 17);
}
}
1