001/**
002 * Copyright (c) 2011, The University of Southampton and the individual contributors.
003 * All rights reserved.
004 *
005 * Redistribution and use in source and binary forms, with or without modification,
006 * are permitted provided that the following conditions are met:
007 *
008 *   *  Redistributions of source code must retain the above copyright notice,
009 *      this list of conditions and the following disclaimer.
010 *
011 *   *  Redistributions in binary form must reproduce the above copyright notice,
012 *      this list of conditions and the following disclaimer in the documentation
013 *      and/or other materials provided with the distribution.
014 *
015 *   *  Neither the name of the University of Southampton nor the names of its
016 *      contributors may be used to endorse or promote products derived from this
017 *      software without specific prior written permission.
018 *
019 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
020 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
021 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
022 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
023 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
024 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
025 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
026 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
027 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
028 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
029 */
030package org.openimaj.image.objectdetection.haar;
031
032import org.openimaj.citation.annotation.Reference;
033import org.openimaj.citation.annotation.ReferenceType;
034import org.openimaj.image.analysis.algorithm.SummedSqTiltAreaTable;
035
036/**
037 * A classifier based on a Haar-like feature. The classifier forms a binary tree
038 * (or stump) and has left and right nodes to apply depending on the outcome of
039 * feature evaluation. If this classifier is a stump, then its left and right
040 * nodes will be {@link ValueClassifier}s. If it is a tree, then the left and/or
041 * right nodes will be {@link HaarFeatureClassifier}s.
042 * 
043 * @author Jonathon Hare (jsh2@ecs.soton.ac.uk)
044 * 
045 */
046@Reference(
047                type = ReferenceType.Inproceedings,
048                author = { "Viola, P.", "Jones, M." },
049                title = "Rapid object detection using a boosted cascade of simple features",
050                year = "2001",
051                booktitle = "Computer Vision and Pattern Recognition, 2001. CVPR 2001. Proceedings of the 2001 IEEE Computer Society Conference on",
052                pages = { " I", "511 ", " I", "518 vol.1" },
053                number = "",
054                volume = "1",
055                customData = {
056                                "keywords", " AdaBoost; background regions; boosted simple feature cascade; classifiers; face detection; image processing; image representation; integral image; machine learning; object specific focus-of-attention mechanism; rapid object detection; real-time applications; statistical guarantees; visual object detection; feature extraction; image classification; image representation; learning (artificial intelligence); object detection;",
057                                "doi", "10.1109/CVPR.2001.990517",
058                                "ISSN", "1063-6919 "
059                })
060public class HaarFeatureClassifier implements Classifier {
061        Classifier left;
062        Classifier right;
063        HaarFeature feature;
064        float threshold;
065
066        /**
067         * Construct with the given feature, threshold and left/right nodes.
068         * 
069         * @param feature
070         *            the feature on which the classifier is based.
071         * @param threshold
072         *            the threshold for the classifier.
073         * @param left
074         *            the classifier to invoke if the feature response is less than
075         *            the threshold
076         * @param right
077         *            the classifier to invoke if the feature response is greater
078         *            than or equal to the threshold
079         */
080        public HaarFeatureClassifier(HaarFeature feature, float threshold, Classifier left, Classifier right) {
081                this.feature = feature;
082                this.threshold = threshold;
083                this.left = left;
084                this.right = right;
085        }
086
087        @Override
088        public float classify(final SummedSqTiltAreaTable sat, final float wvNorm,
089                        final int x, final int y)
090        {
091                final float response = feature.computeResponse(sat, x, y);
092
093                return (response < threshold * wvNorm) ?
094                                left.classify(sat, wvNorm, x, y) :
095                                        right.classify(sat, wvNorm, x, y);
096        }
097
098        @Override
099        public void updateCaches(StageTreeClassifier cascade) {
100                feature.updateCaches(cascade);
101                left.updateCaches(cascade);
102                right.updateCaches(cascade);
103        }
104}