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

ltiCorrelation.h

00001 /*
00002  * Copyright (C) 1999, 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 .......: ltiCorrelation.h
00027  * authors ....: Pablo Alvarado
00028  * organization: LTI, RWTH Aachen
00029  * creation ...: 19.04.99
00030  * revisions ..: $Id: ltiCorrelation.h,v 1.9 2006/02/07 18:42:43 ltilib Exp $
00031  */
00032 
00033 #ifndef _LTI_CORRELATION_H_
00034 #define _LTI_CORRELATION_H_
00035 
00036 #include <vector>
00037 #include "ltiFilter.h"
00038 #include "ltiArray.h"
00039 #include "ltiMatrix.h"
00040 #include "ltiTypes.h"
00041 #include "ltiLinearKernels.h"
00042 
00043 namespace lti {
00044   /**
00045    * Correlation %functor.
00046    *
00047    * This functor convolves a %filter kernel (given in the
00048    * correlation::parameters) with a vector or matrix.
00049    *
00050    * @see lti::gaussKernel, lti::gaborKernel
00051    *
00052    * Example using a gaussian kernel
00053    *
00054    * \code
00055    * // the channel to be filtered:
00056    * lti::channel data,result;
00057    *
00058    * // ... initialize channel here ...
00059    *
00060    * // gauss filter kernel with dimensions 5x5, and a variance of 1.3
00061    * lti::gaussKernel2D<lti::channel::value_type> kernel(5,1.3);
00062    *
00063    * lti::correlation filter;                        // correlation operator
00064    * lti::correlation::parameters param;             // parameters
00065    * param.setKernel(kernel);                        // use the gauss kernel
00066    * filter.setParameters(param);                    // use given parameters
00067    *
00068    * // filter the channel and leave the result there too
00069    * filter.apply(data);
00070    * \endcode
00071    *
00072    * You can also create the functor with a given filter kernel:
00073    *
00074    * \code
00075    * lti::correlation filter(lti::gaussKernel2D<lti::channel::value_type>(5,1.3);
00076    * filter.apply(data); // parameters already set in the constructor!
00077    * \endcode
00078    *
00079    * The filter kernel can also be change, changing the parameters or with
00080    * the shortcut setKernel():
00081    *
00082    * \code
00083    * param.setKernel(anotherKernel);
00084    * filter.setParamters(param);
00085    *
00086    * // this is equivalent to:
00087    *
00088    * filter.setKernel(anotherKernel);
00089    *
00090    * \endcode
00091    */
00092   class correlation : public filter {
00093   public:
00094     /**
00095      * parameters of the lti::correlation functor
00096      */
00097     class parameters : public filter::parameters {
00098     public:
00099       /**
00100        * default constructor
00101        */
00102       parameters();
00103 
00104       /**
00105        * copy constructor
00106        * @param other the parameters object to be copied
00107        */
00108       parameters(const parameters& other);
00109 
00110       /**
00111        * destructor
00112        */
00113       virtual ~parameters();
00114 
00115       /**
00116        * returns name of this type
00117        */
00118       const char* getTypeName() const;
00119 
00120       /**
00121        * copy the contents of other parameter object
00122        * @param other the parameters object to be copied
00123        * @return a reference to this object
00124        */
00125       parameters& copy(const parameters& other);
00126 
00127       /**
00128        * returns a pointer to a clone of this parameters
00129        */
00130       virtual functor::parameters* clone() const;
00131 
00132       /**
00133        * returns the kernel in use.  If it is not set yet, an
00134        * lti::invalidParameters exception will be thrown
00135        * @return a const reference to the filter kernel.
00136        */
00137       const mathObject& getKernel() const;
00138 
00139       /**
00140        * sets the filter kernel to be used.
00141        * A copy of the given %parameter will be made!
00142        * @param aKernel the filter kernel to be used
00143        */
00144       void setKernel(const mathObject& aKernel);
00145 
00146       /**
00147        * Returns the mask in use. If none is set yet, an
00148        * lti::invalidParametersException will be thrown.
00149        */
00150       const channel8& getMask() const;
00151 
00152       /**
00153        * Sets the maks to be used for the kernel.
00154        */
00155       void setMask(const channel8& aMask);
00156 
00157       /**
00158        * Determine whether the whole (rectangular) kernel should be convolved,
00159        * or only an arbitrarily shaped object within in, described by the mask.
00160        * The default value for this option is false, meaning the whole kernel
00161        * is convolved (just as one would expect).
00162        */
00163       void setUseMask(const bool b);
00164 
00165       /**
00166        * Returns the value set by setUseMask.
00167        */
00168       const bool& getUseMask() const;
00169 
00170       /**
00171        * write the parameters in the given ioHandler
00172        * @param handler the ioHandler to be used
00173        * @param complete if true (the default) the enclosing begin/end will
00174        *        be also written, otherwise only the data block will be written.
00175        * @return true if write was successful
00176        */
00177       virtual bool write(ioHandler& handler,const bool complete=true) const;
00178 
00179       /**
00180        * write the parameters in the given ioHandler
00181        * @param handler the ioHandler to be used
00182        * @param complete if true (the default) the enclosing begin/end will
00183        *        be also written, otherwise only the data block will be written.
00184        * @return true if write was successful
00185        */
00186       virtual bool read(ioHandler& handler,const bool complete=true);
00187 
00188 #     ifdef _LTI_MSC_6
00189       /**
00190        * this function is required by MSVC only, as a workaround for a
00191        * very awful bug, which exists since MSVC V.4.0, and still by
00192        * V.6.0 with all bugfixes (so called "service packs") remains
00193        * there...  This method is public due to another bug, so please
00194        * NEVER EVER call this method directly: use read() instead!
00195        */
00196       bool readMS(ioHandler& handler,const bool complete=true);
00197 
00198       /**
00199        * this function is required by MSVC only, as a workaround for a
00200        * very awful bug, which exists since MSVC V.4.0, and still by
00201        * V.6.0 with all bugfixes (so called "service packs") remains
00202        * there...  This method is public due to another bug, so please
00203        * NEVER EVER call this method directly: use write() instead!
00204        */
00205       bool writeMS(ioHandler& handler,const bool complete=true) const;
00206 #     endif
00207 
00208       /**
00209        * Specify the correlation type to be used
00210        */
00211       enum eMode {
00212         Classic, /*!< The correlation is defined as f(x)*k(-x), where "*"
00213                       specifies convolution, f(x) is the image or data where
00214                       the kernel k(x) must be searched
00215                  */
00216         Coefficient, /*!< Calculates the correlation coefficient defined in
00217                        Gonzales&Woods, 1993, "Digital Image Processing",
00218                        pp. 584.
00219 
00220                        \f$\gamma(s,t) =
00221                           \frac{\displaystyle\sum_{x,y}\left[f(x,y)-
00222                           \overline{f}(x,y)\right]
00223                           \left[w(x-s,y-t)-\overline{w}\right]}
00224                           {\left\{\displaystyle\sum_{x,y}\left[f(x,y)-
00225                           \overline{f}(x,y)\right]^2
00226                           \sum_{x,y}\left[w(x-s,y-t)-
00227                           \overline{w}\right]^2\right\}^{1/2}}\f$
00228                      */
00229         C1,  /*!< Calculates matching criteria C1 defined in Sonka et.al 2nd
00230                   Edition, pp. 191:
00231 
00232                   \f$C_1(u,v)=
00233                   \displaystyle\frac{1}
00234                   {\displaystyle\max_{(i,j)\in V}
00235                   \left|{f(i+u,j+v)-h(i,j)}\right|+1}\f$
00236              */
00237         C2,  /*!< Calculates matching criteria C2 defined in Sonka et.al 2nd
00238                   Edition, pp. 191:
00239                   \f$C_2(u,v)=
00240                   \displaystyle\frac{1}
00241                   {\left(\displaystyle\sum_{(i,j)\in V}\left|
00242                   f(i+u,j+v)-h(i,j)\right|+1\right)}\f$
00243 
00244              */
00245         C3  /*!< Calculates matching criteria C3 defined in Sonka et.al 2nd
00246               Edition, pp. 191:
00247 
00248               \f$C_3(u,v)=
00249               \displaystyle\frac{1}
00250               {\left(\displaystyle\sum_{(i,j)\in V}
00251               \left[f(i+u,j+v)-h(i,j)\right]^2+1\right)}\f$
00252             */
00253       };
00254 
00255       /**
00256        * Type of correlation (see parameters::eMode)
00257        */
00258       eMode mode;
00259 
00260       /**
00261        * pointer to the filter kernel copy
00262        */
00263       mathObject* kernel;
00264 
00265       /**
00266        * The mask that defines the object depicted in the kernel
00267        */
00268       const channel8* mask;
00269 
00270       /**
00271        * Convolve the whole kernel, or just the object defined by the mask?
00272        */
00273       bool useMask;
00274 
00275       /**
00276        * get kernel average
00277        */
00278       const double& getKernelAverage() const;
00279 
00280       /**
00281        * get kernel size
00282        */
00283       const int& getKernelSize() const;
00284 
00285     protected:
00286       /**
00287        * average of the kernel elements.
00288        * This pseudo-parameters is calculated by set kernel
00289        */
00290       double kernelAverage;
00291 
00292       /**
00293        * the number of elements of the kernel is also needed
00294        */
00295       int kernelSize;
00296 
00297     };
00298 
00299     /**
00300      * default constructor
00301      */
00302     correlation();
00303 
00304     /**
00305      * construct a correlation functor with a parameters set
00306      * which includes the given filter kernel.
00307      */
00308     correlation(const mathObject& aKernel);
00309 
00310     /**
00311      * copy constructor
00312      * @param other the other functor to be copied
00313      */
00314     correlation(const correlation& other);
00315 
00316     /**
00317      * destructor
00318      */
00319     virtual ~correlation();
00320 
00321     /**
00322      * returns the name of this type
00323      */
00324     virtual const char* getTypeName() const;
00325 
00326     /**
00327      * operates on the given %parameter.
00328      * @param srcdest channel8 with the source data.  The result
00329      *                will be left here too.
00330      * @return true if successful, false otherwise.
00331      */
00332     bool apply(channel8& srcdest) const;
00333 
00334     /**
00335      * operates on the given %parameter.
00336      * @param srcdest channel with the source data.  The result
00337      *                will be left here too.
00338      * @return true if successful, false otherwise.
00339      */
00340     bool apply(matrix<float>& srcdest) const;
00341 
00342     /**
00343      * operates on the given %parameter.
00344      * @param srcdest dmatrix with the source data.  The result
00345      *                will be left here too.
00346      * @return true if successful, false otherwise.
00347      */
00348     bool apply(dmatrix& srcdest) const;
00349 
00350 
00351     /**
00352      * operates on the given %parameter.
00353      * @param srcdest vector<channel8::value_type> with the source data.
00354      *                The result will be left here too.
00355      * @return true if successful, false otherwise.
00356      */
00357     bool apply(vector<channel8::value_type>& srcdest) const;
00358 
00359     /**
00360      * operates on the given %parameter.
00361      * @param srcdest vector<channel::value_type> with the source data.
00362      *                The result will be left here too.
00363      * @return true if successful, false otherwise.
00364      */
00365     bool apply(vector<channel::value_type>& srcdest) const;
00366 
00367     /**
00368      * operates on the given %parameter.
00369      * @param srcdest dvector with the source data.
00370      *                The result will be left here too.
00371      * @return true if successful, false otherwise.
00372      */
00373     bool apply(dvector& srcdest) const;
00374 
00375 
00376     /**
00377      * operates on a copy of the given parameters.
00378      * @param src channel8 with the source data.
00379      * @param dest channel8 where the result will be left.
00380      * @return true if successful, false otherwise.
00381      */
00382     bool apply(const channel8& src,channel8& dest) const;
00383 
00384     /**
00385      * operates on a copy of the given parameters.
00386      * @param src channel with the source data.
00387      * @param dest channel where the result will be left.
00388      * @return true if successful, false otherwise.
00389      */
00390     bool apply(const matrix<float>& src,matrix<float>& dest) const;
00391 
00392     /**
00393      * operates on a copy of the given parameters.
00394      * @param src dmatrix with the source data.
00395      * @param dest dmatrix where the result will be left.
00396      * @return true if successful, false otherwise.
00397      */
00398     bool apply(const dmatrix& src,dmatrix& dest) const;
00399 
00400     /**
00401      * operates on a copy of the given parameters.
00402      * @param src vector<channel8::value_type> with the source data.
00403      * @param dest vector<channel8::value_type> where the result will be left.
00404      * @return true if successful, false otherwise.
00405      */
00406     bool apply(const vector<channel8::value_type>& src,
00407                      vector<channel8::value_type>& dest) const;
00408 
00409     /**
00410      * operates on a copy of the given parameters.
00411      * @param src vector<channel::value_type> with the source data.
00412      * @param dest vector<channel::value_type> where the result will be left.
00413      * @return true if successful, false otherwise.
00414      */
00415     bool apply(const vector<channel::value_type>& src,
00416                      vector<channel::value_type>& dest) const;
00417 
00418     /**
00419      * operates on a copy of the given parameters.
00420      * @param src dvector with the source data.
00421      * @param dest dvector where the result will be left.
00422      * @return true if successful, false otherwise.
00423      */
00424     bool apply(const dvector& src,
00425                      dvector& dest) const;
00426 
00427     /**
00428      * copy data of "other" functor.
00429      */
00430     correlation& copy(const correlation& other);
00431 
00432     /**
00433      * returns a pointer to a clone of the functor.
00434      */
00435     virtual functor* clone() const;
00436 
00437     /**
00438      * returns used parameters
00439      */
00440     const parameters& getParameters() const;
00441 
00442     /**
00443      * shortcut to set the filter kernel in the functor parameters.
00444      * The other parameters remain unchanged.
00445      */
00446     void setKernel(const mathObject& aKernel);
00447 
00448   private:
00449     /**
00450      * This is the accumulator class needed by the convolution helper to
00451      * act as a linear convolution operator for gray valued images.
00452      *
00453      * The type T is the type of the elements of the object to be filtered
00454      * The (optional) type U is the type of the accumulator variable for
00455      * the filter.
00456      */
00457     template<class T,class U=T>
00458     class classic {
00459     public:
00460       /**
00461        * Default constructor
00462        */
00463       classic();
00464 
00465       /**
00466        * Accumulate the values of filter and src
00467      * (srcL and srcR for symetric or asymetric kernel)
00468        */
00469       inline void accumulate(const T& filter,const T& src);
00470       inline void accumulateASym(const T& filter,const T& srcL,const T& srcR);
00471       inline void accumulateSym(const T& filter,const T& srcL,const T& srcR);
00472 
00473       /**
00474        * Accumulate the values of T(0) and src
00475        */
00476       inline void accumulateZero(const T& src);
00477 
00478       /**
00479        * Get the state of the accumulator
00480        */
00481       inline T getResult() const;
00482 
00483       /**
00484        * Reset the state of the accumulator
00485        */
00486       inline void reset();
00487 
00488       /**
00489        * set norm
00490        */
00491       inline void setNorm(const T& norm);
00492 
00493     protected:
00494       /**
00495        * the accumulated value
00496        */
00497       U state;
00498 
00499       /**
00500        * norm
00501        */
00502       T norm;
00503     };
00504 
00505 
00506     /**
00507      * This is the accumulator class needed by the correlation helper to
00508      * act as a correlation coefficient operator for gray valued images.
00509      *
00510      * This implements the Eq. (9.3-8) on page 584 on Gonzalez & Woods,
00511      * "Digital Image Processing"
00512      *
00513      * The type T is the type of the elements of the object to be filtered
00514      * The (optional) type U is the type of the accumulator variable for
00515      * the filter.
00516      */
00517     template<class T,class U=T>
00518       class coefficient {
00519         public:
00520         /**
00521          * Default constructor
00522          */
00523         coefficient();
00524 
00525       /**
00526        * Accumulate the values of filter and src
00527      * (srcL and srcR for symetric or asymetric kernel)
00528        */
00529       inline void accumulate(const T& filter,const T& src);
00530       inline void accumulateASym(const T& filter,const T& srcL,const T& srcR);
00531       inline void accumulateSym(const T& filter,const T& srcL,const T& srcR);
00532 
00533       /**
00534        * Accumulate the values of T(0) and src
00535        */
00536       inline void accumulateZero(const T& src);
00537 
00538         /**
00539          * Get the state of the accumulator
00540          */
00541         inline T getResult() const;
00542 
00543         /**
00544          * Reset the state of the accumulator
00545          */
00546         inline void reset();
00547 
00548         /**
00549          * set norm
00550          */
00551         inline void setNorm(const T& norm);
00552 
00553         /**
00554          * set averages of data and kernel
00555          */
00556         void setData(const T& kernelAverage,const T& kernelSize);
00557 
00558         protected:
00559         /**
00560          * the accumulated w-E(w)
00561          */
00562         U sumW;
00563 
00564         /**
00565          * the accumulated (w-E(w))(w-E(w))
00566          */
00567         U sumWW;
00568 
00569         /**
00570          * the accumulated f*(w-E(w))
00571          */
00572         U sumFW;
00573 
00574         /**
00575          * the accumulated f*f
00576          */
00577         U sumFF;
00578 
00579         /**
00580          * the accumulated f
00581          */
00582         U sumF;
00583 
00584         /**
00585          * norm
00586          */
00587         T norm;
00588 
00589         /**
00590          * Average of the data
00591          */
00592         T kernSize;
00593 
00594         /**
00595          * Average of the kernel
00596          */
00597         T kernAvg;
00598       };
00599 
00600     /**
00601      * This is the accumulator class needed by the convolution helper to
00602      * calculate the matching criterium C1 described in Sonka et.al.,pp.191
00603      *
00604      * The type T is the type of the elements of the object to be filtered
00605      * The (optional) type U is the type of the accumulator variable for
00606      * the filter.
00607      */
00608     template<class T,class U=T>
00609     class criterium1 {
00610     public:
00611       /**
00612        * Default constructor
00613        */
00614       criterium1();
00615 
00616       /**
00617        * Accumulate the values of filter and src
00618      * (srcL and srcR for symetric or asymetric kernel)
00619        */
00620       inline void accumulate(const T& filter,const T& src);
00621       inline void accumulateASym(const T& filter,const T& srcL,const T& srcR);
00622       inline void accumulateSym(const T& filter,const T& srcL,const T& srcR);
00623 
00624       /**
00625        * Accumulate the values of T(0) and src
00626        */
00627       inline void accumulateZero(const T& src);
00628 
00629       /**
00630        * Get the state of the accumulator
00631        */
00632       inline T getResult() const;
00633 
00634       /**
00635        * Reset the state of the accumulator
00636        */
00637       inline void reset();
00638 
00639       /**
00640        * set norm
00641        */
00642       inline void setNorm(const T& norm);
00643 
00644     protected:
00645       /**
00646        * the accumulated value
00647        */
00648       U state;
00649 
00650       /**
00651        * norm
00652        */
00653       T norm;
00654     };
00655 
00656     /**
00657      * This is the accumulator class needed by the convolution helper to
00658      * calculate the matching criterium C2 described in Sonka et.al.,pp.191
00659      *
00660      * The type T is the type of the elements of the object to be filtered
00661      * The (optional) type U is the type of the accumulator variable for
00662      * the filter.
00663      */
00664     template<class T,class U=T>
00665     class criterium2 : public criterium1<T,U> {
00666       public:
00667       /**
00668        * Accumulate the values of filter and src
00669      * (srcL and srcR for symetric or asymetric kernel)
00670        */
00671       inline void accumulate(const T& filter,const T& src);
00672       inline void accumulateASym(const T& filter,const T& srcL,const T& srcR);
00673       inline void accumulateSym(const T& filter,const T& srcL,const T& srcR);
00674 
00675       /**
00676        * Accumulate the values of T(0) and src
00677        */
00678       inline void accumulateZero(const T& src);
00679   };
00680     /**
00681      * This is the accumulator class needed by the convolution helper to
00682      * calculate the matching criterium C3 described in Sonka et.al.,pp.191
00683      *
00684      * The type T is the type of the elements of the object to be filtered
00685      * The (optional) type U is the type of the accumulator variable for
00686      * the filter.
00687      */
00688     template<class T,class U=T>
00689     class criterium3 : public criterium1<T,U> {
00690     public:
00691       /**
00692        * Accumulate the values of filter and src
00693      * (srcL and srcR for symetric or asymetric kernel)
00694        */
00695       inline void accumulate(const T& filter,const T& src);
00696       inline void accumulateASym(const T& filter,const T& srcL,const T& srcR);
00697       inline void accumulateSym(const T& filter,const T& srcL,const T& srcR);
00698 
00699       /**
00700        * Accumulate the values of T(0) and src
00701        */
00702       inline void accumulateZero(const T& src);
00703     };
00704 
00705   };
00706   }
00707 #include "ltiCorrelation_template.h"
00708 
00709 #endif

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