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;
033
034/**
035 * A set of standard derivative kernels. These kernels help estimate the derivative over various orders at a point in a matrix. 
036 * This is approximated by applying a finite difference derivative operation on a gaussian kernel with a very low sigma. i.e. a gaussian
037 * kernel that looks like:
038 * 
039 * [
040 *      [0,0,0],
041 *  [0,1,0],
042 *  [0,0,0]
043 * ]
044 * 
045 * By successive derivative calculations in the x direction and y direction it is possible to estimate derivatives in both directions as well.
046 * 
047 * @author Jonathon Hare (jsh2@ecs.soton.ac.uk)
048 *
049 */
050public class BasicDerivativeKernels {
051        static class DxKernel extends FConvolution {
052                public DxKernel() { super(new FImage(new float[][] {{-0.5f,0,0.5f}})); }
053        }
054        
055        static class DyKernel extends FConvolution {
056                public DyKernel() { super(new FImage(new float[][] {{-0.5f}, {0}, {0.5f}})); }
057        }
058
059        static class DxxKernel extends FConvolution {
060                public DxxKernel() { super(new FImage(new float[][] {{1,-2,1}})); }
061        }
062        
063        static class DxyKernel extends FConvolution {
064                public DxyKernel() { super(new FImage(new float[][] {{0.25f,0,-0.25f}, {0,0,0}, {-0.25f,0,0.25f}})); }
065        }
066        
067        static class DyyKernel extends FConvolution {
068                public DyyKernel() { super(new FImage(new float[][] {{1}, {-2}, {1}})); }
069        }
070        
071        static class DxxxxKernel extends FConvolution {
072                public DxxxxKernel() { super(new FImage(new float[][] {{1,-4 ,6 ,-4 ,1}})); }
073        }
074        
075        static class DyyyyKernel extends FConvolution {
076                public DyyyyKernel() { super(new FImage(new float[][] {{1}, {-4},{6},{-4},{1}})); }
077        }
078        
079        static class DxxyyKernel extends FConvolution {
080                public DxxyyKernel() { super(new FImage(new float[][] {{1f,-2f,1f},{-2f,4f,-2f},{1f,-2f,1f}})); }
081        }
082        
083        /**
084         * kernel approximating the first derivative of a low-sigma gaussian in the x-direction [-0.5, 0, 0.5]. 
085         * Useful for giving an estimate of the second derivative in x of any given point
086         */
087        public static final FConvolution  DX_KERNEL  = new DxKernel();
088        
089        /**
090         * kernel approximating the first derivative of a low-sigma gaussian in the y-direction [-0.5, 0, 0.5]'. 
091         * Useful for giving an estimate of the second derivative in y of any given point
092         */
093        public static final FConvolution DY_KERNEL  = new DyKernel();
094        
095        /**
096         * kernel approximating the second derivative of a low sigma gaussian in the x-direction [1, -2, 1]. 
097         * Useful for giving an estimate of the second derivative in x of any given point
098         */
099        public static final FConvolution DXX_KERNEL = new DxxKernel();
100        
101        /**
102         * kernel approximating the first derivative of a low sigma gaussian in the x-direction and y-direction [[-0.25, 0, 0.25], [0, 0, 0], [0.25, 0, -0.25]] . 
103         * Useful for giving an estimate of the first order derivative in x then y of any given point
104         */
105        public static final FConvolution DXY_KERNEL = new DxyKernel();
106        
107        /**
108         * kernel approximating the second derivative of a low sigma gaussian in the y-direction [1, -2, 1]'. 
109         * Useful for giving an estimate of the second derivative in y of any given point
110         */
111        public static final FConvolution DYY_KERNEL = new DyyKernel();
112        
113        
114        /**
115         * kernel approximating the fourth derivative of a low sigma gaussian in the x-direction [1,-4,6,-4,1]^T
116         * Useful for giving an estimate of the fourth derivative in y of any given point
117         */
118        public static final FConvolution DXXXX_KERNEL = new DxxxxKernel();
119        
120        /**
121         * kernel approximating the second derivative of a low sigma gaussian in the x-direction and y-direction [[1,-2,1],[-2,4,-2],[1,-2,1]] . 
122         * Useful for giving an estimate of the second order derivative in x then y of any given point
123         */
124        public static final FConvolution DXXYY_KERNEL = new DxxyyKernel();
125        /**
126         * kernel approximating the fourth derivative of a low sigma gaussian in the y-direction [1,-4,6,-4,1]^T
127         * Useful for giving an estimate of the fourth derivative in y of any given point
128         */
129        public static final FConvolution DYYYY_KERNEL = new DyyyyKernel();
130}