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

Re : Mirroring a webcam in a sketch

$
0
0
I have a much belated response to deevaniej. I was having this same problem yesterday, and figure I might as well post my solution in case someone else stumbles across this thread.

I flipped the video for this color-tracking example by copying the output of the camera into a separate PImage called camMirror (which has its own array of pixels). I copied the pixels from the camera into camMirror in a for loop, but flipped the x axis around. Finally I drew camMirror with image(camMirror,0,0).

  1. import processing.video.*;
  2. Capture cam;
  3. color trackColor;
  4. PImage camMirror;
  5. void setup(){
  6.   size(1024,768,P2D);
  7.   cam = new Capture(this,width,height,30);
  8.   cam.start();
  9.   camMirror = new PImage(cam.width,cam.height);
  10.   trackColor = color(255,0,0);
  11.   smooth();
  12. }
  13. void draw(){
  14.   if(cam.available() == true){
  15.    cam.read();
  16.   }
  17.   cam.loadPixels();
  18.   //Mirroring the image
  19.   for(int x = 0; x < cam.width; x++){
  20.     for(int y = 0; y < cam.height; y++){
  21.       camMirror.pixels[x+y*cam.width] = cam.pixels[(cam.width-(x+1))+y*cam.width];
  22.     }
  23.   }
  24.   camMirror.updatePixels();
  25.   image(camMirror,0,0);
  26.   //the closest to the actual color we're tracking
  27.   float maxCloseness = 500;
  28.   //XY coord for closest color
  29.   int closestX = 0;
  30.   int closestY = 0;
  31.   //Begin loop to walk through every pixel
  32.   for(int x = 0; x < camMirror.width; x++){
  33.     for(int y = 0; y < camMirror.height; y++){
  34.       int loc = x + y*camMirror.width;
  35.       //get current color
  36.       color currentColor = camMirror.pixels[loc];
  37.       float r1 = red(currentColor);
  38.       float g1 = green(currentColor);
  39.       float b1 = blue(currentColor);
  40.       float r2 = red(trackColor);
  41.       float g2 = green(trackColor);
  42.       float b2 = blue(trackColor);
  43.       //euclidean distance to compute colors
  44.       float d = dist(r1,g1,b1,r2,g2,b2);  
  45.       //if the current color is closer than the last max closeness, get its location
  46.       if(d < maxCloseness){
  47.         maxCloseness = d;
  48.         closestX = x;
  49.         closestY = y;
  50.       }
  51.     }
  52.   } 
  53.   //Make a threshold; if the color distance is less than 10, draw a circle
  54.   if(maxCloseness < 20){
  55.     //Draw a circle at the tracked pixel
  56.     fill(trackColor);
  57.     strokeWeight(4.0);
  58.     stroke(0);
  59.     ellipse(closestX,closestY,16,16);
  60.   }
  61. }
  62. void mousePressed(){
  63.   //Save color that user clicks
  64.   int loc = mouseX + mouseY*cam.width;
  65.   trackColor = camMirror.pixels[loc];
  66. }


Viewing all articles
Browse latest Browse all 1768

Trending Articles