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

Re : Drawing on video captured by webcam

$
0
0
OPENCV COLOR TRACKER 

  1. import hypermedia.video.*;

  2. OpenCV opencv;
  3. color trackColor;
  4. PImage t; 
  5. void setup() {
  6.   size(480, 360);
  7.   opencv = new OpenCV(this);
  8.   opencv.capture(width, height);  // open video stream
  9.   trackColor = color(255, 0, 0); 
  10. }
  11.  
  12. void draw() {
  13.   background(0);
  14.   opencv.read();
  15.    image(opencv.image(),0,0);
  16.    float worldRecord = 500; 
  17.   int closestX = 0; 
  18.   int closestY = 0; 
  19.   t = opencv.image();
  20.   for (int x = 0; x < opencv.width; x ++ ) { 
  21.     for (int y = 0; y < opencv.height; y ++ ) { 
  22.       int loc = x + y*opencv.width; 
  23.       color currentColor = t.pixels[loc]; 
  24.       float r1 = red(currentColor); 
  25.       float g1 = green(currentColor); 
  26.       float b1 = blue(currentColor); 
  27.       float r2 = red(trackColor); 
  28.       float g2 = green(trackColor); 
  29.       float b2 = blue(trackColor); 
  30.       float d = dist(r1, g1, b1, r2, g2, b2); 
  31.       if (d < worldRecord) { 
  32.         worldRecord = d; 
  33.         closestX = x; 
  34.         closestY = y;
  35.       }
  36.     }
  37.   }
  38.   if (worldRecord < 10) { 
  39.     // Draw a circle at the tracked pixel 
  40.     fill(trackColor); 
  41.     strokeWeight(4.0); 
  42.     stroke(0); 
  43.     ellipse(closestX, closestY, 16, 16);
  44.   }

  45. void mousePressed() { 
  46.   int loc = mouseX + mouseY*opencv.width; 
  47.   trackColor = t.pixels[loc];
  48. }

Viewing all articles
Browse latest Browse all 1768

Trending Articles