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;
031
032import org.openimaj.image.FImage;
033import org.openimaj.image.analyser.ImageAnalyser;
034import org.openimaj.math.geometry.shape.Rectangle;
035
036/**
037 * Implementation of an Integral Image or Summed Area Table.
038 * <p>
039 * See http://en.wikipedia.org/wiki/Summed_area_table and
040 * http://research.microsoft
041 * .com/en-us/um/people/viola/Pubs/Detect/violaJones_IJCV.pdf
042 * <p>
043 * Basically, this provides an efficient way to find the sum of all pixels in a
044 * rectangular area of an image.
045 * 
046 * @author Jonathon Hare (jsh2@ecs.soton.ac.uk)
047 */
048public class SummedAreaTable implements ImageAnalyser<FImage> {
049        /**
050         * The SAT data
051         */
052        public FImage data;
053
054        /**
055         * Construct an empty SAT
056         */
057        public SummedAreaTable() {
058        }
059
060        /**
061         * Construct a SAT from the provided image
062         * 
063         * @param image
064         *            the image
065         */
066        public SummedAreaTable(FImage image) {
067                computeTable(image);
068        }
069
070        protected void computeTable(FImage image) {
071                data = new FImage(image.width + 1, image.height + 1);
072
073                for (int y = 0; y < image.height; y++) {
074                        for (int x = 0; x < image.width; x++) {
075                                data.pixels[y + 1][x + 1] = image.pixels[y][x] +
076                                                data.pixels[y + 1][x] +
077                                                data.pixels[y][x + 1] -
078                                                data.pixels[y][x];
079                        }
080                }
081        }
082
083        /**
084         * Calculate the sum of pixels in the image used for constructing this SAT
085         * within the rectangle defined by (x1,y1) [top-left coordinate] and (x2,y2)
086         * [bottom- right coordinate]
087         * 
088         * @param x1
089         *            x1
090         * @param y1
091         *            y1
092         * @param x2
093         *            x2
094         * @param y2
095         *            y2
096         * @return sum of pixels in given rectangle
097         */
098        public float calculateArea(int x1, int y1, int x2, int y2) {
099                final float A = data.pixels[y1][x1];
100                final float B = data.pixels[y1][x2];
101                final float C = data.pixels[y2][x2];
102                final float D = data.pixels[y2][x1];
103
104                return A + C - B - D;
105        }
106
107        /**
108         * Calculate the sum of pixels in the image used for constructing this SAT
109         * within the given rectangle
110         * 
111         * @param r
112         *            rectangle
113         * @return sum of pixels in given rectangle
114         */
115        public float calculateArea(Rectangle r) {
116                return calculateArea(Math.round(r.x), Math.round(r.y), Math.round(r.x + r.width), Math.round(r.y + r.height));
117        }
118
119        /*
120         * (non-Javadoc)
121         * 
122         * @see
123         * org.openimaj.image.analyser.ImageAnalyser#analyseImage(org.openimaj.image
124         * .Image)
125         */
126        @Override
127        public void analyseImage(FImage image) {
128                computeTable(image);
129        }
130}