jjil.algorithm
Class HaarClassifierCascade

java.lang.Object
  extended by jjil.algorithm.HaarClassifierCascade

public abstract class HaarClassifierCascade
extends java.lang.Object

HaarClassifierCascade implements a Haar classifier, which is a trainable image processing tool for detecting the presence of a feature or class of features. A Haar classifier is trained by providing it with a large collection of positive and negative sample images. The training technique develops a collection of simple feature detection operations (like simple edge detectors) that are applied to the image and then thresholded. The feature detectors are organized into a tree so that they work as a cascade. An image which makes it to the end of the cascade has a high probability of actually containing the feature in question (depending on how well the sample image selection was done and how thorough the training was.)
The code here does not implement the training step, which is compute-intensive. That should be run on a PC, using code from the Open Computer Vision (OpenCV) library. The OpenCV is available on-line at http://sourceforge.net/projects/opencvlibrary/. It can be run under Windows or Linux and has been optimized for best performance on Intel processors. It also includes multiprocessor support.
Once the Haar classifier has been trained using the OpenCV HaarTraining application, the cascade has to be transformed into a text file that can be loaded into this code. This is done with a C++ program called haar2j2me. Haar2j2me changes the floating-point values in the OpenCV's Haar cascade into integer, scaling appropriately, and greatly reduces the size of the file (the XML files produced by HaarTraining are just too large to fit on many cellphones). You can find a copy of haar2j2me where you got this code.
Note: the code below does not implement tilted features, and has not been tested for anything but stump-based Haar classifiers.

Author:
webb

Nested Class Summary
static class HaarClassifierCascade.ParseException
          ParseException is thrown whenever the input doesn't match what is expected.
 
Constructor Summary
HaarClassifierCascade()
           
 
Method Summary
abstract  boolean eval(Image i)
          Returns true iff the input image passes all the tests in the Haar cascade, i.e., is a member of the positive sample image set, so far as it can tell.
static HaarClassifierCascade fromStream(java.io.InputStreamReader isr)
          Creates a new instance of HaarClassifierCascade from an input stream as generated by haar2j2me.
 int getHeight()
          Returns the Haar cascade image height.
 int getWidth()
          Returns the Haar cascade image width.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

HaarClassifierCascade

public HaarClassifierCascade()
Method Detail

getWidth

public int getWidth()
Returns the Haar cascade image width.

Returns:
the Haar cascade image width.

getHeight

public int getHeight()
Returns the Haar cascade image height.

Returns:
the Haar cascade image height.

eval

public abstract boolean eval(Image i)
                      throws Error
Returns true iff the input image passes all the tests in the Haar cascade, i.e., is a member of the positive sample image set, so far as it can tell.

Parameters:
i - The input Gray8Image. The image size must be equal to the expected size (as given by getWidth() and getHeight()).
Returns:
true iff the input image passes all the tests in the Haar cascade.
Throws:
Error - if the input image is not a Gray8Image or is the wrong size.

fromStream

public static HaarClassifierCascade fromStream(java.io.InputStreamReader isr)
                                        throws Error,
                                               java.io.IOException
Creates a new instance of HaarClassifierCascade from an input stream as generated by haar2j2me. The data structure is (hcsb "Haar classifer stump base") where "Haar classifer stump base" is the string for a stump-based Haar classifer (this loader only loads stump-based Haar classifiers).

Parameters:
isr - Input stream containing the description of the Haar classifier.
Returns:
The created HaarClassifierCascade. This will always be of type HaarClassifierStumpBase.
Throws:
java.io.IOException - if the read from isr returns an IOException, or if end of file is encountered unexpectedly.
Error - If the input doesn't match what is expected.