Well I managed to fix the error and organize the code in to functions, but now when I run the program I just get a black screen. I had the USB cameras plugged in and on before running the code and I am still not sure what is wrong. I am also not sure how to make sure each of the camera variables is to a different camera, because I am using two cameras
- // Thanks too
- // Learning Processing
- // Daniel Shiffman
- // http://www.learningprocessing.com
- // Example 16-11: Simple color tracking
- import processing.video.*;
- // Variables for capture devices
- Capture video;
- Capture video2;
- // A variable for the color we are searching for.
- color trackColor;
- void setup() {
- size(640,480);
- video = new Capture(this, width, height,15);
- video.start();
- // Start off tracking for red
- trackColor = color(255,0,0);
- smooth();
- size(640,480);
- video2 = new Capture(this, width, height,15);
- video.start();
- // Start off tracking for red
- trackColor = color(255,0,0);
- smooth();
- }
- void draw()
- {
- cam1();
- cam2();
- }
- void cam1()
- {
- // Capture and display the video
- if (video.available()) {
- video.read();
- }
- video.loadPixels();
- image(video,0,0);
- // Before we begin searching, the "world record" for closest color is set to a high number that is easy for the first pixel to beat.
- float worldRecord = 500;
- // XY coordinate of closest color
- int closestX = 0;
- int closestY = 0;
- // Begin loop to walk through every pixel
- for (int x = 0; x < video.width; x ++ ) {
- for (int y = 0; y < video.height; y ++ ) {
- int loc = x + y*video.width;
- // What is current color
- color currentColor = video.pixels[loc];
- float r1 = red(currentColor);
- float g1 = green(currentColor);
- float b1 = blue(currentColor);
- float r2 = red(trackColor);
- float g2 = green(trackColor);
- float b2 = blue(trackColor);
- // Using euclidean distance to compare colors
- float d = dist(r1,g1,b1,r2,g2,b2); // We are using the dist( ) function to compare the current color with the color we are tracking.
- // If current color is more similar to tracked color than
- // closest color, save current location and current difference
- if (d < worldRecord) {
- worldRecord = d;
- closestX = x;
- closestY = y;
- }
- }
- }
- if (worldRecord < 10) {
- // Draw a circle at the tracked pixel
- fill(trackColor);
- strokeWeight(4.0);
- stroke(0);
- ellipse(closestX,closestY,16,16);
- }
- }
- void cam2()
- {
- // Capture and display the video
- if (video2.available()) {
- video2.read();
- }
- video2.loadPixels();
- image(video2,0,0);
- // Before we begin searching, the "world record" for closest color is set to a high number that is easy for the first pixel to beat.
- float worldRecord = 500;
- // XY coordinate of closest color
- int closestX2 = 0;
- int closestY2 = 0;
- // Begin loop to walk through every pixel
- for (int x = 0; x < video2.width; x ++ ) {
- for (int y = 0; y < video2.height; y ++ ) {
- int loc = x + y*video2.width;
- // What is current color
- color currentColor2 = video2.pixels[loc];
- float r12 = red(currentColor2);
- float g12 = green(currentColor2);
- float b12 = blue(currentColor2);
- float r22 = red(trackColor);
- float g22 = green(trackColor);
- float b22 = blue(trackColor);
- // Using euclidean distance to compare colors
- float d2 = dist(r12,g12,b12,r22,g22,b22); // We are using the dist( ) function to compare the current color with the color we are tracking.
- // If current color is more similar to tracked color than
- // closest color, save current location and current difference
- if (d2 < worldRecord) {
- worldRecord = d2;
- closestX2 = x;
- closestY2 = y;
- }
- }
- }
- if (worldRecord < 10) {
- // Draw a circle at the tracked pixel
- fill(trackColor);
- strokeWeight(4.0);
- stroke(0);
- ellipse(closestX2,closestY2,16,16);
- }
- }
- void mousePressed() {
- // Save color where the mouse is clicked in trackColor variable
- int loc = mouseX + mouseY*video.width;
- trackColor = video.pixels[loc];
- }
~Thanks Idan