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

ltiErosion.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 .......: ltiErosion.h
00027  * authors ....: Pablo Alvarado
00028  * organization: LTI, RWTH Aachen
00029  * creation ...: 19.7.2000
00030  * revisions ..: $Id: ltiErosion.h,v 1.11 2006/02/07 18:49:54 ltilib Exp $
00031 */
00032 
00033 #ifndef _LTI_EROSION_H_
00034 #define _LTI_EROSION_H_
00035 
00036 #include "ltiObject.h"
00037 #include "ltiMath.h"
00038 #include "ltiImage.h"
00039 #include "ltiLinearKernels.h"
00040 #include "ltiMorphology.h"
00041 #include "ltiConvolutionHelper.h"
00042 
00043 namespace lti {
00044   /**
00045    * Erosion functor.
00046    *
00047    * This functor implements the morphological operator "erosion".
00048    * Through the parameters a "binary" or "gray scale" modus can be choosed,
00049    * and the structuring element (represented by a linear filter kernel) can
00050    * be given.
00051    *
00052    * For mode Binary the destination image is set to 0 if there is a
00053    * source element in the kernel region that is zero and to the norm
00054    * value of the used kernel otherwise.
00055    *
00056    * The definition for mode Gray can be found in e.g. Gonzalez,
00057    * R. and Woods, R.  Digital Image Processing, 2nd Edition,
00058    * pp. 550--553, Prentice Hall, 2002
00059    *
00060    * /code
00061    * dest(s,t) = min(src(s-x, t-y) - kernel(x,y)) 
00062    * /endcode
00063    *
00064    * where the regions of the kernel and source overlap. Qualitatively
00065    * the Gray operation results in darkening esp. of bright
00066    * details. For channel8 the resulting values are clipped to be in
00067    * the allowed range of [0,255]. Note that for channels the kernel
00068    * values should be much lower than the default 1.f. Also note that when the
00069    * kernel is separable (sepKernel) the values of all column and row
00070    * kernels are added. An example is chessBoardKernel.
00071    *
00072    * Example:
00073    *
00074    * \code
00075    * lti::erosion eroder;                   // the erosion functor
00076    * lti::erosion::parameters erosionParam; // the parameters
00077    *
00078    * lti::cityBlockKernel<float> kern(3);   // 3x3 diamond shaped kernel
00079    *
00080    * // binary erosion
00081    * erosionParam.mode = lti::erosion::parameters::Binary;
00082    * erosionParam.setKernel(parameters);
00083    *
00084    * // set the parameters
00085    * eroder.setParameters(erosionParam);
00086    *
00087    * // apply the erosion to a channel "chnlSrc" and leave the result in
00088    * // "chnlDest"
00089    *
00090    * eroder.apply(chnlSrc,chnlDest);
00091    * \endcode
00092    *
00093    * @ingroup gMorphology
00094    */
00095   class erosion : public morphology {
00096   public:
00097     /**
00098      * the parameters for the class erosion
00099      */
00100     class parameters : public morphology::parameters {
00101     public:
00102       /**
00103        * type to specify what kind of erosion should be applied. See
00104        * lti::erosion for details.
00105        */
00106       enum eMode {
00107         Binary, /*!< Binary erosion */
00108         Gray    /*!< Gray valued erosion */
00109       };
00110 
00111       /**
00112        * default constructor
00113        */
00114       parameters();
00115 
00116       /**
00117        * copy constructor
00118        * @param other the parameters object to be copied
00119        */
00120       parameters(const parameters& other);
00121 
00122       /**
00123        * destructor
00124        */
00125       ~parameters();
00126 
00127       /**
00128        * returns name of this type
00129        */
00130       const char* getTypeName() const;
00131 
00132       /**
00133        * copy the contents of a parameters object
00134        * @param other the parameters object to be copied
00135        * @return a reference to this parameters object
00136        */
00137       parameters& copy(const parameters& other);
00138 
00139       /**
00140        * returns a pointer to a clone of the parameters
00141        */
00142       virtual functor::parameters* clone() const;
00143 
00144       /**
00145        * sets the filter kernel to be used.
00146        * A copy of the given parameter will be made!
00147        * @param aKernel the filter kernel to be used
00148        */
00149       void setKernel(const mathObject& aKernel);
00150 
00151       /**
00152        * returns the kernel in use.  If it is not set yet, an
00153        * lti::invalidParameters exception will be thrown
00154        * @return a const reference to the filter kernel.
00155        */
00156       const mathObject& getKernel() const;
00157 
00158       /**
00159        * write the parameters in the given ioHandler
00160        * @param handler the ioHandler to be used
00161        * @param complete if true (the default) the enclosing begin/end will
00162        *        be also written, otherwise only the data block will be written.
00163        * @return true if write was successful
00164        */
00165       virtual bool write(ioHandler& handler,const bool complete=true) const;
00166 
00167       /**
00168        * write the parameters in the given ioHandler
00169        * @param handler the ioHandler to be used
00170        * @param complete if true (the default) the enclosing begin/end will
00171        *        be also written, otherwise only the data block will be written.
00172        * @return true if write was successful
00173        */
00174       virtual bool read(ioHandler& handler,const bool complete=true);
00175 
00176 #     ifdef _LTI_MSC_6
00177       /**
00178        * this function is required by MSVC only, as a workaround for a
00179        * very awful bug, which exists since MSVC V.4.0, and still by
00180        * V.6.0 with all bugfixes (so called "service packs") remains
00181        * there...  This method is public due to another bug, so please
00182        * NEVER EVER call this method directly: use read() instead!
00183        */
00184       bool readMS(ioHandler& handler,const bool complete=true);
00185 
00186       /**
00187        * this function is required by MSVC only, as a workaround for a
00188        * very awful bug, which exists since MSVC V.4.0, and still by
00189        * V.6.0 with all bugfixes (so called "service packs") remains
00190        * there...  This method is public due to another bug, so please
00191        * NEVER EVER call this method directly: use write() instead!
00192        */
00193       bool writeMS(ioHandler& handler,const bool complete=true) const;
00194 #     endif
00195 
00196       // ---------------------------------------------------
00197       // the parameters
00198       // ---------------------------------------------------
00199 
00200       /**
00201        * Erosion mode.
00202        *
00203        * The erosion is defined for "Binary" images (only two values per
00204        * pixel) and for gray scaled images (channels or channel8).
00205        * In "Binary" modus, the channels and channel8s will be interpreted as
00206        * binary with 0 as "0" and everything else as "1"
00207        * The default value is "Binary"
00208        */
00209       eMode mode;
00210 
00211     protected:
00212       /**
00213        * The kernel to be used.  This parameter can only be set through the
00214        * setKernel member.
00215        */
00216       mathObject* kernel;
00217     };
00218 
00219     /**
00220      * default constructor
00221      */
00222     erosion();
00223 
00224     /**
00225      * construct  with the given kernel
00226      */
00227     erosion(const mathObject& aKernel);
00228 
00229     /**
00230      * copy constructor
00231      * @param other the object to be copied
00232      */
00233     erosion(const erosion& other);
00234 
00235     /**
00236      * destructor
00237      */
00238     virtual ~erosion();
00239 
00240     /**
00241      * returns the name of this type ("erosion")
00242      */
00243     virtual const char* getTypeName() const;
00244 
00245     /**
00246      * operates on the given parameter.
00247      * @param srcdest channel with the source data.  The result
00248      *                 will be left here too.
00249      * @result a reference to the <code>srcdest</code>.
00250      */
00251     bool apply(channel& srcdest) const;
00252 
00253     /**
00254      * operates on the given parameter.
00255      * @param srcdest channel8 with the source data.  The result
00256      *                 will be left here too.
00257      * @return true if successful, false otherwise.
00258      */
00259     bool apply(channel8& srcdest) const;
00260 
00261     /**
00262      * operates on the given parameter.
00263      * @param srcdest fvector with the source data.  The result
00264      *                 will be left here too.
00265      * @return true if successful, false otherwise.
00266      */
00267     bool apply(fvector& srcdest) const;
00268 
00269     /**
00270      * operates on the given parameter.
00271      * @param srcdest vector<channel8::value_type> with the source data.
00272      *                The result will be left here too.
00273      * @return true if successful, false otherwise.
00274      */
00275     bool apply(vector<channel8::value_type>& srcdest) const;
00276 
00277     /**
00278      * operates on a copy of the given parameters.
00279      * @param src channel with the source data.
00280      * @param dest channel where the result will be left.
00281      * @return true if successful, false otherwise.
00282      */
00283     bool apply(const channel& src,channel& dest) const;
00284 
00285     /**
00286      * operates on a copy of the given parameters.
00287      * @param src channel8 with the source data.
00288      * @param dest channel8 where the result will be left.
00289      * @return true if successful, false otherwise.
00290      */
00291     bool apply(const channel8& src,channel8& dest) const;
00292 
00293     /**
00294      * operates on a copy of the given parameters.
00295      * @param src fvector with the source data.
00296      * @param dest fvector where the result will be left.
00297      * @return true if successful, false otherwise.
00298      */
00299     bool apply(const fvector& src,fvector& dest) const;
00300 
00301     /**
00302      * operates on a copy of the given parameters.
00303      * @param src vector<channel8::value_type> with the source data.
00304      * @param dest vector<channel8::value_type> where the result will be left.
00305      * @return true if successful, false otherwise.
00306      */
00307     bool apply(const vector<channel8::value_type>& src,
00308                      vector<channel8::value_type>& dest) const;
00309 
00310     /**
00311      * copy data of "other" functor.
00312      * @param other the functor to be copied
00313      * @return a reference to this functor object
00314      */
00315     erosion& copy(const erosion& other);
00316 
00317     /**
00318      * returns a pointer to a clone of this functor.
00319      */
00320     virtual functor* clone() const;
00321 
00322     /**
00323      * returns used parameters
00324      */
00325     const parameters& getParameters() const;
00326     /**
00327      * shortcut to set the filter kernel in the functor parameters.
00328      * The other parameters remain unchanged.
00329      */
00330     void setKernel(const mathObject& aKernel);
00331 
00332   private:
00333     /**
00334      * This is the accumulator class needed by the convolution helper to
00335      * act as a erosion operator for gray valued images.
00336      *
00337      * The type T is the type of the elements of the object to be filtered
00338      * The (optional) type U is the type of the accumulator variable for
00339      * the filter.
00340      */
00341     template<class T,class U=T>
00342     class accumulatorGray {
00343     public:
00344       /**
00345        * Default constructor
00346        */
00347       accumulatorGray();
00348 
00349       /**
00350        * Accumulate the values of filter and src
00351        */
00352       inline void accumulate(const T& filter,const T& src);
00353 
00354       /**
00355        * Accumulate the values of T(0) and src
00356        */
00357       inline void accumulateZero(const T& src);
00358 
00359       /**
00360        * Accumulate the values of filter and srcL and srcR
00361      * for symmetric filter kernel
00362      * src:       srcL  *  middle  *  srcR
00363      * filter:      *  *  *  middle  *  *  *
00364      * used filter part:  *  *  *  middle
00365        */
00366       inline void accumulateSym(const T& filter,const T& srcL,const T& srcR);
00367 
00368       /**
00369        * Accumulate the values of filter and src
00370      * for asymmetric filter kernel
00371      * src:       srcL  *  middle  *  srcR
00372      * filter:      *  *  *  middle  *  *  *
00373      * used filter part:  *  *  *  middle
00374        */
00375       inline void accumulateASym(const T& filter,const T& srcL,const T& srcR);
00376 
00377       /**
00378        * Get the state of the accumulator
00379        */
00380       inline T getResult() const;
00381 
00382       /**
00383        * Reset the state of the accumulator
00384        */
00385       inline void reset();
00386 
00387       /**
00388        * set norm
00389        */
00390       inline void setNorm(const T& norm);
00391     protected:
00392       /**
00393        * the accumulated value
00394        */
00395       U state;
00396 
00397       /**
00398        * the norm
00399        */
00400       T norm;
00401     };
00402 
00403     /**
00404      * This is the accumulator class needed by the convolution helper to
00405      * act as a erosion operator for binary valued images.
00406      *
00407      * The type T is the type of the elements of the object to be filtered
00408      * The (optional) type U is the type of the accumulator variable for
00409      * the filter.
00410      */
00411     template<class T,class U=T>
00412     class accumulatorBin {
00413     public:
00414       /**
00415        * Default constructor
00416        */
00417       accumulatorBin();
00418 
00419       /**
00420        * Accumulate the values of filter and src
00421        */
00422       inline void accumulate(const T& filter,const T& src);
00423 
00424       /**
00425        * Accumulate the values of T(0) and src
00426        */
00427       inline void accumulateZero(const T& src);
00428 
00429       /**
00430        * Accumulate the values of filter and srcL and srcR
00431      * for symmetric filter kernel
00432      * src:       srcL  *  middle  *  srcR
00433      * filter:      *  *  *  middle  *  *  *
00434      * used filter part:  *  *  *  middle
00435        */
00436       inline void accumulateSym(const T& filter,const T& srcL,const T& srcR);
00437 
00438       /**
00439        * Accumulate the values of filter and src
00440      * for asymmetric filter kernel
00441      * src:       srcL  *  middle  *  srcR
00442      * filter:      *  *  *  middle  *  *  *
00443      * used filter part:  *  *  *  middle
00444        */
00445       inline void accumulateASym(const T& filter,const T& srcL,const T& srcR);
00446 
00447 
00448       /**
00449        * Get the state of the accumulator
00450        */
00451       inline T getResult() const;
00452 
00453       /**
00454        * Reset the state of the accumulator
00455        */
00456       inline void reset();
00457 
00458       /**
00459        * set norm
00460        */
00461       inline void setNorm(const T& nrm);
00462     protected:
00463       /**
00464        * the accumulated value
00465        */
00466       U state;
00467 
00468       /**
00469        * the norm
00470        */
00471       T norm;
00472     };
00473 
00474   };
00475 }
00476 
00477 #include "ltiErosion_template.h"
00478 
00479 #endif

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