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

Drawing on video captured by webcam

$
0
0
Hi,
 I know I ask a lot of question  but this one really irritating me. I  have googled a lot but couldn't find any solution.

Question is

I got a code from somewhere which can track any color picked from a video by mouse clicking and draw a ellipse filled with it. I want this is to use as tool to draw images over the video.

like here :



you will see video and graphics drawn over the video together 


Code that I found is here :
  1. import processing.video.*; 
  2. Capture video; 
  3. color trackColor; 
  4. void setup() { 
  5.   size(320, 240); 
  6.   video = new Capture(this, width, height, 15); 
  7.   trackColor = color(255, 0, 0); 
  8.   smooth();
  9. void draw() { 
  10.   if (video.available()) { 
  11.     video.read();
  12.   } 
  13.   video.loadPixels(); 
  14.   image(video, 0, 0); 
  15.   float worldRecord = 500; 
  16.   int closestX = 0; 
  17.   int closestY = 0; 
  18.   for (int x = 0; x < video.width; x ++ ) { 
  19.     for (int y = 0; y < video.height; y ++ ) { 
  20.       int loc = x + y*video.width; 
  21.       color currentColor = video.pixels[loc]; 
  22.       float r1 = red(currentColor); 
  23.       float g1 = green(currentColor); 
  24.       float b1 = blue(currentColor); 
  25.       float r2 = red(trackColor); 
  26.       float g2 = green(trackColor); 
  27.       float b2 = blue(trackColor); 
  28.       float d = dist(r1, g1, b1, r2, g2, b2); 
  29.       if (d < worldRecord) { 
  30.         worldRecord = d; 
  31.         closestX = x; 
  32.         closestY = y;
  33.       }
  34.     }
  35.   }
  36.   if (worldRecord < 10) { 
  37.     // Draw a circle at the tracked pixel 
  38.     fill(trackColor); 
  39.     strokeWeight(4.0); 
  40.     stroke(0); 
  41.     ellipse(closestX, closestY, 16, 16);
  42.   }
  43. void mousePressed() { 
  44.   int loc = mouseX + mouseY*video.width; 
  45.   trackColor = video.pixels[loc];
  46. }

Viewing all articles
Browse latest Browse all 1768

Trending Articles