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

Re : Color Tracking with Multiple Cameras

$
0
0
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

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

  5. // Example 16-11: Simple color tracking
  6. import processing.video.*;

  7. // Variables for capture devices
  8. Capture video;

  9. Capture video2;

  10. // A variable for the color we are searching for.
  11. color trackColor; 

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

  33. void cam1()
  34. {
  35.   // Capture and display the video
  36.   if (video.available()) {
  37.     video.read();
  38.   }
  39.   video.loadPixels();
  40.   image(video,0,0);

  41.   // 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.
  42.   float worldRecord = 500; 

  43.   // XY coordinate of closest color
  44.   int closestX = 0;
  45.   int closestY = 0;

  46.   // Begin loop to walk through every pixel
  47.   for (int x = 0; x < video.width; x ++ ) {
  48.     for (int y = 0; y < video.height; y ++ ) {
  49.       int loc = x + y*video.width;
  50.       // What is current color
  51.       color currentColor = video.pixels[loc];
  52.       float r1 = red(currentColor);
  53.       float g1 = green(currentColor);
  54.       float b1 = blue(currentColor);
  55.       float r2 = red(trackColor);
  56.       float g2 = green(trackColor);
  57.       float b2 = blue(trackColor);

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

  60.       // If current color is more similar to tracked color than
  61.       // closest color, save current location and current difference
  62.       if (d < worldRecord) {
  63.         worldRecord = d;
  64.         closestX = x;
  65.         closestY = y;
  66.       }
  67.     }
  68.   }

  69. if (worldRecord < 10) { 
  70.     // Draw a circle at the tracked pixel
  71.     fill(trackColor);
  72.     strokeWeight(4.0);
  73.     stroke(0);
  74.     ellipse(closestX,closestY,16,16);
  75. }
  76. }

  77. void cam2()
  78. {
  79.  // Capture and display the video
  80.   if (video2.available()) {
  81.     video2.read();
  82.   }
  83.   video2.loadPixels();
  84.   image(video2,0,0);

  85.   // 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.
  86.   float worldRecord = 500; 

  87.   // XY coordinate of closest color
  88.   int closestX2 = 0;
  89.   int closestY2 = 0;

  90.   // Begin loop to walk through every pixel
  91.   for (int x = 0; x < video2.width; x ++ ) {
  92.     for (int y = 0; y < video2.height; y ++ ) {
  93.       int loc = x + y*video2.width;
  94.       // What is current color
  95.       color currentColor2 = video2.pixels[loc];
  96.       float r12 = red(currentColor2);
  97.       float g12 = green(currentColor2);
  98.       float b12 = blue(currentColor2);
  99.       float r22 = red(trackColor);
  100.       float g22 = green(trackColor);
  101.       float b22 = blue(trackColor);

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

  104.       // If current color is more similar to tracked color than
  105.       // closest color, save current location and current difference
  106.       if (d2 < worldRecord) {
  107.         worldRecord = d2;
  108.         closestX2 = x;
  109.         closestY2 = y;
  110.       }
  111.     }
  112.   }
  113.  
  114.  if (worldRecord < 10) { 
  115.     // Draw a circle at the tracked pixel
  116.     fill(trackColor);
  117.     strokeWeight(4.0);
  118.     stroke(0);
  119.     ellipse(closestX2,closestY2,16,16); 
  120.     
  121.   }
  122. }

  123. void mousePressed() {
  124.   // Save color where the mouse is clicked in trackColor variable
  125.   int loc = mouseX + mouseY*video.width;
  126.   trackColor = video.pixels[loc];
  127. }
 ~Thanks Idan


Viewing all articles
Browse latest Browse all 1768

Trending Articles