latest version v1.9 - last update 10 Apr 2010 |
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 /*-------------------------------------------------------------------- 00025 * project ....: LTI-Lib: Image Processing and Computer Vision Library 00026 * file .......: ltiLocalMaxima.h 00027 * authors ....: Pablo Alvarado 00028 * organization: LTI, RWTH Aachen 00029 * creation ...: 1.11.2002 00030 * revisions ..: $Id: ltiLocalMaxima.h,v 1.13 2006/02/08 11:24:34 ltilib Exp $ 00031 */ 00032 00033 #ifndef _LTI_LOCAL_MAXIMA_H_ 00034 #define _LTI_LOCAL_MAXIMA_H_ 00035 00036 #include "ltiMaximumFilter.h" 00037 #include "ltiMatrix.h" 00038 #include "ltiPointList.h" 00039 #include <list> 00040 00041 namespace lti { 00042 /** 00043 * This simple functor tries to find the local maxima at a given window 00044 * and suppresses all other values, setting them the "nonMaxValue" given 00045 * in the parameters (usually 0). 00046 * 00047 * The template parameter T defines the type of the matrix or vector 00048 * to be filtered. 00049 * 00050 * An instance of this class cannot be used from different threads 00051 * (not thread-save). If you have multiple threads, use simply one 00052 * functor instance for each thread. 00053 * 00054 * This class is used for example in the extraction of the maxima in 00055 * the Hough Transform. 00056 * 00057 * A very similar functor with more use in edge-detection applications is 00058 * the lti::nonMaximaSuppression, which is used for example in the Canny 00059 * edge detection. 00060 * 00061 * @see parameters 00062 * @see lti::nonMaximaSuppression 00063 */ 00064 template<class T> 00065 class localMaxima : public maximumFilter<T> { 00066 public: 00067 /** 00068 * the parameters for the class localMaxima 00069 */ 00070 class parameters : public maximumFilter<T>::parameters { 00071 public: 00072 /** 00073 * default constructor 00074 */ 00075 parameters() : maximumFilter<T>::parameters() { 00076 relativeMinimum = float(0.0f); 00077 noMaxValue = ubyte(0); 00078 hystheresisThreshold = -1.0f; 00079 maxNumber = -1; 00080 }; 00081 00082 /** 00083 * copy constructor 00084 * @param other the parameters object to be copied 00085 */ 00086 parameters(const parameters& other) : maximumFilter<T>::parameters() { 00087 copy(other); 00088 }; 00089 00090 /** 00091 * destructor 00092 */ 00093 ~parameters() { 00094 }; 00095 00096 /** 00097 * returns name of this type 00098 */ 00099 const char* getTypeName() const { 00100 return "localMaxima::parameters"; 00101 }; 00102 00103 /** 00104 * copy the contents of a parameters object 00105 * @param other the parameters object to be copied 00106 * @return a reference to this parameters object 00107 */ 00108 parameters& copy(const parameters& other) { 00109 # ifndef _LTI_MSC_6 00110 // MS Visual C++ 6 is not able to compile this... 00111 maximumFilter<T>::parameters::copy(other); 00112 # else 00113 // ...so we have to use this workaround. 00114 // Conditional on that, copy may not be virtual. 00115 maximumFilter<T>::parameters& (maximumFilter<T>::parameters::* p_copy) 00116 (const maximumFilter<T>::parameters&) = 00117 maximumFilter<T>::parameters::copy; 00118 (this->*p_copy)(other); 00119 # endif 00120 00121 relativeMinimum = other.relativeMinimum; 00122 noMaxValue = other.noMaxValue; 00123 hystheresisThreshold = other.hystheresisThreshold; 00124 maxNumber = other.maxNumber; 00125 return *this; 00126 }; 00127 00128 /** 00129 * copy the contents of a parameters object 00130 * @param other the parameters object to be copied 00131 * @return a reference to this parameters object 00132 */ 00133 parameters& operator=(const parameters& other) { 00134 return copy(other); 00135 }; 00136 00137 /** 00138 * returns a pointer to a clone of the parameters 00139 */ 00140 virtual functor::parameters* clone() const { 00141 return new parameters(*this); 00142 }; 00143 00144 /** 00145 * write the parameters in the given ioHandler 00146 * @param handler the ioHandler to be used 00147 * @param complete if true (the default) the enclosing begin/end will 00148 * be also written, otherwise only the data block will be 00149 * written. 00150 * @return true if write was successful 00151 */ 00152 # ifndef _LTI_MSC_6 00153 virtual bool write(ioHandler& handler,const bool complete=true) const 00154 # else 00155 bool writeMS(ioHandler& handler,const bool complete=true) const 00156 # endif 00157 { 00158 bool b = true; 00159 if (complete) { 00160 b = handler.writeBegin(); 00161 } 00162 00163 if (b) { 00164 lti::write(handler,"relativeMinimum",relativeMinimum); 00165 lti::write(handler,"noMaxValue",noMaxValue); 00166 lti::write(handler,"hystheresisThreshold",hystheresisThreshold); 00167 lti::write(handler,"maxNumber",maxNumber); 00168 00169 } 00170 00171 # ifndef _LTI_MSC_6 00172 // This is the standard C++ code, which MS Visual C++ 6 is not able to 00173 // compile... 00174 b = b && maximumFilter<T>::parameters::write(handler,false); 00175 # else 00176 bool (maximumFilter<T>::parameters::* p_writeMS)(ioHandler&, 00177 const bool) const = 00178 maximumFilter<T>::parameters::writeMS; 00179 b = b && (this->*p_writeMS)(handler,false); 00180 # endif 00181 00182 if (complete) { 00183 b = b && handler.writeEnd(); 00184 } 00185 00186 return b; 00187 } 00188 00189 # ifdef _LTI_MSC_6 00190 virtual bool write(ioHandler& handler, 00191 const bool complete = true) const { 00192 // ...we need this workaround to cope with another really 00193 // awful MSVC bug. 00194 return writeMS(handler,complete); 00195 } 00196 # endif 00197 00198 00199 /** 00200 * read the parameters from the given ioHandler 00201 * @param handler the ioHandler to be used 00202 * @param complete if true (the default) the enclosing begin/end will 00203 * be also read, otherwise only the data block will be read. 00204 * @return true if write was successful 00205 */ 00206 # ifndef _LTI_MSC_6 00207 virtual bool read(ioHandler& handler,const bool complete = true) 00208 # else 00209 bool readMS(ioHandler& handler,const bool complete=true) 00210 # endif 00211 { 00212 bool b = true; 00213 if (complete) { 00214 b = handler.readBegin(); 00215 } 00216 00217 if (b) { 00218 lti::read(handler,"relativeMinimum",relativeMinimum); 00219 lti::read(handler,"noMaxValue",noMaxValue); 00220 lti::read(handler,"hystheresisThreshold",hystheresisThreshold); 00221 lti::read(handler,"maxNumber",maxNumber); 00222 } 00223 00224 # ifndef _LTI_MSC_6 00225 // This is the standard C++ code, which MS Visual C++ 6 is not able to 00226 // compile... 00227 b = b && maximumFilter<T>::parameters::read(handler,false); 00228 # else 00229 bool (maximumFilter<T>::parameters::* p_readMS)(ioHandler&, 00230 const bool) = 00231 maximumFilter<T>::parameters::readMS; 00232 b = b && (this->*p_readMS)(handler,false); 00233 # endif 00234 00235 if (complete) { 00236 b = b && handler.readEnd(); 00237 } 00238 00239 return b; 00240 } 00241 00242 # ifdef _LTI_MSC_6 00243 virtual bool read(ioHandler& handler,const bool complete=true) { 00244 // ...we need this workaround to cope with another really awful MSVC 00245 // bug. 00246 return readMS(handler,complete); 00247 } 00248 # endif 00249 00250 // ------------------------------------------------ 00251 // the parameters 00252 // ------------------------------------------------ 00253 00254 /** 00255 * All pixels with values smaller than the maximum found in the 00256 * input channel multiplied by "relativeMinimum" will be 00257 * suppressed. (Zero means, all detected local minima will be kept) 00258 * 00259 * Default value: 0.0f; 00260 */ 00261 float relativeMinimum; 00262 00263 /** 00264 * All non-maximum values will be set to this value. All maxima will 00265 * keep their value. 00266 * 00267 * Default value: 0 00268 */ 00269 T noMaxValue; 00270 00271 /** 00272 * This value offers a more stronger suppression of non-maxima. 00273 * If a local maxima is detected in a given window (defined by 00274 * the attribute maximumFilter<T>::parameters::kernelSize) a 00275 * region search algorithm will look for pixels around with a value 00276 * greater than the local maxima found, the search will consider 00277 * for the expansion only pixels with a value greater than the 00278 * found local maxima multiplied by this hystheresis value. 00279 * 00280 * The seach will be done for each local maxima, starting with the 00281 * smallest ones. 00282 * 00283 * To activate this region-search mode, use a value between 0 (meaning 00284 * "search for a global maximum") and 1 ("detect all local minima"). 00285 * 00286 * A negative value or a value greater 1 deactivates this search 00287 * (saves some time). 00288 * 00289 * Default value: -1 (search deactivated) 00290 */ 00291 float hystheresisThreshold; 00292 00293 /** 00294 * Maximum number of local maxima to be kept. 00295 * 00296 * If you are only interested in a fixed number of maxima, the set 00297 * this attribute to a value larger or equal 1. If you want to 00298 * always obtaine the maximum number of local maxima, set this value 00299 * to -1. 00300 * 00301 * Default value: -1 (not used) 00302 */ 00303 int maxNumber; 00304 }; 00305 00306 /** 00307 * default constructor 00308 */ 00309 localMaxima(); 00310 00311 /** 00312 * Construct a functor using the given parameters 00313 */ 00314 localMaxima(const parameters& par); 00315 00316 /** 00317 * copy constructor 00318 * @param other the object to be copied 00319 */ 00320 localMaxima(const localMaxima& other); 00321 00322 /** 00323 * destructor 00324 */ 00325 virtual ~localMaxima(); 00326 00327 /** 00328 * returns the name of this type ("localMaxima") 00329 */ 00330 virtual const char* getTypeName() const; 00331 00332 /** 00333 * Extract the local maxima from srcdest and leave the result there too, 00334 * consisting on the maxima values in their respective positions and 00335 * in the rest of the image the value indicated in parameters::noMaxValue. 00336 * 00337 * @param srcdest matrix<T> with the source data. The result 00338 * will be left here too. 00339 * @return true if apply successful or false otherwise. 00340 */ 00341 bool apply(matrix<T>& srcdest); 00342 00343 /** 00344 * Extract the local maxima from srcdest and leave the result there too, 00345 * consisting on the maxima values in their respective positions and 00346 * in the rest of the image the value indicated in parameters::noMaxValue. 00347 * 00348 * @param srcdest vector<T> with the source data. The result 00349 * will be left here too. 00350 * @return true if apply successful or false otherwise. 00351 */ 00352 bool apply(vector<T>& srcdest); 00353 00354 /** 00355 * Extract the local maxima from src and leave the result in dest, 00356 * consisting on the maxima values in their respective positions and 00357 * in the rest of the image the value indicated in parameters::noMaxValue. 00358 * 00359 * @param src matrix<T> with the source data. 00360 * @param dest matrix<T> where the result will be left. 00361 * @return true if apply successful or false otherwise. 00362 */ 00363 bool apply(const matrix<T>& src,matrix<T>& dest); 00364 00365 /** 00366 * Extract the local maxima from src and leave the result in dest, 00367 * consisting on the maxima values in their respective positions 00368 * and in the rest of the image the value indicated in 00369 * parameters::noMaxValue. Those positions with local maxima are 00370 * also stored in the \a theMaxs list. 00371 * 00372 * operates on a copy of the given %parameters. 00373 * @param src matrix<T> with the source data. 00374 * @param dest matrix<T> where the result will be left. 00375 * @param theMaxs list of points where the local maxima are found. 00376 * It is sorted in decreasing order of the maxima values 00377 * @return true if apply successful or false otherwise. 00378 */ 00379 bool apply(const matrix<T>& src,matrix<T>& dest,pointList& theMaxs); 00380 00381 /** 00382 * Extract the local maxima from src and leave their position in the 00383 * given \a theMaxs point-list. 00384 * 00385 * @param src matrix<T> with the source data. 00386 * @param theMaxs list of points where the local maxima are found. 00387 * It is sorted in decreasing order of the maxima values 00388 * @return true if apply successful or false otherwise. 00389 */ 00390 bool apply(const matrix<T>& src,pointList& theMaxs); 00391 00392 /** 00393 * Extract the local maxima from src and leave the result in dest, 00394 * consisting on the maxima values in their respective positions and 00395 * in the rest of the image the value indicated in parameters::noMaxValue. 00396 * 00397 * @param src vector<T> with the source data. 00398 * @param dest vector<T> where the result will be left. 00399 * @return true if apply successful or false otherwise. 00400 */ 00401 bool apply(const vector<T>& src,vector<T>& dest); 00402 00403 /** 00404 * copy data of "other" functor. 00405 * @param other the functor to be copied 00406 * @return a reference to this functor object 00407 */ 00408 localMaxima& copy(const localMaxima& other); 00409 00410 /** 00411 * alias for copy member 00412 * @param other the functor to be copied 00413 * @return a reference to this functor object 00414 */ 00415 localMaxima& operator=(const localMaxima& other); 00416 00417 /** 00418 * returns a pointer to a clone of this functor. 00419 */ 00420 virtual functor* clone() const; 00421 00422 /** 00423 * returns used parameters 00424 */ 00425 const parameters& getParameters() const; 00426 00427 /** 00428 * set functor's parameters. 00429 * This member makes a copy of <em>theParam</em>: the functor 00430 * will keep its own copy of the parameters! 00431 * @return true if successful, false otherwise 00432 */ 00433 virtual bool updateParameters(); 00434 00435 protected: 00436 /** 00437 * operates on a copy of the given %parameters. 00438 * @param src vector<T> with the source data. 00439 * @param maxRes vector<T> with the source data, where is has been 00440 * filtered to contain the max of columns. 00441 * @param dest vector<T> where the result will be left. 00442 * @return true if apply successful or false otherwise. 00443 */ 00444 bool apply(const vector<T>& src,const vector<T>& maxRes, 00445 vector<T>& dest); 00446 00447 /** 00448 * this function evaluates if the given srcVal can be a local maximum 00449 * depending on the content of the buffer array (in the parent class). 00450 */ 00451 inline T getValue(const T srcVal) const; 00452 00453 /** 00454 * value used to indicate a "suppressed" value (usually 0) 00455 */ 00456 T suppressValue; 00457 00458 /** 00459 * minimumT is the smallest number that type T can describe 00460 */ 00461 static const T minimumT; 00462 00463 00464 /** 00465 * Suppression with hystheresis. 00466 * 00467 * Given all local maxima in localMax, this method sorts them 00468 * and beginning with the smallest ones, it starts a region 00469 * search through all pixels with a value greater than the local 00470 * maximum being evaluated multiplied with the parameter 00471 * hystheresisThreshold. All other local maxima found in the region 00472 * will be absorved if they are smaller, or they eliminate the actual 00473 * local maximum if they are greater. 00474 * 00475 * @param src the source image 00476 * @param localMax local maxima found. It may contain at the end 00477 * of the algorithm less maxima as in the beginning. 00478 * Also the minimumT value will be replaced by 00479 * suppressValue. 00480 * @param maxList list of local maxima sorted in descending order. 00481 */ 00482 bool hystheresisSuppression(const matrix<T>& src, 00483 matrix<T>& localMax, 00484 pointList& maxList) const; 00485 00486 }; 00487 } 00488 00489 #endif