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.processor.SinglebandImageProcessor;
034
035/**
036 * Image processor for FImage capable of performing convolutions with Gaussians.
037 * 
038 * @author Jonathon Hare (jsh2@ecs.soton.ac.uk)
039 */
040public class FGaussianConvolve implements SinglebandImageProcessor<Float, FImage> {
041        /**
042         * The default number of sigmas at which the Gaussian function is truncated
043         * when building a kernel
044         */
045        public static final float DEFAULT_GAUSS_TRUNCATE = 4.0f;
046
047        protected float[] kernel;
048
049        /**
050         * Construct an {@link FGaussianConvolve} with a Gaussian of standard
051         * deviation sigma.
052         * 
053         * @param sigma
054         *            Gaussian kernel standard deviation
055         */
056        public FGaussianConvolve(float sigma) {
057                this(sigma, DEFAULT_GAUSS_TRUNCATE);
058        }
059
060        /**
061         * Construct an {@link FGaussianConvolve} with a Gaussian of standard
062         * deviation sigma. The truncate parameter defines how many sigmas wide the
063         * kernel is.
064         * 
065         * @param sigma
066         * @param truncate
067         */
068        public FGaussianConvolve(float sigma, float truncate) {
069                kernel = makeKernel(sigma, truncate);
070        }
071
072        /**
073         * Construct a zero-mean Gaussian with the specified standard deviation.
074         * 
075         * @param sigma
076         *            the standard deviation of the Gaussian
077         * @return an array representing a Gaussian function
078         */
079        public static float[] makeKernel(float sigma) {
080                return makeKernel(sigma, DEFAULT_GAUSS_TRUNCATE);
081        }
082
083        /**
084         * Construct a zero-mean Gaussian with the specified standard deviation.
085         * 
086         * @param sigma
087         *            the standard deviation of the Gaussian
088         * @param truncate
089         *            the number of sigmas from the centre at which to truncate the
090         *            Gaussian
091         * @return an array representing a Gaussian function
092         */
093        public static float[] makeKernel(float sigma, float truncate) {
094                if (sigma == 0)
095                        return new float[] { 1f };
096                // The kernel is truncated at truncate sigmas from center.
097                int ksize = (int) (2.0f * truncate * sigma + 1.0f);
098                // ksize = Math.max(1, ksize); // size must be at least 3
099                if (ksize % 2 == 0)
100                        ksize++; // size must be odd
101
102                final float[] kernel = new float[ksize];
103
104                // build kernel
105                float sum = 0.0f;
106                for (int i = 0; i < ksize; i++) {
107                        final float x = i - ksize / 2;
108                        kernel[i] = (float) Math.exp(-x * x / (2.0 * sigma * sigma));
109                        sum += kernel[i];
110                }
111
112                // normalise area to 1
113                for (int i = 0; i < ksize; i++) {
114                        kernel[i] /= sum;
115                }
116
117                return kernel;
118        }
119
120        /*
121         * (non-Javadoc)
122         * 
123         * @see
124         * org.openimaj.image.processor.ImageProcessor#processImage(org.openimaj
125         * .image.Image)
126         */
127        @Override
128        public void processImage(FImage image) {
129                FImageConvolveSeparable.convolveHorizontal(image, kernel);
130                FImageConvolveSeparable.convolveVertical(image, kernel);
131        }
132}