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.filterbank;
031
032import static java.lang.Math.PI;
033import static java.lang.Math.cos;
034import static java.lang.Math.exp;
035import static java.lang.Math.sqrt;
036
037import org.openimaj.image.FImage;
038import org.openimaj.image.processing.convolution.FConvolution;
039
040/**
041 * Implementation of the MR8 filter bank described in: C. Schmid. Constructing
042 * models for content-based image retrieval. In Proceedings of the IEEE
043 * Conference on Computer Vision and Pattern Recognition, volume 2, pages 39-45,
044 * 2001.
045 * 
046 * Inspired by the matlab implementation from
047 * http://www.robots.ox.ac.uk/~vgg/research/texclass/filters.html
048 * 
049 * @author Jonathon Hare (jsh2@ecs.soton.ac.uk)
050 */
051
052public class SchmidFilterBank extends FilterBank {
053        /**
054         * Default constructor with a support of 49 pixels.
055         */
056        public SchmidFilterBank() {
057                this(49);
058        }
059
060        /**
061         * Construct with given support (filter size).
062         * 
063         * @param size
064         *            the filter size
065         */
066        public SchmidFilterBank(int size) {
067                super(makeFilters(size));
068        }
069
070        protected static FConvolution[] makeFilters(int SUP) {
071                final FConvolution[] F = new FConvolution[13];
072
073                F[0] = makeFilter(SUP, 2, 1);
074                F[1] = makeFilter(SUP, 4, 1);
075                F[2] = makeFilter(SUP, 4, 2);
076                F[3] = makeFilter(SUP, 6, 1);
077                F[4] = makeFilter(SUP, 6, 2);
078                F[5] = makeFilter(SUP, 6, 3);
079                F[6] = makeFilter(SUP, 8, 1);
080                F[7] = makeFilter(SUP, 8, 2);
081                F[8] = makeFilter(SUP, 8, 3);
082                F[9] = makeFilter(SUP, 10, 1);
083                F[10] = makeFilter(SUP, 10, 2);
084                F[11] = makeFilter(SUP, 10, 3);
085                F[12] = makeFilter(SUP, 10, 4);
086
087                return F;
088        }
089
090        private static FConvolution makeFilter(int sup, float sigma, float tau) {
091                final int hs = (sup - 1) / 2;
092
093                final FImage filter = new FImage(sup, sup);
094                for (int y = -hs, j = 0; y < hs; y++, j++) {
095                        for (int x = -hs, i = 0; x < hs; x++, i++) {
096                                final float r = (float) sqrt(x * x + y * y);
097
098                                filter.pixels[j][i] = (float) (cos(r * (PI * tau / sigma)) * exp(-(r * r) / (2 * sigma * sigma)));
099                        }
100                }
101
102                return new FConvolution(LeungMalikFilterBank.normalise(filter));
103        }
104}