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.util.array.ArrayUtils; 034 035/** 036 * Utility methods for creating Gabor Filters 037 * 038 * @author Jonathon Hare (jsh2@ecs.soton.ac.uk) 039 * 040 */ 041public class GaborFilters { 042 private GaborFilters() { 043 }; 044 045 /** 046 * Create a jet of (multiscale) Gabor filters in the frequency domain. The 047 * returned filters have the highest frequency in the centre; to apply them 048 * to an image, use 049 * {@link FourierConvolve#convolvePrepared(FImage, FImage, boolean)} with 050 * the third argument set to true. 051 * 052 * @param width 053 * the width of the image that will be filtered (note that the 054 * returned filters will have twice this width to account of the 055 * imaginary (phase) values [all of which are zero]) 056 * @param height 057 * the height of the image that will be filtered 058 * @param orientationsPerScale 059 * the number of filter orientations for each scale (from HF to 060 * LF) 061 * @return the jet of filters 062 */ 063 public static FImage[] createGaborJets(int width, int height, int... orientationsPerScale) { 064 final int nscales = orientationsPerScale.length; 065 final int nfilters = (int) ArrayUtils.sumValues(orientationsPerScale); 066 067 final FImage[] filters = new FImage[nfilters]; 068 069 final double[][] param = new double[nfilters][]; 070 for (int i = 0, l = 0; i < nscales; i++) { 071 for (int j = 0; j < orientationsPerScale[i]; j++) { 072 param[l++] = new double[] { 073 .35, 074 .3 / Math.pow(1.85, i), 075 16.0 * orientationsPerScale[i] * orientationsPerScale[i] / (32.0 * 32.0), 076 Math.PI / (orientationsPerScale[i]) * j 077 }; 078 } 079 } 080 081 final double[][] freq = new double[height][width]; 082 final double[][] phase = new double[height][width]; 083 084 final float hw = width / 2f; 085 final float hh = height / 2f; 086 087 for (int y = 0; y < height; y++) { 088 for (int x = 0; x < width; x++) { 089 final float fx = x - hw; 090 final float fy = y - hh; 091 freq[y][x] = Math.sqrt(fx * fx + fy * fy); 092 phase[y][x] = Math.atan2(fy, fx); 093 } 094 } 095 096 for (int i = 0; i < nfilters; i++) { 097 filters[i] = new FImage(width * 2, height); 098 for (int y = 0; y < height; y++) { 099 for (int x = 0; x < width; x++) { 100 double tr = phase[y][x] + param[i][3]; 101 102 if (tr > Math.PI) 103 tr -= 2 * Math.PI; 104 else if (tr < -Math.PI) 105 tr += 2 * Math.PI; 106 107 filters[i].pixels[y][x * 2] = (float) Math.exp(-10 * param[i][0] * 108 (freq[y][x] / width / param[i][1] - 1) * (freq[y][x] / width / param[i][1] - 1) 109 - 2 * param[i][2] * Math.PI * tr * tr 110 ); 111 } 112 } 113 } 114 115 return filters; 116 } 117}