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.algorithm; 031 032import java.util.HashSet; 033import java.util.Set; 034 035import org.openimaj.image.pixel.ConnectedComponent; 036import org.openimaj.image.pixel.Pixel; 037 038/** 039 * Methods and statically defined templates for defining the support of local 040 * image filters. 041 * 042 * @author Jonathon Hare (jsh2@ecs.soton.ac.uk) 043 */ 044public class FilterSupport { 045 046 /** 047 * Offsets for using a 3x3 cross shaped mask to select pixels for computing 048 * median. 049 */ 050 public static final Set<Pixel> CROSS_3x3 = new HashSet<Pixel>(); 051 static { 052 CROSS_3x3.add(new Pixel(0, -1)); 053 CROSS_3x3.add(new Pixel(-1, 0)); 054 CROSS_3x3.add(new Pixel(0, 0)); 055 CROSS_3x3.add(new Pixel(1, 0)); 056 CROSS_3x3.add(new Pixel(0, 1)); 057 } 058 059 /** 060 * Offsets for using a 3x3 blocked shaped mask to select pixels for 061 * computing median. 062 */ 063 public static final Set<Pixel> BLOCK_3x3 = createBlockSupport(3, 3); 064 065 /** 066 * Create a a rectangular support. 067 * 068 * @param width 069 * the width of the support 070 * @param height 071 * the height of the support 072 * @return the support 073 */ 074 public static final Set<Pixel> createBlockSupport(final int width, final int height) { 075 final HashSet<Pixel> indices = new HashSet<Pixel>(width * height); 076 077 final int startX = -width / 2; 078 final int startY = -height / 2; 079 080 for (int y = 0; y < height; y++) { 081 for (int x = 0; x < width; x++) { 082 indices.add(new Pixel(startX + x, startY + y)); 083 } 084 } 085 086 return indices; 087 } 088 089 /** 090 * Test whether the given support is a centred block 091 * 092 * @param support 093 * the support 094 * @return true if block, false otherwise 095 */ 096 public final static boolean isBlockSupport(final Set<Pixel> support) { 097 final int sw = getSupportWidth(support); 098 final int sh = getSupportHeight(support); 099 100 return sw * sh == support.size() && isCentred(support); 101 } 102 103 private static boolean isCentred(Set<Pixel> support) { 104 final ConnectedComponent cc = new ConnectedComponent(support); 105 final Pixel cp = cc.calculateCentroidPixel(); 106 return cp.x == 0 && cp.y == 0; 107 } 108 109 /** 110 * Get the width of the support region 111 * 112 * @param support 113 * the region 114 * @return the width 115 */ 116 public final static int getSupportWidth(final Set<Pixel> support) { 117 int min = Integer.MAX_VALUE; 118 int max = -Integer.MAX_VALUE; 119 120 for (final Pixel p : support) { 121 min = Math.min(min, p.x); 122 max = Math.max(max, p.x); 123 } 124 125 return max - min + 1; 126 } 127 128 /** 129 * Get the height of the support region 130 * 131 * @param support 132 * the region 133 * @return the height 134 */ 135 public final static int getSupportHeight(final Set<Pixel> support) { 136 int min = Integer.MAX_VALUE; 137 int max = -Integer.MAX_VALUE; 138 139 for (final Pixel p : support) { 140 min = Math.min(min, p.y); 141 max = Math.max(max, p.y); 142 } 143 144 return max - min + 1; 145 } 146}