View on GitHub

oddgui

So tracking the red color of the iphone red light from the flashlight app, this code moves the cursor(the cow) around,

import processing.video.Capture;
Capture video;// regular processing libary
PFont f;
int threshold = 150; //255 is white, 0 is black
int aveX, aveY; //this is what we are trying to find
int objectR =255;
int objectG = 0;
int objectB = 0;
boolean debug = true;
int lastTime, ellapsedTime; //for checking performance
PImage cow;
PImage grass;
String message;
void setup() {
size(640, 480);
println(Capture.list());
video = new Capture(this,width,height);
//video.settings();
cow = loadImage(“cowsmall.png”);
grass = loadImage(“grass.png”);
f = createFont(“Arial”, 32);
}
void draw(){
if (video.available()){
ellapsedTime = millis() – lastTime; Â //find time since last time, only print it out if you press “t”
lastTime = millis(); Â //reset timer for checking time next fram
video.read();
int totalFoundPixels= 0; Â //we are going to find the average location of change pixes so
int sumX = 0; Â //we will need the sum of all the x find, the sum of all the y find and the total finds
int sumY = 0;
//enter into the classic nested for statements of computer vision
for (int row = 0; row < video.height; row++) {
for (int col = 0; col < video.width; col++) {
//the pixels file into the room long line you use this simple formula to find what row and column the sit in
int offset = row * width + col;
//pull out the same pixel from the current frame
int thisColor = video.pixels[offset];
//pull out the individual colors for both pixels
float r = red(thisColor);
float g = green(thisColor);
float b = blue(thisColor);
//in a color “space” you find the distance between color the same whay you would in a cartesian space, phythag or dist in processing
float diff = dist(r, g, b, objectR, objectG, objectB);
if (diff < threshold) { Â //if it is close enough in size, add it to the average
sumX = sumX + col;
sumY= sumY + row;
totalFoundPixels++;
if (debug) video.pixels[offset] = 0xff000000;//debugging
}
}
}
// look here
if (debug){
image(video,0,0);
}
image(grass, 0,0);
if (totalFoundPixels > 0){
aveX = sumX/totalFoundPixels;
aveY = sumY/totalFoundPixels;
// noStroke();
// fill(255 , 200,200);
// ellipse(aveX-10,(aveY-10),20,20);
image(cow,aveX-10,aveY-10);
}
}
// This is how to get a hot area
if(aveX > 450 && aveX < 650 && aveY >300 && aveY < 470){
// Â noStroke();
// Â fill(255,0,0);
// Â ellipse(100,100,100,100);
message = “The cow jumped over the moon”;
textFont(f);
text(message, 75,75);
}
else{
message = ” “;
textFont(f);
text(message, 75,75);
}
}
void mousePressed(){
//if they click, use that picture for the new thing to follow
int offset = mouseY * width + mouseX;
//pull out the same pixel from the current frame
int thisColor = video.pixels[offset];
//pull out the individual colors for both pixels
float objectR = red(thisColor);
float objectG = green(thisColor);
float objectB = blue(thisColor);
println(“Chasing new color  ” + objectR + ” ” + objectG + ” ” + objectB);
}
void keyPressed() {
//for adjusting things on the fly
if (key == ‘s’) {
video.settings();
}
else if (key == ‘-‘) {
threshold–;
println(“Threshold ” + threshold);
}
else if (key == ‘=’) {
threshold++;
println(“Threshold ” + threshold);
}
else if (key == ‘d’) {
background(255);
debug = !debug;
println(“Debug ” + debug);
}
else if (key == ‘t’) {
println(“Time Between Frames ” + ellapsedTime);
}
}