[ VIGRA Homepage | Function Index | Class Index | Namespaces | File List | Main Page ]

vigra/gaussians.hxx

00001 /************************************************************************/
00002 /*                                                                      */
00003 /*               Copyright 1998-2004 by Ullrich Koethe                  */
00004 /*       Cognitive Systems Group, University of Hamburg, Germany        */
00005 /*                                                                      */
00006 /*    This file is part of the VIGRA computer vision library.           */
00007 /*    The VIGRA Website is                                              */
00008 /*        http://kogs-www.informatik.uni-hamburg.de/~koethe/vigra/      */
00009 /*    Please direct questions, bug reports, and contributions to        */
00010 /*        ullrich.koethe@iwr.uni-heidelberg.de    or                    */
00011 /*        vigra@informatik.uni-hamburg.de                               */
00012 /*                                                                      */
00013 /*    Permission is hereby granted, free of charge, to any person       */
00014 /*    obtaining a copy of this software and associated documentation    */
00015 /*    files (the "Software"), to deal in the Software without           */
00016 /*    restriction, including without limitation the rights to use,      */
00017 /*    copy, modify, merge, publish, distribute, sublicense, and/or      */
00018 /*    sell copies of the Software, and to permit persons to whom the    */
00019 /*    Software is furnished to do so, subject to the following          */
00020 /*    conditions:                                                       */
00021 /*                                                                      */
00022 /*    The above copyright notice and this permission notice shall be    */
00023 /*    included in all copies or substantial portions of the             */
00024 /*    Software.                                                         */
00025 /*                                                                      */
00026 /*    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND    */
00027 /*    EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES   */
00028 /*    OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND          */
00029 /*    NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT       */
00030 /*    HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,      */
00031 /*    WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING      */
00032 /*    FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR     */
00033 /*    OTHER DEALINGS IN THE SOFTWARE.                                   */                
00034 /*                                                                      */
00035 /************************************************************************/
00036 
00037 #ifndef VIGRA_GAUSSIANS_HXX
00038 #define VIGRA_GAUSSIANS_HXX
00039 
00040 #include <cmath>
00041 #include "config.hxx"
00042 #include "mathutil.hxx"
00043 #include "array_vector.hxx"
00044 #include "error.hxx"
00045 
00046 namespace vigra {
00047 
00048 #if 0
00049 /** \addtogroup MathFunctions Mathematical Functions
00050 */
00051 //@{
00052 #endif
00053 /*! The Gaussian function and its derivatives.
00054 
00055     Implemented as a unary functor. Since it supports the <tt>radius()</tt> function
00056     it can also be used as a kernel in \ref resamplingConvolveImage().
00057 
00058     <b>\#include</b> <<a href="gaussians_8hxx-source.html">vigra/gaussians.hxx</a>><br>
00059     Namespace: vigra
00060 
00061     \ingroup MathFunctions
00062 */
00063 template <class T = double>
00064 class Gaussian
00065 {
00066   public:
00067 
00068         /** the value type if used as a kernel in \ref resamplingConvolveImage().
00069         */
00070     typedef T            value_type;
00071         /** the functor's argument type
00072         */
00073     typedef T            argument_type;
00074         /** the functor's result type
00075         */
00076     typedef T            result_type;
00077 
00078         /** Create functor for the given standard deviation <tt>sigma</tt> and
00079             derivative order <i>n</i>. The functor then realizes the function
00080 
00081             \f[ f_{\sigma,n}(x)=\frac{\partial^n}{\partial x^n}
00082                  \frac{1}{\sqrt{2\pi}\sigma}e^{-\frac{x^2}{2\sigma^2}}
00083             \f]
00084 
00085             Precondition:
00086             \code
00087             sigma > 0.0
00088             \endcode
00089         */
00090     explicit Gaussian(T sigma = 1.0, unsigned int derivativeOrder = 0)
00091     : sigma_(sigma),
00092       sigma2_(-0.5 / sigma / sigma),
00093       norm_(0.0),
00094       order_(derivativeOrder),
00095       hermitePolynomial_(derivativeOrder / 2 + 1)
00096     {
00097         vigra_precondition(sigma_ > 0.0,
00098             "Gaussian::Gaussian(): sigma > 0 required.");
00099         switch(order_)
00100         {
00101             case 1:
00102             case 2:
00103                 norm_ = -1.0 / (VIGRA_CSTD::sqrt(2.0 * M_PI) * sq(sigma) * sigma);
00104                 break;
00105             case 3:
00106                 norm_ = 1.0 / (VIGRA_CSTD::sqrt(2.0 * M_PI) * sq(sigma) * sq(sigma) * sigma);
00107                 break;
00108             default:
00109                 norm_ = 1.0 / VIGRA_CSTD::sqrt(2.0 * M_PI) / sigma;
00110         }
00111         calculateHermitePolynomial();
00112     }
00113 
00114         /** Function (functor) call.
00115         */
00116     result_type operator()(argument_type x) const;
00117 
00118         /** Get the standard deviation of the Gaussian.
00119         */
00120     value_type sigma() const
00121         { return sigma_; }
00122 
00123         /** Get the derivative order of the Gaussian.
00124         */
00125     unsigned int derivativeOrder() const
00126         { return order_; }
00127 
00128         /** Get the required filter radius for a discrete approximation of the Gaussian.
00129             The radius is given as a multiple of the Gaussian's standard deviation
00130             (default: <tt>sigma * (3 + 1/2 * derivativeOrder()</tt> -- the second term
00131             accounts for the fact that the derivatives of the Gaussian become wider
00132             with increasing order). The result is rounded to the next higher integer.
00133         */
00134     double radius(double sigmaMultiple = 3.0) const
00135         { return VIGRA_CSTD::ceil(sigma_ * (sigmaMultiple + 0.5 * derivativeOrder())); }
00136 
00137   private:
00138     void calculateHermitePolynomial();
00139     T horner(T x) const;
00140 
00141     T sigma_, sigma2_, norm_;
00142     unsigned int order_;
00143     ArrayVector<T> hermitePolynomial_;
00144 };
00145 
00146 template <class T>
00147 typename Gaussian<T>::result_type
00148 Gaussian<T>::operator()(argument_type x) const
00149 {
00150     T x2 = x * x;
00151     T g  = norm_ * VIGRA_CSTD::exp(x2 * sigma2_);
00152     switch(order_)
00153     {
00154         case 0:
00155             return g;
00156         case 1:
00157             return x * g;
00158         case 2:
00159             return (1.0 - sq(x / sigma_)) * g;
00160         case 3:
00161             return (3.0 - sq(x / sigma_)) * x * g;
00162         default:
00163             return order_ % 2 == 0 ?
00164                        g * horner(x2)
00165                      : x * g * horner(x2);
00166     }
00167 }
00168 
00169 template <class T>
00170 T Gaussian<T>::horner(T x) const
00171 {
00172     int i = order_ / 2;
00173     T res = hermitePolynomial_[i];
00174     for(--i; i >= 0; --i)
00175         res = x * res + hermitePolynomial_[i];
00176     return res;
00177 }
00178 
00179 template <class T>
00180 void Gaussian<T>::calculateHermitePolynomial()
00181 {
00182     if(order_ == 0)
00183     {
00184         hermitePolynomial_[0] = 1.0;
00185     }
00186     else if(order_ == 1)
00187     {
00188         hermitePolynomial_[0] = -1.0 / sigma_ / sigma_;
00189     }
00190     else
00191     {
00192         // calculate Hermite polynomial for requested derivative
00193         // recursively according to
00194         //     (0)
00195         //    h   (x) = 1
00196         //
00197         //     (1)
00198         //    h   (x) = -x / s^2
00199         //
00200         //     (n+1)                        (n)           (n-1)
00201         //    h     (x) = -1 / s^2 * [ x * h   (x) + n * h     (x) ]
00202         //
00203         T s2 = -1.0 / sigma_ / sigma_;
00204         ArrayVector<T> hn(3*order_+3, 0.0);
00205         typename ArrayVector<T>::iterator hn0 = hn.begin(),
00206                                           hn1 = hn0 + order_+1,
00207                                           hn2 = hn1 + order_+1,
00208                                           ht;
00209         hn2[0] = 1.0;
00210         hn1[1] = s2;
00211         for(unsigned int i = 2; i <= order_; ++i)
00212         {
00213             hn0[0] = s2 * (i-1) * hn2[0];
00214             for(unsigned int j = 1; j <= i; ++j)
00215                 hn0[j] = s2 * (hn1[j-1] + (i-1) * hn2[j]);
00216             ht = hn2;
00217             hn2 = hn1;
00218             hn1 = hn0;
00219             hn0 = ht;
00220         }
00221         // keep only non-zero coefficients of the polynomial
00222         for(unsigned int i = 0; i < hermitePolynomial_.size(); ++i)
00223             hermitePolynomial_[i] = order_ % 2 == 0 ?
00224                                          hn1[2*i]
00225                                        : hn1[2*i+1];
00226     }
00227 }
00228 
00229 
00230 ////@}
00231 
00232 } // namespace vigra
00233 
00234 
00235 #endif /* VIGRA_GAUSSIANS_HXX */

© Ullrich Köthe (ullrich.koethe@iwr.uni-heidelberg.de)
Heidelberg Collaboratory for Image Processing, University of Heidelberg, Germany

html generated using doxygen and Python
VIGRA 1.6.0 (5 Nov 2009)