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

ltiKNearestNeighFilter.h

00001 /*
00002  * Copyright (C) 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  * project ....: LTI Digital Image/Signal Processing Library
00025  * file .......: ltiKNearestNeighFilter.h
00026  * authors ....: Axel Berner
00027  * organization: LTI, RWTH Aachen
00028  * creation ...: 3.2.2002
00029  * revisions ..: $Id: ltiKNearestNeighFilter.h,v 1.9 2006/02/08 11:20:24 ltilib Exp $
00030  */
00031 
00032 #ifndef _LTI_KNEARESTNEIGH_FILTER_H_
00033 #define _LTI_KNEARESTNEIGH_FILTER_H_
00034 
00035 #include "ltiFilter.h"
00036 
00037 namespace lti {
00038   /**
00039    * A smoothness filter for label images (channel8,matrix<int>)
00040    * which uses the idea of the k-Nearest Neighbour concept.  This means,
00041    * this filter will assign to the origin position of the kernel mask, the
00042    * label with the most number of instances within the kernel region.
00043    * The filter removes disturbance pixels and smooths edges.
00044    *
00045    * In case that more than one label has the "maximal" number of
00046    * instances within the kernel mask, then the assigned label will
00047    * -# remain unchanged, if the kernel origin contains already one of the
00048    *    most frequent labels.
00049    * -# be the median of all "winner" labels will.
00050    *
00051    * The algorithms uses an optimized histogram actualization method, similar
00052    * to the one used for the median filter of channel8 (see also Sonka et.al,
00053    * Image Processing, Analysis, and Machine Vision, 2nd edition, 1998,
00054    * pp. 74-76
00055    */
00056   class kNearestNeighFilter : public filter {
00057   public:
00058     /**
00059      * the parameters for the class kNearestNeighFilter
00060      */
00061     class parameters : public filter::parameters {
00062     public:
00063       /**
00064        * default constructor
00065        */
00066       parameters();
00067 
00068       /**
00069        * copy constructor
00070        * @param other the parameters object to be copied
00071        */
00072       parameters(const parameters& other);
00073 
00074       /**
00075        * destructor
00076        */
00077       ~parameters();
00078 
00079       /**
00080        * returns name of this type
00081        */
00082       const char* getTypeName() const;
00083 
00084       /**
00085        * copy the contents of a parameters object
00086        * @param other the parameters object to be copied
00087        * @return a reference to this parameters object
00088        */
00089       parameters& copy(const parameters& other);
00090 
00091       /**
00092        * copy the contents of a parameters object
00093        * @param other the parameters object to be copied
00094        * @return a reference to this parameters object
00095        */
00096       parameters& operator=(const parameters& other);
00097 
00098 
00099       /**
00100        * returns a pointer to a clone of the parameters
00101        */
00102       virtual functor::parameters* clone() const;
00103 
00104       /**
00105        * write the parameters in the given ioHandler
00106        * @param handler the ioHandler to be used
00107        * @param complete if true (the default) the enclosing begin/end will
00108        *        be also written, otherwise only the data block will be written.
00109        * @return true if write was successful
00110        */
00111       virtual bool write(ioHandler& handler,const bool complete=true) const;
00112 
00113       /**
00114        * read the parameters from the given ioHandler
00115        * @param handler the ioHandler to be used
00116        * @param complete if true (the default) the enclosing begin/end will
00117        *        be also written, otherwise only the data block will be written.
00118        * @return true if write was successful
00119        */
00120       virtual bool read(ioHandler& handler,const bool complete=true);
00121 
00122 #     ifdef _LTI_MSC_6
00123       /**
00124        * this function is required by MSVC only, as a workaround for a
00125        * very awful bug, which exists since MSVC V.4.0, and still by
00126        * V.6.0 with all bugfixes (so called "service packs") remains
00127        * there...  This method is also public due to another bug, so please
00128        * NEVER EVER call this method directly: use read() instead
00129        */
00130       bool readMS(ioHandler& handler,const bool complete=true);
00131 
00132       /**
00133        * this function is required by MSVC only, as a workaround for a
00134        * very awful bug, which exists since MSVC V.4.0, and still by
00135        * V.6.0 with all bugfixes (so called "service packs") remains
00136        * there...  This method is also public due to another bug, so please
00137        * NEVER EVER call this method directly: use write() instead
00138        */
00139       bool writeMS(ioHandler& handler,const bool complete=true) const;
00140 #     endif
00141 
00142       // ------------------------------------------------
00143       // the parameters
00144       // ------------------------------------------------
00145 
00146       /**
00147        * kernelSize defines the size of the square filter kernel
00148        *
00149        * Default value: 5
00150        */
00151       int kernelSize;
00152     };
00153 
00154     /**
00155      * default constructor
00156      */
00157     kNearestNeighFilter();
00158 
00159     /**
00160      * copy constructor
00161      * @param other the object to be copied
00162      */
00163     kNearestNeighFilter(const kNearestNeighFilter& other);
00164 
00165     /**
00166      * destructor
00167      */
00168     virtual ~kNearestNeighFilter();
00169 
00170     /**
00171      * returns the name of this type ("kNearestNeighFilter")
00172      */
00173     virtual const char* getTypeName() const;
00174 
00175     /**
00176      * operates on the given %parameter.
00177      * @param srcdest channel8 with the source data.  The result
00178      *                 will be left here too.
00179      * @return true if apply successful or false otherwise.
00180      */
00181     bool apply(channel8& srcdest);
00182 
00183     /**
00184      * operates on a copy of the given %parameters.
00185      * @param src channel8 with the source data.
00186      * @param dest channel8 where the result will be left.
00187      * @return true if apply successful or false otherwise.
00188      */
00189     bool apply(const channel8& src,channel8& dest) ;
00190 
00191     /**
00192      * operates on the given %parameter.
00193      * @param srcdest channel8 with the source data.  The result
00194      *                 will be left here too.
00195      * @return true if apply successful or false otherwise.
00196      */
00197     bool apply(imatrix& srcdest);
00198 
00199     /**
00200      * operates on a copy of the given %parameters.
00201      * @param src channel8 with the source data.
00202      * @param dest channel8 where the result will be left.
00203      * @return true if apply successful or false otherwise.
00204      */
00205     bool apply(const imatrix& src,imatrix& dest) ;
00206 
00207     /**
00208      * copy data of "other" functor.
00209      * @param other the functor to be copied
00210      * @return a reference to this functor object
00211      */
00212     kNearestNeighFilter& copy(const kNearestNeighFilter& other);
00213 
00214     /**
00215      * alias for copy member
00216      * @param other the functor to be copied
00217      * @return a reference to this functor object
00218      */
00219     kNearestNeighFilter& operator=(const kNearestNeighFilter& other);
00220 
00221     /**
00222      * returns a pointer to a clone of this functor.
00223      */
00224     virtual functor* clone() const;
00225 
00226     /**
00227      * returns used parameters
00228      */
00229     const parameters& getParameters() const;
00230 
00231   protected:
00232     /**
00233      * the corrected kernel size (always odd number)
00234      */
00235     int sizeOfKernel;
00236 
00237     /**
00238      * size of the histogram (= max. labelnumber from src-image)
00239      */
00240     int histoSize;
00241 
00242     /**
00243      * return the most avaible label in the given histogram
00244      */
00245     int getMostLabel(const ivector& histogram,
00246          const imatrix& src,
00247          const int& row, const int& col) const;
00248 
00249     /**
00250      * returns the median of all (max)histogram entries
00251      */
00252     int getMedian(const ivector& histogram,
00253       const int max,
00254                   const int numOfMax) const;
00255 
00256     /**
00257      * runs inside the image src
00258      */
00259     void histogramMethodMiddle(const imatrix& src,imatrix& dest,
00260                                ivector& histogram,
00261                                const int& row, int& col) const;
00262 
00263     /**
00264      * applies the histogramMethod for the type boundary Zero
00265      */
00266     bool histogramMethodZero(const imatrix& src,
00267                              imatrix& dest) const;
00268 
00269     /**
00270      * applies the histogramMethod for the type boundary Constant
00271      */
00272     bool histogramMethodConstant(const imatrix& src,
00273                                  imatrix& dest) const;
00274 
00275     /**
00276      * applies the histogramMethod for the type boundary Periodic
00277      */
00278     bool histogramMethodPeriodic(const imatrix& src,
00279                                  imatrix& dest) const;
00280 
00281     /**
00282      * applies the histogramMethod for the type boundary Mirror
00283      */
00284     bool histogramMethodMirror(const imatrix& src,
00285                                imatrix& dest) const;
00286 
00287     /**
00288      * applies the histogramMethod for the type boundary NoBoundary
00289      */
00290     bool histogramMethodNoBoundary(const imatrix& src,
00291                                    imatrix& dest) const;
00292 
00293   };
00294 
00295 }
00296 
00297 #endif

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