ColorMatcher
ColorMatcher
About
ColorMatcher is a simple library for processing to track colors in a image or live video. It can be used to implement user interfaces based on colored objects the application 'sees' in a video from a webcam. See my original post for an example
Download
Installation
to install the library simply download it and unzip it in the library folder of your processing installation. From release 0149 on the libraries folder should be in your sketchbook folder. older releases need it to be copied into the library folder of your processing installation.
Documentation
to use the library you need to define an instance of the color matcher. First import the library via the "sketch->Import Library ..."-Menu, than either use the default constructor, or use a constant to use a specific matching algorithm
ColorMatcher cm = new ColorMatcher(); // use the default macher (rgb)
or
ColorMatcher cm = new ColorMatcher( ColorMatcher.CM_RGB ); // use the rgb matcher
or
ColorMatcher cm = new ColorMatcher( ColorMatcher.CM_nRGB ); // use the normalized rgb matcher
then you need a array of colors and a PImage- or Capture-object
int searchColor1 = color( 128, 255, 0 );
int searchColor2 = color( 255, 0, 0 );
color[] colors = new color[] { searchColor1, searchColor2 };
CMPoint[] points = cm.matchColors( img, colors );
matchColors returns an array of 2D Points. the indizes in the resulting array, match the indizes of the colors in the method parameter. If a color is not found in the image, the corresponding slot in the resulting array is null. You are not limited to 2 colors. you can use as much as your processor can handle.
Examples
ColorMatcher can be used with the processing video lib (which is based on QuickTime) or GSVideo (which is based on GStreamer).
To use it with the processing standard video lib (which needs QuickTime) use the setup()
-code the following block
...
import processing.video.*;
Capture video;
void setup() {
size( 640, 480 );
video = new Capture( this, width, height, 30 );
noStroke();
smooth();
}
...
Click on a color with the left and/or right mousebutton to set a marker
import guru.colormatcher.*;
import java.util.*;
import codeanticode.gsvideo.*;
GSCapture video;
void setup() {
size( 640, 480 );
String[] parName = new String[1];
String[] parValue = new String[1];
parName[0] = "device";
parValue[0] = "/dev/video1";
video = new GSCapture(this, 640, 480, "v4l2src", parName, parValue);
strokeWeight(1);
stroke( 0 );
smooth();
}
int searchColor1 = color( 128, 255, 0 );
int searchColor2 = color( 255, 0, 0 );
boolean s1, s2;
ColorMatcher cm = new ColorMatcher( ColorMatcher.CM_RGB );
void draw() {
if ( video.available()) {
video.read();
image( video, 0, 0, width, height );
color[] colors = new color[] { searchColor1, searchColor2 };
CMPoint[] points = cm.matchColors( video, colors );
for ( int i = 0; i < points.length; i++ ) {
if (points[i] != null) {
fill( colors[i] );
ellipse( points[i].x, points[i].y, 30, 30 );
}
}
}
}
void mousePressed() {
if (mouseButton == LEFT) {
searchColor1 = get( mouseX, mouseY );
s1 = true;
}
if (mouseButton == RIGHT) {
searchColor2 = get( mouseX, mouseY );
s2 = true;
}
}
void keyPressed() {
s1 = false;
s2 = false;
}
Licence
ColorMatcher is released under the GNU LGPL