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.processing.convolution; 031 032import org.openimaj.image.FImage; 033import org.openimaj.image.analysis.algorithm.SummedAreaTable; 034import org.openimaj.image.processor.SinglebandImageProcessor; 035 036/** 037 * A rectangular averaging convolution operator (often known as a Box filter). 038 * For efficiency, this is implemented using a {@link SummedAreaTable} rather 039 * than through an actual convolution. 040 * 041 * @author Jonathon Hare (jsh2@ecs.soton.ac.uk) 042 */ 043public class AverageBoxFilter implements SinglebandImageProcessor<Float, FImage> { 044 private int width; 045 private int height; 046 047 /** 048 * Construct the averaging operator with a kernel of the given dimensions. 049 * 050 * @param width 051 * width of the kernel 052 * @param height 053 * height of the kernel 054 */ 055 public AverageBoxFilter(int width, int height) { 056 this.width = width; 057 this.height = height; 058 } 059 060 /** 061 * Construct the averaging operator with a square kernel of the given 062 * dimension. 063 * 064 * @param dim 065 * The width and height of the box 066 */ 067 public AverageBoxFilter(int dim) { 068 this(dim, dim); 069 } 070 071 @Override 072 public void processImage(FImage image) { 073 // shortcut trivial case 074 if (this.height == 1 && this.width == 1) 075 return; 076 077 final SummedAreaTable sat = new SummedAreaTable(); 078 sat.analyseImage(image); 079 080 final int hw = width / 2; 081 final int hh = height / 2; 082 083 for (int y = 0; y < image.height; y++) { 084 for (int x = 0; x < image.width; x++) { 085 final int sx = Math.max(0, x - hw); 086 final int sy = Math.max(0, y - hh); 087 final int ex = Math.min(image.width, x + hw + 1); 088 final int ey = Math.min(image.height, y + hh + 1); 089 090 final int area = (ex - sx) * (ey - sy); 091 final float mean = sat.calculateArea(sx, sy, ex, ey) / area; 092 image.pixels[y][x] = mean; 093 } 094 } 095 } 096}