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).
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).
- import processing.video.*;
- Capture cam;
- color trackColor;
- PImage camMirror;
- void setup(){
- size(1024,768,P2D);
- cam = new Capture(this,width,height,30);
- cam.start();
- camMirror = new PImage(cam.width,cam.height);
- trackColor = color(255,0,0);
- smooth();
- }
- void draw(){
- if(cam.available() == true){
- cam.read();
- }
- cam.loadPixels();
- //Mirroring the image
- for(int x = 0; x < cam.width; x++){
- for(int y = 0; y < cam.height; y++){
- camMirror.pixels[x+y*cam.width] = cam.pixels[(cam.width-(x+1))+y*cam.width];
- }
- }
- camMirror.updatePixels();
- image(camMirror,0,0);
- //the closest to the actual color we're tracking
- float maxCloseness = 500;
- //XY coord for closest color
- int closestX = 0;
- int closestY = 0;
- //Begin loop to walk through every pixel
- for(int x = 0; x < camMirror.width; x++){
- for(int y = 0; y < camMirror.height; y++){
- int loc = x + y*camMirror.width;
- //get current color
- color currentColor = camMirror.pixels[loc];
- float r1 = red(currentColor);
- float g1 = green(currentColor);
- float b1 = blue(currentColor);
- float r2 = red(trackColor);
- float g2 = green(trackColor);
- float b2 = blue(trackColor);
- //euclidean distance to compute colors
- float d = dist(r1,g1,b1,r2,g2,b2);
- //if the current color is closer than the last max closeness, get its location
- if(d < maxCloseness){
- maxCloseness = d;
- closestX = x;
- closestY = y;
- }
- }
- }
- //Make a threshold; if the color distance is less than 10, draw a circle
- if(maxCloseness < 20){
- //Draw a circle at the tracked pixel
- fill(trackColor);
- strokeWeight(4.0);
- stroke(0);
- ellipse(closestX,closestY,16,16);
- }
- }
- void mousePressed(){
- //Save color that user clicks
- int loc = mouseX + mouseY*cam.width;
- trackColor = camMirror.pixels[loc];
- }