Loading...
Logo
Processing Forum
I'm trying to flip the webcam while using this colour tracking code. How do I do this?

import processing.video.*;

// Variable for capture device
Capture video;

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

void setup() {
size(320,240);
video = new Capture(this,width,height,15);
// Start off tracking for red
trackColor = color(255,0,0);
smooth();
}

void draw() {
// Capture and display the video
if (video.available()) {
video.read();
}
video.loadPixels();
image(video,0,0);

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

// XY coordinate of closest color
int closestX = 0;
int closestY = 0;

// Begin loop to walk through every pixel
for (int x = 0; x < video.width; x ++ ) {
for (int y = 0; y < video.height; y ++ ) {
int loc = x + y*video.width;
// What is current color
color currentColor = video.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);

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

// If current color is more similar to tracked color than
// closest color, save current location and current difference
if (d < worldRecord) {
worldRecord = d;
closestX = x;
closestY = y;
}
}
}

// We only consider the color found if its color distance is less than 10. 
// This threshold of 10 is arbitrary and you can adjust this number depending on how accurate you require the tracking to be.
if (worldRecord < 10) { 
// Draw a circle at the tracked pixel
fill(trackColor);
strokeWeight(4.0);
stroke(0);
ellipse(closestX,closestY,16,16);
}
}

void mousePressed() {
// Save color where the mouse is clicked in trackColor variable
int loc = mouseX + mouseY*video.width;
trackColor = video.pixels[loc];
}

Replies(7)

On the line where it says

int loc = x + y*video.width;

You can flip horizontall with

(width-1-x) + y*video.width;

You can flip vertically with

x + (height-1-y)*video.width;


Hey J! I tried the code... but it didnt work for me :S 
pushMatrix();
scale(-1,1);
image(video,0,0);
popMatrix();
makio, that seems to pixelate the code in a stange way 
new code:
how do i flip video with this code:
import processing.video.*;
Capture video;
color trackColor;
void setup() {
size(320,240);
video = new Capture(this,width,height,15);
trackColor = color(255,0,0);
smooth();}
void draw() {
if (video.available()) {
video.read();}
video.loadPixels();
image(video,0,0);
pushMatrix();
      scale(-1,1);
      translate(-video.width, 0);
      image(video, 0, 0); 
    popMatrix();
float worldRecord = 500; 
int closestX = 0;
int closestY = 0;
for (int x = 0; x < video.width; x ++ ) {
for (int y = 0; y < video.height; y ++ ) {
int loc = x + y*video.width;
color currentColor = video.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);
float d = dist(r1,g1,b1,r2,g2,b2);
if (d < worldRecord) {
worldRecord = d;
closestX = x;
closestY = y;}}}
if (worldRecord < 10) { 
fill(trackColor);
strokeWeight(4.0);
stroke(0);
ellipse(closestX,closestY,16,16);}}
void mousePressed() {
int loc = mouseX + mouseY*video.width;
trackColor = video.pixels[loc];
}
1) In that code, the video is already flipped.  Perhaps you want to flip it back?  It is being flipped here:

pushMatrix();
      scale(-1,1);
      translate(-video.width, 0);
      image(video, 0, 0); 
    popMatrix();

2) Please run auto-format on your code before posting it.

3) Please don't post the same question to multiple threads.  You posted this question to no less than six threads. This will leave a lot of unanswered questions in the archive, which makes it difficult for other people searching through it in the future.
As said...
It is good that you searched the forum for an answer, but you should try to understand the threads instead of spamming the forum with the same question repeated 6 times!
I deleted the extra messages...