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.training;
031
032import java.util.List;
033
034import org.openimaj.image.analysis.algorithm.SummedSqTiltAreaTable;
035import org.openimaj.image.objectdetection.haar.HaarFeature;
036import org.openimaj.ml.classification.LabelledDataProvider;
037import org.openimaj.util.array.ArrayUtils;
038
039public class BasicTrainingData implements LabelledDataProvider {
040        SummedSqTiltAreaTable[] sats;
041        boolean[] classes;
042        HaarFeature[] features;
043
044        public BasicTrainingData(List<SummedSqTiltAreaTable> positive, List<SummedSqTiltAreaTable> negative,
045                        List<HaarFeature> features)
046        {
047                sats = new SummedSqTiltAreaTable[positive.size() + negative.size()];
048                classes = new boolean[sats.length];
049
050                int count = 0;
051                for (final SummedSqTiltAreaTable t : positive) {
052                        sats[count] = t;
053                        classes[count] = true;
054                        ++count;
055                }
056
057                for (final SummedSqTiltAreaTable t : negative) {
058                        sats[count] = t;
059                        classes[count] = false;
060                        ++count;
061                }
062
063                this.features = features.toArray(new HaarFeature[features.size()]);
064        }
065
066        @Override
067        public float[] getFeatureResponse(int dimension) {
068                final float[] response = new float[sats.length];
069
070                for (int i = 0; i < sats.length; i++) {
071                        final float wvNorm = computeWindowVarianceNorm(sats[i]);
072
073                        response[i] = features[dimension].computeResponse(sats[i], 0, 0) / wvNorm;
074                }
075
076                return response;
077        }
078
079        @Override
080        public boolean[] getClasses() {
081                return classes;
082        }
083
084        @Override
085        public int numInstances() {
086                return classes.length;
087        }
088
089        @Override
090        public int numDimensions() {
091                return features.length;
092        }
093
094        float computeWindowVarianceNorm(SummedSqTiltAreaTable sat) {
095                final int w = sat.sum.width - 1 - 2;
096                final int h = sat.sum.height - 1 - 2;
097
098                final int x = 1; // shift by 1 scaled px to centre box
099                final int y = 1;
100
101                final float sum = sat.sum.pixels[y + h][x + w] + sat.sum.pixels[y][x] -
102                                sat.sum.pixels[y + h][x] - sat.sum.pixels[y][x + w];
103                final float sqSum = sat.sqSum.pixels[y + w][x + w] + sat.sqSum.pixels[y][x] -
104                                sat.sqSum.pixels[y + w][x] - sat.sqSum.pixels[y][x + w];
105
106                final float cachedInvArea = 1.0f / (w * h);
107                final float mean = sum * cachedInvArea;
108                float wvNorm = sqSum * cachedInvArea - mean * mean;
109                wvNorm = (float) ((wvNorm >= 0) ? Math.sqrt(wvNorm) : 1);
110
111                return wvNorm;
112        }
113
114        @Override
115        public float[] getInstanceFeature(int idx) {
116                final float[] feature = new float[features.length];
117                final SummedSqTiltAreaTable sat = sats[idx];
118
119                final float wvNorm = computeWindowVarianceNorm(sat);
120
121                for (int i = 0; i < features.length; i++) {
122                        feature[i] = features[i].computeResponse(sat, 0, 0) / wvNorm;
123                }
124
125                return feature;
126        }
127
128        @Override
129        public int[] getSortedResponseIndices(int d) {
130                return ArrayUtils.indexSort(getFeatureResponse(d));
131        }
132
133        public HaarFeature getFeature(int dimension) {
134                return features[dimension];
135        }
136}