Plotting data from webcam versus time
in
Core Library Questions
•
2 years ago
Hi
I'm trying to write a program that will be able to plot the angle of a pendulum versus time. The program so far detects the bottom of the pendulum by detecting the brightest pixel on screen (the pendulum has an LED attached). I've drawn a stationary ellipse as the starting position of the pendulum.
I have gotten the program to determine theta based on the initial postion. I now have to plot theta versus time and do not know how to do this. I started making an array to record the x,y values but don't know where to go from there. I can print them using the println() command but I want a live plot of theta versus time. My programming knowledge is very basic so please be specific.
import processing.video.*;
Capture video;
lightPoint point1;
void setup() {
size(640, 480);
video = new Capture(this, width, height, 30);
noStroke();
smooth();
//Create a light point to visualise from the class lightPoint
point1 = new lightPoint(0.0, 0.0, 0);
}
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;
point1.x = brightestX;
point1.y = brightestY;
}
index++;
}
ellipse(320, 150, 25, 25);
float[]x = new float[1000];
float[]y = new float[1000];
float[][]xy = {
println(atan(abs(point1.x-320)/abs(point1.y-150)));
}
// Display the lightPoint objects
// ellipse(brightestX, brightestY, 50, 50);
point1.display();
// draw_grid(brightestX, brightestY);
}
}
float[] values = new float[width];
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, 25, 25);
}
}
1