LTI-Lib latest version v1.9 - last update 10 Apr 2010

ltiConvolutionHelper.h

00001 /*
00002  * Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006
00003  * Lehrstuhl fuer Technische Informatik, RWTH-Aachen, Germany
00004  *
00005  * This file is part of the LTI-Computer Vision Library (LTI-Lib)
00006  *
00007  * The LTI-Lib is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public License (LGPL)
00009  * as published by the Free Software Foundation; either version 2.1 of
00010  * the License, or (at your option) any later version.
00011  *
00012  * The LTI-Lib is distributed in the hope that it will be
00013  * useful, but WITHOUT ANY WARRANTY; without even the implied warranty
00014  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with the LTI-Lib; see the file LICENSE.  If
00019  * not, write to the Free Software Foundation, Inc., 59 Temple Place -
00020  * Suite 330, Boston, MA 02111-1307, USA.
00021  */
00022 
00023 
00024 /*----------------------------------------------------------------
00025  * project ....: LTI Digital Image/Signal Processing Library
00026  * file .......: ltiConvolutionHelper.h
00027  * authors ....: Pablo Alvarado, Bodo Hoffmanna
00028  * organization: LTI, RWTH Aachen
00029  * creation ...: 19.7.2000
00030  * revisions ..: $Id: ltiConvolutionHelper.h,v 1.4 2006/02/07 18:41:27 ltilib Exp $
00031  */
00032 
00033 #ifndef _LTI_CONVOLUTION_HELPER_H_
00034 #define _LTI_CONVOLUTION_HELPER_H_
00035 
00036 #include "ltiObject.h"
00037 #include "ltiMath.h"
00038 #include "ltiImage.h"
00039 #include "ltiLinearKernels.h"
00040 #include "ltiModifier.h"
00041 #include "ltiMorphology.h"
00042 #include "ltiContour.h"
00043 #include "ltiKernelType.h"
00044 
00045 namespace lti {
00046   /**
00047    * pre-convolution algorithm for a vector * kernel1D
00048    * The type T is the type of the elements of the vector and kernel1D
00049    * The class A determines how the values should be accumulated.
00050    * The class A must have following  methods:
00051    *
00052    *  - accumulate(T kernelElement,T source)
00053    *    accumulate the result of applying some function to one element
00054    *    of the kernel and one element of the source channel.
00055    *  - accumulateSym(T kernelElement,T source1, T source2)
00056    *    accumulate the result of applying some function to one element of a
00057    *    symmetric kernel and the corresponding elements of the source
00058    *    channel.
00059    *  - accumulateASym(T kernelElement,T source1, T source2)
00060    *    accumulate the result of applying some function to one element of a
00061    *    anti-symmetric kernel (e.g. a gradient) and the corresponding elements
00062    *    of the source channel.
00063    *  - accumulateZero(T source) 
00064    *    accumulate the result of applying some function with kernel value
00065    *    zero to the source channel. For efficient implementation of 
00066    *    accumulateASym() where middle kernel value is zero by definition.
00067    *    For filters this is an empty function, however, not for others.
00068    *  - getResult()
00069    *    returns the accumulated value
00070    *  - reset()
00071    *    reset the accumulated value
00072    *  - setNorm()
00073    *    set the normalization constant used by the getResult() method
00074    *
00075    * Examples of the accumulator class can be found in:
00076    * lti::dilation::accumulatorGray, lti::dilation::accumulatorBin,
00077    * lti::erosion::accumulatorGray, lti::erosion::accumulatorBin,
00078    * lti::convolution::accumulator
00079    *
00080    * The implementation of these methods allows the efficient use
00081    * of this class for the implementation of convolution, dilation and
00082    * erosion.
00083    *
00084    * Please note that this class is intended to be used just as a helper class.
00085    * The correctness of the parameters and input data must be ensured by the
00086    * "helped" classes before these methods are called.
00087    *
00088    */
00089   template <class T,class A>
00090   class convHelper1D {
00091   public:
00092     /**
00093      * default constructor
00094      */
00095     convHelper1D();
00096 
00097     /**
00098      * destructor
00099      */
00100     ~convHelper1D();
00101 
00102     /**
00103      * try to get a Kernel from the parameters.
00104      * If it is not possible to get it, return false;
00105      * This will not copy the kernel, it just will keep a reference to it!
00106      * @param kern the mathObject with the kernel.
00107      * @return true if kernel could be set and false otherwise
00108      */
00109     bool setKernel(const mathObject* kern);
00110 
00111     /**
00112      * convHelpers the vector src with the filter kernel and
00113      * leaves the result in dest.
00114      *
00115      * Consistency checks should be done by the apply methods!
00116      *
00117      * @param src vector to be filtered
00118      * @param dest destination vector for the result
00119      * @param param parameters with the boundaryType to be used
00120      */
00121     void apply(const vector<T>& src,
00122                      vector<T>& dest,
00123                const modifier::parameters& param);
00124 
00125     /**
00126      * convHelpers the columns of matrix src with the filter kernel and
00127      * leaves the result in dest.
00128      *
00129      * Consistency checks should be done by the apply methods!
00130      *
00131      * @param src matrix to be filtered
00132      * @param dest destination matrix for the result
00133      * @param param parameters with the boundaryType to be used
00134      */
00135     void applyCol(const matrix<T>& src,
00136                         matrix<T>& dest,
00137                   const modifier::parameters& param);
00138 
00139     /**
00140      * convHelpers the rows of matrix src with the filter kernel and
00141      * leaves the result in dest.
00142      *
00143      * Consistency checks should be done by the apply methods!
00144      *
00145      * @param src matrix to be filtered
00146      * @param dest destination matrix for the result
00147      * @param param parameters with the boundaryType to be used
00148      */
00149     void applyRow(const matrix<T>& src,
00150                         matrix<T>& dest,
00151                   const modifier::parameters& param);
00152 
00153     /**
00154      * returns a reference to the accumulator object being used
00155      */
00156     A& getAccumulator();
00157 
00158     /**
00159      * returns a reference to the accumulator object being used
00160      */
00161     const A& getAccumulator() const;
00162 
00163   protected:
00164     const kernel1D<T>* kernel;
00165     A lresult;
00166     eKernelType kernelType;
00167 
00168     private:
00169     kernel1D<T>* tmpKernel;
00170     
00171 
00172     /**
00173      * Does the work of apply() if the kernel is larger than the src.
00174      */
00175     void applyLargeFilter(const vector<T>& src,
00176                           vector<T>& dest,
00177                           const modifier::parameters& param);
00178 
00179     /**
00180      * Does the work of applyCol() if the kernel is larger than the src.
00181      */
00182     void applyLargeFilterCol(const matrix<T>& src,
00183                              matrix<T>& dest,
00184                              const modifier::parameters& param);
00185     
00186       
00187   };
00188 
00189   /**
00190    * pre-convolution algorithm for a matrix * kernel2D
00191    * The type T is the type of the elements of the vector an kernel1D
00192    * The class A determines how the values should be accumulated.
00193    * The class A must have following methods:
00194    *
00195    *  - accumulate(T kernelElement,T source)
00196    *    accumulate the result of applying some function to one element
00197    *    of the kernel and one element of the source channel.
00198    *  - accumulateSym(T kernelElement,T source1, T source2)
00199    *    accumulate the result of applying some function to one element of a
00200    *    symmetric kernel and the corresponding elements of the source
00201    *    channel.
00202    *  - accumulateASym(T kernelElement,T source1, T source2)
00203    *    accumulate the result of applying some function to one element of a
00204    *    anti-symmetric kernel (e.g. a gradient) and the corresponding elements
00205    *    of the source channel.
00206    *  - accumulateZero(T source) 
00207    *    accumulate the result of applying some function with kernel value
00208    *    zero to the source channel. For efficient implementation of 
00209    *    accumulateASym() where middle kernel value is zero by definition.
00210    *    For filters this is an empty function, however, not for others.
00211    *  - getResult()
00212    *    returns the accumulated value
00213    *  - reset()
00214    *    reset the accumulated value
00215    *  - setNorm()
00216    *    set the normalization constant used by the getResult() method
00217    *
00218    * Examples of the accumulator class can be found in:
00219    * lti::dilation::accumulatorGray, lti::dilation::accumulatorBin,
00220    * lti::erosion::accumulatorGray, lti::erosion::accumulatorBin,
00221    * lti::convolution::accumulator
00222    *
00223    * The implementation of these methods allows the efficient use
00224    * of this class for the implementation of convolution, dilation and
00225    * erosion.
00226    *
00227    * Please note that this class is intended to be used just as a helper class.
00228    * The correctness of the parameters and input data must be ensured by the
00229    * "helped" classes before these methods are called.
00230    *
00231    */
00232   template <class T,class A>
00233     class convHelper2D  {
00234     public:
00235     /**
00236      * default constructor
00237      */
00238     convHelper2D();
00239 
00240     /**
00241      * destructor
00242      */
00243     ~convHelper2D();
00244 
00245     /**
00246      * try to get a Kernel from the parameters.
00247      * If it is not possible to get it, return false;
00248      * This will not copy the kernel, it just will keep a reference to it!
00249      * @param kern the mathObject with the kernel.
00250      * @return true if kernel could be set and false otherwise
00251      */
00252     bool setKernel(const mathObject* kern);
00253 
00254     /**
00255      * get kernel type
00256      */
00257     inline bool isKernelSeparable() const;
00258 
00259     /**
00260      * convHelpers the matrix src with the filter kernel and
00261      * leaves the result in dest.
00262      *
00263      * Consistency checks should be done by the apply methods!
00264      *
00265      * @param src matrix to be filtered
00266      * @param dest result
00267      * @param param parameters with the boundaryType to be used
00268      */
00269     void apply(const matrix<T>& src,
00270                matrix<T>& dest,
00271                const modifier::parameters& param);
00272 
00273     /**
00274      * convHelpers the matrix src with the separable filter kernel and
00275      * leaves the result in dest.
00276      *
00277      * Consistency checks should be done by the apply methods!
00278      *
00279      * @param src matrix to be filtered
00280      * @param dest result
00281      * @param param parameters with the boundaryType to be used
00282      */
00283     void applySep(const matrix<T>& src,
00284                   matrix<T>& dest,
00285                   const modifier::parameters& param);
00286     /**
00287      * convolves the matrix src with the object depicted in the filter
00288      * kernel and described by the mask. Leaves the result in dest.
00289      *
00290      * Consistency checks should be done by the apply methods!
00291      *
00292      * @param src matrix to be filtered
00293      * @param dest result
00294      * @param param parameters that determine e.g. border behaviour
00295      */
00296     void applyMask(const matrix<T>& src,
00297                     matrix<T>& dest,
00298                     const modifier::parameters& param);
00299 
00300     /**
00301      * Set the mask to be used with the kernel.
00302      */
00303     void setMask(const channel8& mask);
00304 
00305     /**
00306      * Returns a pointer to the current mask.
00307      */
00308     const channel8* getMask() const;
00309 
00310     /**
00311      * returns a reference to the accumulator object being used
00312      */
00313     A& getAccumulator();
00314 
00315     /**
00316      * returns a reference to the accumulator object being used
00317      */
00318     const A& getAccumulator() const;
00319 
00320   protected:
00321     const kernel2D<T>* kernel;
00322     const sepKernel<T>* sKernel;
00323     const channel8* mask;
00324 
00325     A lresult;
00326   private:
00327     kernel2D<T>* tmpKernel;
00328     sepKernel<T>* tmpSKernel;
00329   };
00330 }
00331 
00332 #include "ltiConvolutionHelper_template.h"
00333 
00334 #endif

Generated on Sat Apr 10 15:25:17 2010 for LTI-Lib by Doxygen 1.6.1