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.histogram; 31 32 import org.openimaj.image.FImage; 33 import org.openimaj.image.analysis.algorithm.SummedAreaTable; 34 import org.openimaj.math.geometry.shape.Rectangle; 35 import org.openimaj.math.statistics.distribution.Histogram; 36 37 /** 38 * This class implements a {@link WindowedHistogramExtractor} with the primary 39 * purpose of of producing efficient access to histograms of arbitrary windows 40 * of the image. 41 * <p> 42 * This implementation is based on a stack of {@link SummedAreaTable}s, with one 43 * {@link SummedAreaTable} per histogram bin. Obviously this is quite memory 44 * intensive, so should probably only be used with small numbers of bins. 45 * However, the advantage over a {@link BinnedWindowedExtractor} is that the 46 * histogram extraction is an O(1) operation, and it is thus very quick for 47 * evaluating many windows. 48 * 49 * @author Jonathon Hare (jsh2@ecs.soton.ac.uk) 50 */ 51 public class SATWindowedExtractor implements WindowedHistogramExtractor { 52 protected final SummedAreaTable[] sats; 53 protected final int nbins; 54 55 /** 56 * Protected constructor for subclasses to use if they don't wan't to 57 * compute the SATs at construction time 58 * 59 * @param nbins 60 * the number of histogram bins. 61 */ 62 protected SATWindowedExtractor(int nbins) { 63 this.nbins = nbins; 64 this.sats = new SummedAreaTable[nbins]; 65 } 66 67 /** 68 * Construct with the given spatial histogram magnitude maps. Each image 69 * represents a bin of the histogram, and each element is the histogram 70 * weight at the respective spatial location for the corresponding bin 71 * (usually the images will be very sparse). 72 * 73 * @param magnitudeMaps 74 * array of images, one per bin, with each pixel set to the 75 * histogram magnitude at that bin 76 */ 77 public SATWindowedExtractor(FImage[] magnitudeMaps) { 78 this.nbins = magnitudeMaps.length; 79 80 sats = new SummedAreaTable[nbins]; 81 computeSATs(magnitudeMaps); 82 } 83 84 protected void computeSATs(FImage[] magnitudeMaps) { 85 for (int i = 0; i < nbins; i++) { 86 sats[i] = new SummedAreaTable(magnitudeMaps[i]); 87 } 88 } 89 90 /* 91 * (non-Javadoc) 92 * 93 * @see 94 * org.openimaj.image.analysis.algorithm.ImageHistogramAnalyser#getNumBins() 95 */ 96 @Override 97 public int getNumBins() { 98 return nbins; 99 } 100 101 /* 102 * (non-Javadoc) 103 * 104 * @see 105 * org.openimaj.image.analysis.algorithm.ImageHistogramAnalyser#computeHistogram 106 * (org.openimaj.math.geometry.shape.Rectangle) 107 */ 108 @Override 109 public Histogram computeHistogram(Rectangle roi) { 110 return computeHistogram((int) roi.x, (int) roi.y, (int) roi.width, (int) roi.height); 111 } 112 113 /* 114 * (non-Javadoc) 115 * 116 * @see 117 * org.openimaj.image.analysis.algorithm.ImageHistogramAnalyser#computeHistogram 118 * (int, int, int, int) 119 */ 120 @Override 121 public Histogram computeHistogram(final int x, final int y, final int w, final int h) { 122 final Histogram hist = new Histogram(nbins); 123 final int x2 = x + w; 124 final int y2 = y + h; 125 126 for (int i = 0; i < nbins; i++) { 127 final float val = sats[i].calculateArea(x, y, x2, y2); 128 // rounding errors in the SAT can lead to small values that should 129 // actually be zero 130 hist.values[i] = val < 1e-4 ? 0 : val; 131 132 } 133 134 return hist; 135 } 136 137 @Override 138 public void computeHistogram(final int x, final int y, final int w, final int h, final Histogram hist) { 139 final int x2 = x + w; 140 final int y2 = y + h; 141 final double[] values = hist.values; 142 143 for (int i = 0; i < values.length; i++) { 144 final float val = sats[i].calculateArea(x, y, x2, y2); 145 values[i] = Math.max(0, val); // rounding errors in the SAT 146 // might lead to small -ve's 147 } 148 } 149 150 @Override 151 public void computeHistogram(Rectangle roi, Histogram histogram) { 152 computeHistogram((int) roi.x, (int) roi.y, (int) roi.width, (int) roi.height, histogram); 153 } 154 }