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.analysis.algorithm.histogram;
031
032import org.openimaj.image.FImage;
033import org.openimaj.image.analyser.ImageAnalyser;
034import org.openimaj.image.analysis.algorithm.histogram.binning.SpatialBinningStrategy;
035import org.openimaj.image.processing.convolution.FImageGradients;
036
037/**
038 * Implementation of the {@link WindowedHistogramExtractor} for efficiently
039 * extracting gradient orientation histograms. This implementation is built on
040 * top of a {@link SATWindowedExtractor}. The {@link #analyseImage(FImage)}
041 * method can be used to precompute the underlying data required for efficient
042 * histogram extraction using any of the <code>computeHistogram</code> methods.
043 * <p>
044 * Computed histograms are simply the sum of magnitudes for each orientation bin
045 * over the given window. If you need to generate more complex features (for
046 * example, aggregated spatially binned histograms) then use this class in
047 * combination with a {@link SpatialBinningStrategy}.
048 * <p>
049 * The {@link #analyseImage(FImage, FImage)} method can be used to construct
050 * histograms with moderated magnitudes (for example, suppressing all magnitudes
051 * except those at edges).
052 *
053 * @author Jonathon Hare (jsh2@ecs.soton.ac.uk)
054 */
055public class GradientOrientationHistogramExtractor
056extends SATWindowedExtractor
057implements ImageAnalyser<FImage>
058{
059        private FImageGradients.Mode orientationMode;
060        private boolean histogramInterpolation;
061
062        /**
063         * Construct a new {@link GradientOrientationHistogramExtractor} with the
064         * given number of bins. Optionally perform linear interpolation across
065         * orientation bins. Histograms can also use either signed or unsigned
066         * gradients.
067         *
068         * @param nbins
069         *            number of bins
070         * @param histogramInterpolation
071         *            if true cyclic linear interpolation is used to share the
072         *            magnitude across the two closest bins; if false only the
073         *            closest bin will be filled.
074         * @param orientationMode
075         *            the range of orientations to extract
076         */
077        public GradientOrientationHistogramExtractor(int nbins, boolean histogramInterpolation,
078                        FImageGradients.Mode orientationMode)
079        {
080                super(nbins);
081
082                this.histogramInterpolation = histogramInterpolation;
083                this.orientationMode = orientationMode;
084        }
085
086        @Override
087        public void analyseImage(FImage image) {
088                final FImage[] magnitudes = new FImage[nbins];
089
090                for (int i = 0; i < nbins; i++)
091                        magnitudes[i] = new FImage(image.width, image.height);
092
093                FImageGradients.gradientMagnitudesAndQuantisedOrientations(image, magnitudes, histogramInterpolation,
094                                orientationMode);
095
096                computeSATs(magnitudes);
097        }
098
099        /**
100         * Analyse the given image, but construct the internal data such that the
101         * gradient magnitudes are multiplied by the given edge map before being
102         * accumulated. This could be used to suppress all magnitudes except those
103         * at edges; the resultant extracted histograms would only contain
104         * information about edge gradients.
105         *
106         * @param image
107         *            the image to analyse
108         * @param edges
109         *            the edge image
110         */
111        public void analyseImage(FImage image, FImage edges) {
112                final FImage[] magnitudes = new FImage[nbins];
113
114                for (int i = 0; i < nbins; i++)
115                        magnitudes[i] = new FImage(image.width, image.height);
116
117                FImageGradients.gradientMagnitudesAndQuantisedOrientations(image, magnitudes, histogramInterpolation,
118                                orientationMode);
119
120                for (int i = 0; i < nbins; i++)
121                        magnitudes[i].multiplyInplace(edges);
122
123                computeSATs(magnitudes);
124        }
125}