Quantcast
Channel: Processing Forum
Viewing all articles
Browse latest Browse all 1768

Color Tracking with Multiple Cameras

$
0
0
I have trying to get two USB cameras to track a specific color. This is going to be used for hand tracking, tip of a finger colored to track. The two cameras is to get three dimensional output, to control a robot arm. Yes, i know their is easer ways to control the arm, but I would like to use this method. Here is some example code if found to track colors, how do I go about using two cameras? 

  1. // Learning Processing
  2. // Daniel Shiffman
  3. // http://www.learningprocessing.com

  4. // Example 16-11: Simple color tracking

  5. import processing.video.*;

  6. // Variable for capture device
  7. Capture video;

  8. // A variable for the color we are searching for.
  9. color trackColor; 

  10. void setup() {
  11.   size(640,480);
  12.   video = new Capture(this, width, height,15);
  13.   // Start off tracking for red
  14.   trackColor = color(255,0,0);
  15.   video.start();
  16.   smooth();
  17. }

  18. void draw() {
  19.   // Capture and display the video
  20.   if (video.available()) {
  21.     video.read();
  22.   }
  23.   video.loadPixels();
  24.   image(video,0,0);

  25.   // 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.
  26.   float worldRecord = 500; 

  27.   // XY coordinate of closest color
  28.   int closestX = 0;
  29.   int closestY = 0;

  30.   // Begin loop to walk through every pixel
  31.   for (int x = 0; x < video.width; x ++ ) {
  32.     for (int y = 0; y < video.height; y ++ ) {
  33.       int loc = x + y*video.width;
  34.       // What is current color
  35.       color currentColor = video.pixels[loc];
  36.       float r1 = red(currentColor);
  37.       float g1 = green(currentColor);
  38.       float b1 = blue(currentColor);
  39.       float r2 = red(trackColor);
  40.       float g2 = green(trackColor);
  41.       float b2 = blue(trackColor);

  42.       // Using euclidean distance to compare colors
  43.       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.

  44.       // If current color is more similar to tracked color than
  45.       // closest color, save current location and current difference
  46.       if (d < worldRecord) {
  47.         worldRecord = d;
  48.         closestX = x;
  49.         closestY = y;
  50.       }
  51.     }
  52.   }
  53.   
  54. println("x = " + closestX + " y = " + closestY);
  55.   // We only consider the color found if its color distance is less than 10. 
  56.   // This threshold of 10 is arbitrary and you can adjust this number depending on how accurate you require the tracking to be.
  57.   if (worldRecord < 10) { 
  58.     // Draw a circle at the tracked pixel
  59.     fill(trackColor);
  60.     strokeWeight(4.0);
  61.     stroke(0);
  62.     ellipse(closestX,closestY,16,16);
  63.    
  64.   }
  65. }

  66. void mousePressed() {
  67.   // Save color where the mouse is clicked in trackColor variable
  68.   int loc = mouseX + mouseY*video.width;
  69.   trackColor = video.pixels[loc]; }
Here is what I tried to do.
  1. // Learning Processing
  2. // Daniel Shiffman
  3. // http://www.learningprocessing.com

  4. // Example 16-11: Simple color tracking

  5. import processing.video.*;

  6. // Variable for capture device
  7. Capture video;
  8. Capture video2;
  9. // A variable for the color we are searching for.
  10. color trackColor; 

  11. void setup() {
  12.   size(640,480);
  13.   video = new Capture(this, width, height,15);
  14.   // Start off tracking for red
  15.   trackColor = color(255,0,0);
  16.   video.start();
  17.   smooth();
  18.   
  19.   size(640,480);
  20.   video2 = new Capture(this, width, height,15);
  21.   // Start off tracking for red
  22.   trackColor = color(255,0,0);
  23.   video2.start();
  24.   smooth();
  25. }

  26. void draw() {
  27.   // Capture and display the video
  28.    if (video.available()) {
  29.     video.read();
  30.   }
  31.   video.loadPixels();
  32.   image(video,0,0);

  33.   // 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.
  34.   float worldRecord = 500; 

  35.   // XY coordinate of closest color
  36.   int closestX = 0;
  37.   int closestY = 0;

  38.   // Begin loop to walk through every pixel
  39.   for (int x = 0; x < video.width; x ++ ) {
  40.     for (int y = 0; y < video.height; y ++ ) {
  41.       int loc = x + y*video.width;
  42.       // What is current color
  43.       color currentColor = video.pixels[loc];
  44.       float r1 = red(currentColor);
  45.       float g1 = green(currentColor);
  46.       float b1 = blue(currentColor);
  47.       float r2 = red(trackColor);
  48.       float g2 = green(trackColor);
  49.       float b2 = blue(trackColor);

  50.       // Using euclidean distance to compare colors
  51.       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.
  52. println("x = " + closestX + " y = " + closestY);
  53.       // If current color is more similar to tracked color than
  54.       // closest color, save current location and current difference
  55.       if (d < worldRecord) {
  56.         worldRecord = d;
  57.         closestX = x;
  58.         closestY = y;
  59.       }
  60.     }
  61.     
  62.    //second camera below 
  63.   if (video2.available()) {
  64.     video2.read();
  65.   }
  66.   video2.loadPixels();
  67.   image(video2,0,0);

  68.   // 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.
  69.   float worldRecord = 500; 

  70.   // XY coordinate of closest color
  71.   int closestX2 = 0;
  72.   int closestY2 = 0;

  73.   // Begin loop to walk through every pixel
  74.   for (int x = 0; x < video2.width; x ++ ) {
  75.     for (int y = 0; y < video2.height; y ++ ) {
  76.       int loc = x + y*video2.width;
  77.       // What is current color
  78.       color currentColor2 = video2.pixels[loc];
  79.       float r1 = red(currentColor2);
  80.       float g1 = green(currentColor2);
  81.       float b1 = blue(currentColor2);
  82.       float r2 = red(trackColor);
  83.       float g2 = green(trackColor);
  84.       float b2 = blue(trackColor);

  85.       // Using euclidean distance to compare colors
  86.       float d = dist(r12,g12,b12,r22,g22,b22); // We are using the dist( ) function to compare the current color with the color we are tracking.
  87. println("x2 = " + closestX2 + " y2 = " + closestY2);
  88.       // If current color is more similar to tracked color than
  89.       // closest color, save current location and current difference
  90.       if (d < worldRecord2) {
  91.         worldRecord2 = d;
  92.         closestX2 = x;
  93.         closestY2 = y;
  94.       }
  95.     }
  96.   }
  97.   
  98.   // We only consider the color found if its color distance is less than 10. 
  99.   // This threshold of 10 is arbitrary and you can adjust this number depending on how accurate you require the tracking to be.
  100.   if (worldRecord < 10) { 
  101.     // Draw a circle at the tracked pixel
  102.     fill(trackColor);
  103.     strokeWeight(4.0);
  104.     stroke(0);
  105.     ellipse(closestX2,closestY2,16,16);
  106.    
  107.   }
  108.  }

  109. void mousePressed() {
  110.   // Save color where the mouse is clicked in trackColor variable
  111.   int loc = mouseX + mouseY*video.width;
  112.   trackColor = video.pixels[loc];
  113. }
Here is the error i am getting
  1. unexpected token: void
I am new to processing and java so this is most likely a stupid mistake, but any help would be appreciated.

~Thanks Idan   

Viewing all articles
Browse latest Browse all 1768

Trending Articles