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>
057implements
058VectorAggregator<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.size());
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        @Override
086        public SparseDoubleFV aggregateVectors(List<? extends ArrayFeatureVector<DATATYPE>> features) {
087                final SparseDoubleFV fv = new SparseDoubleFV(assigner.size());
088
089                for (final ArrayFeatureVector<DATATYPE> f : features) {
090                        final IndependentPair<int[], DISTANCE> a = assigner.assignWeighted(f.values);
091
092                        increment(fv, a);
093                }
094
095                return fv;
096        }
097
098        /**
099         * Aggregate the given features into a vector.
100         *
101         * @param features
102         *            the features to aggregate
103         * @return the aggregated vector
104         */
105        public SparseDoubleFV aggregateVectorsRaw(List<DATATYPE> features) {
106                final SparseDoubleFV fv = new SparseDoubleFV(assigner.size());
107
108                for (final DATATYPE f : features) {
109                        final IndependentPair<int[], DISTANCE> a = assigner.assignWeighted(f);
110
111                        increment(fv, a);
112                }
113
114                return fv;
115        }
116
117        private void increment(SparseDoubleFV fv, IndependentPair<int[], DISTANCE> a) {
118                final int[] assignments = a.firstObject();
119                final DISTANCE distances = a.getSecondObject();
120
121                if (distances instanceof byte[]) {
122                        for (int i = 0; i < assignments.length; i++) {
123                                fv.values.increment(assignments[i], ((byte[]) distances)[i]);
124                        }
125                } else if (distances instanceof short[]) {
126                        for (int i = 0; i < assignments.length; i++) {
127                                fv.values.increment(assignments[i], ((short[]) distances)[i]);
128                        }
129                } else if (distances instanceof int[]) {
130                        for (int i = 0; i < assignments.length; i++) {
131                                fv.values.increment(assignments[i], ((int[]) distances)[i]);
132                        }
133                } else if (distances instanceof long[]) {
134                        for (int i = 0; i < assignments.length; i++) {
135                                fv.values.increment(assignments[i], ((long[]) distances)[i]);
136                        }
137                } else if (distances instanceof float[]) {
138                        for (int i = 0; i < assignments.length; i++) {
139                                fv.values.increment(assignments[i], ((float[]) distances)[i]);
140                        }
141                } else if (distances instanceof double[]) {
142                        for (int i = 0; i < assignments.length; i++) {
143                                fv.values.increment(assignments[i], ((double[]) distances)[i]);
144                        }
145                } else {
146                        throw new UnsupportedOperationException("Unsupported type");
147                }
148        }
149}