2 years ago
Video Tracking the Color Red
Things to keep in mind:
The difficulties/issues of using a camera/video as a sensor?
Issue examples like: calibrating? the environment?
How does the code work?
How does these specific cameras work that we are dealing with?
Assignment:
Using a video capturing devise have it track a specific colour, in this case the colour red.
Code:
// Source :http://www.learningprocessing.com/examples/chapter-16/example-16-11/// Learning Processing
// Daniel Shiffman
// Example 16-11: Simple color tracking
import processing.video.*;
// Variable for capture device
Capture video;
// A variable for the color we are searching for.
color trackColor;
void setup() {
//screen size
size(640,480);
// “new” - creates a “new” object.
// “this” - refers to the current object (i.e. “this object”).
// “width” - a system variable which stores the width of the display window. This value is set by the first parameter of the size() function. Vise versa for height.
// guessing - the fourth variable “15” effects frame rate - ie the capture?
video = new Capture(this,width,height,15);
// Start off tracking for red
trackColor = color(255,0,0);
smooth();
}
// “void” - Keyword used indicate a function returns no value. Which is then followed by a function that is being implemented/used and then the statement.
void draw() {
// Capture and display the video
if (video.available()) {
video.read();
}
// “loadpixel ()” - Loads the pixel data for the display window (or an image) into the pixels[] array.
video.loadPixels();
//”image” - Diplays images to the screen. The images must be in the sketch’s “data” directory to load correctly (which was done in the beginning here).
// image(img, x, y)
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. “dist ()” - Calculates the distance between two points.
// 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 < 50) {
// Draw a circle at the tracked pixel
fill(trackColor);
strokeWeight(4.0);
stroke(0);
// “elipse” - ellipse(x, y, width, height)
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];
}
