View Javadoc

1   /**
2    * Copyright (c) 2011, The University of Southampton and the individual contributors.
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without modification,
6    * are permitted provided that the following conditions are met:
7    *
8    *   * 	Redistributions of source code must retain the above copyright notice,
9    * 	this list of conditions and the following disclaimer.
10   *
11   *   *	Redistributions in binary form must reproduce the above copyright notice,
12   * 	this list of conditions and the following disclaimer in the documentation
13   * 	and/or other materials provided with the distribution.
14   *
15   *   *	Neither the name of the University of Southampton nor the names of its
16   * 	contributors may be used to endorse or promote products derived from this
17   * 	software without specific prior written permission.
18   *
19   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20   * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21   * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22   * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
23   * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24   * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25   * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
26   * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27   * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28   * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29   */
30  package org.openimaj.image.analysis.algorithm;
31  
32  import org.openimaj.image.FImage;
33  import org.openimaj.image.analyser.ImageAnalyser;
34  import org.openimaj.math.geometry.shape.Rectangle;
35  
36  /**
37   * Implementation of an Integral Image or Summed Area Table.
38   * <p>
39   * See http://en.wikipedia.org/wiki/Summed_area_table and
40   * http://research.microsoft
41   * .com/en-us/um/people/viola/Pubs/Detect/violaJones_IJCV.pdf
42   * <p>
43   * Basically, this provides an efficient way to find the sum of all pixels in a
44   * rectangular area of an image.
45   * 
46   * @author Jonathon Hare (jsh2@ecs.soton.ac.uk)
47   */
48  public class SummedAreaTable implements ImageAnalyser<FImage> {
49  	/**
50  	 * The SAT data
51  	 */
52  	public FImage data;
53  
54  	/**
55  	 * Construct an empty SAT
56  	 */
57  	public SummedAreaTable() {
58  	}
59  
60  	/**
61  	 * Construct a SAT from the provided image
62  	 * 
63  	 * @param image
64  	 *            the image
65  	 */
66  	public SummedAreaTable(FImage image) {
67  		computeTable(image);
68  	}
69  
70  	protected void computeTable(FImage image) {
71  		data = new FImage(image.width + 1, image.height + 1);
72  
73  		for (int y = 0; y < image.height; y++) {
74  			for (int x = 0; x < image.width; x++) {
75  				data.pixels[y + 1][x + 1] = image.pixels[y][x] +
76  						data.pixels[y + 1][x] +
77  						data.pixels[y][x + 1] -
78  						data.pixels[y][x];
79  			}
80  		}
81  	}
82  
83  	/**
84  	 * Calculate the sum of pixels in the image used for constructing this SAT
85  	 * within the rectangle defined by (x1,y1) [top-left coordinate] and (x2,y2)
86  	 * [bottom- right coordinate]
87  	 * 
88  	 * @param x1
89  	 *            x1
90  	 * @param y1
91  	 *            y1
92  	 * @param x2
93  	 *            x2
94  	 * @param y2
95  	 *            y2
96  	 * @return sum of pixels in given rectangle
97  	 */
98  	public float calculateArea(int x1, int y1, int x2, int y2) {
99  		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 }