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