-
Notifications
You must be signed in to change notification settings - Fork 1
Simple Tracking Example
Yuri Pourre edited this page Oct 5, 2019
·
3 revisions
This example shows how to use keel to track objects by color.
- Linux
- V4L4J (Installing V4L4J)
*This example only works in Linux.
We will use the ColorFilter to track objects by color.
First we create an Application Class as always
public class SimpleCam extends Application{
public SimpleCam(int w, int h) {
super(w, h);
}
}
Now, we declare the objects
//The Camera object (a wrap to v4l4j)
private Camera cam;
//The filter itself
private ColorFilter colorFilter;
//This BufferedLayer is needed to flip (horizontally) our Camera image
private BufferedLayer mirror;
//This is used in filter method to tell filter to search in the whole screen
private Component screen = new Component(w, h);
//This is our tracking point
private Point2D point;
At load() method we initialize our objects (the loadingPhrase says what is happening)
@Override
public void load() {
loadingPhrase = "Opening Camera";
cam = new CameraV4L4J(0);
loadingPhrase = "Setting Filter";
colorFilter = new ColorFilter();
colorFilter.setBorder(20);
colorFilter.setTolerance(20);
mirror = new BufferedLayer(0, 0);
loading = 100;
}
In the update method we set the camera image in mirror and flip filter the image
@Override
public void update(long now){
//Get the Camera image
mirror.setBuffer(cam.getBufferedImage());
//Normally the camera shows the image flipped, but we want to see something like a mirror
//So we flip the image
mirror.flipHorizontal();
//Now we search for the first pixel with the desired color in the whole screen
point = colorFilter.filterFirst(mirror.getModifiedBuffer(), screen);
}
Now we draw the Application
@Override
public void draw(Graphic g) {
//Draw the mirror image
mirror.draw(g);
//Set a Color to our Point
g.setColor(Color.CYAN);
//Draw our tracking point with radius = 10 pixels
g.fillCircle((int)point.getX(), (int)point.getY(), 10);
}
In updateMouse method, we set the color we want to track!
@Override
public GUIEvent updateMouse(PointerEvent event) {
if(event.onButtonDown(MouseButton.MOUSE_BUTTON_LEFT)){
//When mouse clicks with LeftButton, the color filter tries to find
//The color we are clicking
colorFilter.setColor(mirror.getModifiedBuffer().getRGB((int)event.getX(), (int)event.getY()));
}
return GUIEvent.NONE;
}
Full Source Code
public class SimpleCam extends Application{
public SimpleCam(int w, int h) {
super(w, h);
}
private Camera cam;
private ColorFilter colorFilter;
private BufferedLayer mirror;
private Component screen = new Component(w, h);
private Point2D point;
@Override
public void load() {
loadingPhrase = "Opening Camera";
cam = new CameraV4L4J(0);
loadingPhrase = "Setting Filter";
colorFilter = new ColorFilter();
colorFilter.setBorder(20);
colorFilter.setTolerance(20);
mirror = new BufferedLayer(0, 0);
loading = 100;
}
@Override
public void update(long now){
mirror.setBuffer(cam.getBufferedImage());
mirror.flipHorizontal();
point = colorFilter.filterFirst(mirror.getModifiedBuffer(), screen);
}
@Override
public void draw(Graphic g) {
mirror.draw(g);
g.setColor(Color.CYAN);
g.fillCircle((int)point.getX(), (int)point.getY(), 10);
}
@Override
public GUIEvent updateMouse(PointerEvent event) {
if(event.onButtonDown(MouseButton.MOUSE_BUTTON_LEFT)){
colorFilter.setColor(mirror.getModifiedBuffer().getRGB((int)event.getX(), (int)event.getY()));
}
return GUIEvent.NONE;
}
@Override
public GUIEvent updateKeyboard(KeyEvent event) {
// TODO Auto-generated method stub
return GUIEvent.NONE;
}
}