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.feature.local.aggregate;
031
032import java.util.List;
033
034import org.openimaj.feature.ArrayFeatureVector;
035import org.openimaj.feature.SparseDoubleFV;
036import org.openimaj.feature.local.LocalFeature;
037import org.openimaj.ml.clustering.assignment.SoftAssigner;
038import org.openimaj.util.pair.IndependentPair;
039
040/**
041 * Implementation of an object capable of extracting the soft-assigned Bag of
042 * Visual Words (BoVW) representation of an image given a list of local features
043 * and an {@link SoftAssigner} with an associated codebook. Soft-assignment
044 * assigns a single feature to multiple visual words, usually with some
045 * weighting for each word.
046 * 
047 * @author Jonathon Hare (jsh2@ecs.soton.ac.uk)
048 * 
049 * @param <DATATYPE>
050 *            Primitive array type of the {@link ArrayFeatureVector}s used by
051 *            the {@link LocalFeature}s that will be processed.
052 * @param <DISTANCE>
053 *            Primitive array datatype for recording distances between points
054 *            and cluster centroids
055 */
056public class SoftBagOfVisualWords<DATATYPE, DISTANCE>
057                implements
058                        VectorAggregator<ArrayFeatureVector<DATATYPE>, SparseDoubleFV>
059{
060        private SoftAssigner<DATATYPE, DISTANCE> assigner;
061
062        /**
063         * Construct with the given assigner.
064         * 
065         * @param assigner
066         *            the assigner
067         */
068        public SoftBagOfVisualWords(SoftAssigner<DATATYPE, DISTANCE> assigner) {
069                this.assigner = assigner;
070        }
071
072        @Override
073        public SparseDoubleFV aggregate(List<? extends LocalFeature<?, ? extends ArrayFeatureVector<DATATYPE>>> features) {
074                final SparseDoubleFV fv = new SparseDoubleFV(assigner.numDimensions());
075
076                for (final LocalFeature<?, ? extends ArrayFeatureVector<DATATYPE>> f : features) {
077                        final IndependentPair<int[], DISTANCE> a = assigner.assignWeighted(f.getFeatureVector().values);
078
079                        increment(fv, a);
080                }
081
082                return fv;
083        }
084
085        private void increment(SparseDoubleFV fv, IndependentPair<int[], DISTANCE> a) {
086                final int[] assignments = a.firstObject();
087                final DISTANCE distances = a.getSecondObject();
088
089                if (distances instanceof byte[]) {
090                        for (int i = 0; i < assignments.length; i++) {
091                                fv.values.increment(assignments[i], ((byte[]) distances)[i]);
092                        }
093                } else if (distances instanceof short[]) {
094                        for (int i = 0; i < assignments.length; i++) {
095                                fv.values.increment(assignments[i], ((short[]) distances)[i]);
096                        }
097                } else if (distances instanceof int[]) {
098                        for (int i = 0; i < assignments.length; i++) {
099                                fv.values.increment(assignments[i], ((int[]) distances)[i]);
100                        }
101                } else if (distances instanceof long[]) {
102                        for (int i = 0; i < assignments.length; i++) {
103                                fv.values.increment(assignments[i], ((long[]) distances)[i]);
104                        }
105                } else if (distances instanceof float[]) {
106                        for (int i = 0; i < assignments.length; i++) {
107                                fv.values.increment(assignments[i], ((float[]) distances)[i]);
108                        }
109                } else if (distances instanceof double[]) {
110                        for (int i = 0; i < assignments.length; i++) {
111                                fv.values.increment(assignments[i], ((double[]) distances)[i]);
112                        }
113                } else {
114                        throw new UnsupportedOperationException("Unsupported type");
115                }
116        }
117}