Color Recognition Using a Webcam
my purpose is to reads an image from a camera and looks for a blob of a
particular color. Click on a color in the image to choose the
color to track.
below is the code I suppose to use,however,it can't work very well,would anybody help me to find out what's wrong with it?
lots of thanks!
import processing.serial.*;
int graphPosition = 0; // horizontal position of the graph
int[] vals = new int[2]; // raw values from the sensor
int[] maximum = new int[2]; // maximum value sensed
int[] minimum = new int[2]; // minimum value sensed
int[] range = new int[2]; // total range sensed
float[] attitude = new float[2]; // the tilt values
float position; // position to translate to
Serial myPort; // the serial port
boolean madeContact = false; // whether there's been serial data in
void setup () {
// draw the window:
size(400, 400, P3D);
// set the background color:
background(0);
// set the maximum and minimum values:
for (int i = 0; i < 2; i++) {
maximum[i] = 600;
minimum[i] = 200;
// calculate the total current range:
range[i] = maximum[i] - minimum[i];
}
// calculate position:
position = width/2;
// create a font with the third font available to the system:
PFont myFont = createFont(PFont.list()[2], 18);
textFont(myFont);
// List all the available serial ports
println(Serial.list());
// Open whatever port is the one you're using.
myPort = new Serial(this, Serial.list()[0], 9600);
// only generate a serial event when you get a return char:
myPort.bufferUntil('\r');
fill(90,250,250); // set the fill color.
}
void draw () {
// clear the screen:
background(0);
// print the values:
text(vals[0] + " " + vals[1], -30, 10);
// if you've never gotten a string from the microcontroller,
// keep sending carriage returns to prompt for one:
if (madeContact == false) {
myPort.write('\r');
}
// set the attitude:
setAttitude();
// draw the plane:
tilt();
}
void setAttitude() {
for (int i = 0; i < 2; i++) {
// calculate the current attitude as a percentage of 2*PI,
// based on the current range:
attitude[i] = (2*PI) * float(vals[i] -
minimum[i]) /float(range[i]);
}
}
void tilt() {
// translate from origin to center:
translate(position, position, position);
// X is front-to-back:
rotateX(-attitude[1]);
// Y is left-to-right:
rotateY(-attitude[0] - PI/2);
// set the fill color:
fill(90,250,250);
// draw the rect:
ellipse(0, 0, width/4, width/4);
// change the fill color:
fill(0);
// Draw some text so you can tell front from back:
// print the values:
text(vals[0] + " " + vals[1], -30, 10,1);
}
// serialEvent method is run automatically by the Processing applet
// whenever the buffer reaches the byte value set in the bufferUntil()
// method in the setup():
void serialEvent(Serial myPort) {
// if serialEvent occurs at all, contact with the microcontroller
// has been made:
madeContact = true;
// read the serial buffer:
String myString = myPort.readStringUntil('\n');
// if you got any bytes other than the linefeed:
if (myString != null) {
myString = trim(myString);
// split the string at the commas
//and convert the sections into integers:
int sensors[] = int(split(myString, ','));
// if you received all the sensor strings, use them:
if (sensors.length >= 2) {
vals[0] = sensors[0];
vals[1] = sensors[1];
// send out the serial port to ask for data:
myPort.write('\r');
}
}
}