Example Tracker Mean Shift
From BoofCV
Jump to navigationJump to search
In this example mean-shift is used to track a color ball. Mean-shift performs a local search that attempts to maximize the similarity of the color in its kernel to the color in its model. It is a fairly robust and fast tracker. If you are lucky, it will sometimes even reaquire a track after it has been lost.
Example Code:
Concepts:
- Object tracking
- Color histogram
- Mean-shift
Relevant Applets:
Example Code
/**
* Mean-shift based trackers use color information to perform a local search. The Comaniciu based trackers match
* the color histogram directly, are more robust to objects with complex distributions, and can be configured to
* estimate the object's scale. Likelihood based trackers use color to compute a likelihood
* function, which mean-shift is then run on. The likelihood based trackers are very fast but only work well
* when the target is composed of a single color.
*
* @author Peter Abeles
*/
public class ExampleTrackerMeanShift {
public static void main(String[] args) {
MediaManager media = DefaultMediaManager.INSTANCE;
String fileName = "../data/applet/tracking/balls_blue_red.mjpeg";
Quadrilateral_F64 location = new Quadrilateral_F64(394,247,474,244,475,325,389,321);
ImageType<MultiSpectral<ImageUInt8>> imageType = ImageType.ms(3,ImageUInt8.class);
SimpleImageSequence<MultiSpectral<ImageUInt8>> video = media.openVideo(fileName, imageType);
// Create the tracker. Comment/Uncomment to change the tracker.
TrackerObjectQuad<MultiSpectral<ImageUInt8>> tracker =
FactoryTrackerObjectQuad.meanShiftComaniciu2003(new ConfigComaniciu2003(), imageType);
// FactoryTrackerObjectQuad.meanShiftComaniciu2003(new ConfigComaniciu2003(true),imageType);
// FactoryTrackerObjectQuad.meanShiftLikelihood(30,5,255, MeanShiftLikelihoodType.HISTOGRAM,imageType);
// specify the target's initial location and initialize with the first frame
MultiSpectral<ImageUInt8> frame = video.next();
tracker.initialize(frame,location);
// For displaying the results
TrackerObjectQuadPanel gui = new TrackerObjectQuadPanel(null);
gui.setPreferredSize(new Dimension(frame.getWidth(),frame.getHeight()));
gui.setBackGround((BufferedImage)video.getGuiImage());
gui.setTarget(location,true);
ShowImages.showWindow(gui, "Tracking Results");
// Track the object across each video frame and display the results
while( video.hasNext() ) {
frame = video.next();
boolean visible = tracker.process(frame,location);
gui.setBackGround((BufferedImage) video.getGuiImage());
gui.setTarget(location,visible);
gui.repaint();
BoofMiscOps.pause(20);
}
}
}