[Add to or Correct This Page]Video Tracking in JavaJava lags behind C for speed of execution, so you may not think that it’s perfect for processor- intensive tasks like video tracking. However, faster machines and Jjust- in-t Time compilers allow you to get good video frame rates along with the portability, extensibility, networking capabilities, relative ease of use, and wide knowledge base of Java. In addition to the configuration that we talked about in the introduction of this section, you will need some additional Java classes to connect QuicktimeQuickTime to Java. The “QuickTime for Java” classes are an optional part of the QuicktimeQuickTime install so you may have to update or even go back to the installer to do a custom install with “QuickTime for Java” checked. In order to remove some of the complexities of the QuicktimeQuickTime for Java Cclasses, we are also using a package called vpb.jar (downloadable from http://stage.itp.tsoa.nyu.edu/~dano/vbp/). If you want to use the code below, you will have to put another file, “vpb.jar,” into your classpath. The same folder as your class should be part of your classpath. To be sure, you can put it in …/jre/lib/ext/ (PC) and /Library/Java/Extensions/ (Macintosh OSX). When the code example below is working properly, you should see video in a window with a red red dot following the reddest thing in the picture. If you click on another color, the program with will try to chase follow that color. The main action of this code happens in the LookAtFrame method where there is a repeat loop that looks through all the rows of pixels, and then another repeat loop is nested within that to scan all the columns within a row. This quick and dirty program might do the trick for some applications. If you want to go a further once you get the pixels, we recommend the books Digital Image Processing Digital Image Processing: A Practical Introduction Using Java by Nick Efford (Addison-Wesley, 2000) or Machine Vision Algorithms in Java Machine Vision Algorithms in Java: Techniques and Implementation by Paul F. Whelan and Derek Molloy (Springer Verlag, 2000).
|
import java.awt.*;
import java.util.*;
import java.awt.event.*;
import java.awt.image.*;
public class PickNTrack extends Frame {
static int kWidth ; //The overall size of your video
static int kHeight ;
static long elapsedTime; //For evaluating performace
static long now;
static int x = 0; //these are the x and y position of the dot you are drawing
static int y = 0;
static int redGoal = 210; //these describe the color you are chasing
static int greenGoal = 20; //255, 255, 255 would be white
static int blueGoal = 20;
static PixelSource ps; //this object from vbp.jar gives you access to video pixels
static ImageWrangler iw; //this object from vbp.jar converst from arrays to images
static PickNTrack myWindow;
static Image myImage; // An image for displaying the video frame
static boolean scanning = true;
PickNTrack() { //this is like startmovie
kWidth =320;
kHeight =240;
}
public static void main(String args[]) { //always the first method called
myWindow = new PickNTrack();
myWindow.setSize(kWidth,kHeight);
myWindow.show();
myWindow.toFront();
myWindow.setLayout (null);
//These are the two objects that you are using out of vpb.jar
ps = new PixelSource(kWidth,kHeight);
iw = new ImageWrangler(ps.vidWidth,kHeight,ps.getMasks());
//add a listener for shutting the window, give it a method to call (thisWindowClosing)
myWindow.addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent e) {
myWindow.thisWindowClosing(e);
}
});
//add a listener for clicking the mouse in the window, give it a method to call (MouseClicked)
myWindow.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent e) {
myWindow.MouseClicked(e);
}
});
//add a listener for pressing a key , give it a method to call (KeyPressed)
myWindow.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyPressed(java.awt.event.KeyEvent e) {
myWindow.KeyPressed(e);
}
});
/////////This is the main loop///////
while (scanning) //you may want to farm this out to a thread
{
LookAtAFrame(); //we look through all the pixels
myWindow.repaint(); // we paint the winner
}
}
static void LookAtAFrame() { //this is where the action is, where we go pixel by pixel through the video
int[] rgb;
int worldRecord = 60000; //some huge number
ps.grabFrame(); //grab a frame
for (int row = 0; row < kHeight; row++) {
//REPEAT FOR EACH ROW OF PIXELS
for (int column = 0; column < kWidth; column++){
//REPEAT FOR EACH PIXEL IN THE ROW
rgb = ps.getPixel(column,row);
int diff = Math.abs(rgb[0] - redGoal) +Math.abs(rgb[2]-blueGoal) + Math.abs(rgb[1 ] - greenGoal);
if (diff < worldRecord) {
//if the difference is smallest for this pixel
x = column; //remember this position
y = row;
worldRecord = diff ;
//reset the record
}
}//END FOR EACH PIXEL IN A ROW
}//END FOR EACH ROW OF PIXELS
try {Thread.sleep(1);} catch(InterruptedException e) {}
myImage = iw.imageFromArray(ps.getPixelArray()); //make a picture
elapsedTime = System.currentTimeMillis()-now; //for checking performance
now = System.currentTimeMillis();
} // end of lookat frame
public void update(Graphics g){ //avoid flicker, don't clear the screen
paint(g);
}
public void paint(Graphics g) { //this is where we paint
if (myImage != null){
g.drawImage(myImage,0,0,this);//the background video
g.setColor(Color.black);//black
g.fillOval( x-5,y-5,10,10);//the dot for tracking
g.setColor(new Color(redGoal, greenGoal, blueGoal));//black
g.fillOval( x-4,y-4,8,8);//the dot for tracking
}
}
public boolean MouseClicked(java.awt.event.MouseEvent evt){
//this repicks the color you are chasing
int[] rgb;
int x = evt.getX();
int y = evt.getY();
rgb = ps.getPixel(x,y);
redGoal = rgb[0];
greenGoal =rgb[1];
blueGoal = rgb[2];
System.out.println ("clicked x" + x + " y" + y + " R" + redGoal + " G" + greenGoal + " B" + blueGoal);
return(true);
}
public boolean KeyPressed(java.awt.event.KeyEvent e){ //pop up dialog
String whichKey =e.getKeyText(e.getKeyCode());
if (whichKey.equals("S")) { //press “s” to get video settings
ps.videoSettings();
}else if (whichKey.equals("T")) { //press “t” to see performance
System.out.println ("Time: " + elapsedTime + " ms/frame ");
}
return(true);
}
void thisWindowClosing(java.awt.event.WindowEvent e) {
scanning = false;
System.out.println ("quit");
dispose();
ps.killSession();
System.exit(5);
}
}