latest version v1.9 - last update 10 Apr 2010 |
00001 /* 00002 * Copyright (C) 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 .......: ltiMulticlassNormalizedCuts.h 00027 * authors ....: Pablo Alvarado 00028 * organization: LTI, RWTH Aachen 00029 * creation ...: 10.10.2003 00030 * revisions ..: $Id: ltiMulticlassNormalizedCuts.h,v 1.7 2006/02/08 11:32:14 ltilib Exp $ 00031 */ 00032 00033 #ifndef _LTI_MULTICLASS_NORMALIZED_CUTS_H_ 00034 #define _LTI_MULTICLASS_NORMALIZED_CUTS_H_ 00035 00036 00037 #include "ltiMatrix.h" 00038 #include "ltiVector.h" 00039 #include "ltiFunctor.h" 00040 00041 namespace lti { 00042 /** 00043 * Multiclass normalized cuts. 00044 * 00045 * This class implements the k-ways normalized cuts algorithm proposed by 00046 * Stella Yu in: 00047 * 00048 * S. Yu, "Computational Models of Perceptual Organization". Ph.D Thesis, 00049 * Carnegie Mellon University, May 2003. 00050 * 00051 * The algorithm is general enough to be applied to any graph partitioning 00052 * problem given the affinity matrix w, which contains the weights between 00053 * each two nodes. It must be symmetric (and obviously square) and its 00054 * values must be greater of equal zero. 00055 * 00056 * The weights denote usually similarity measures (and not distances) between 00057 * two nodes. 00058 * 00059 * The output of the algorithm is an integer vector with labels for each of 00060 * the n nodes of the graph, corresponding to the indices of the vectors in 00061 * the affinity matrix. 00062 * 00063 * If you compiled the LTI-Lib with LAPACK support, this vector will work 00064 * much faster than without. This is because it makes use of an 00065 * eigensolution functor and the singular value decomposition functor, which 00066 * are both efficiently computed by the LAPACK. 00067 */ 00068 class multiclassNormalizedCuts : public functor { 00069 public: 00070 /** 00071 * the parameters for the class multiclassNormalizedCuts 00072 */ 00073 class parameters : public functor::parameters { 00074 public: 00075 /** 00076 * default constructor 00077 */ 00078 parameters(); 00079 00080 /** 00081 * copy constructor 00082 * @param other the parameters object to be copied 00083 */ 00084 parameters(const parameters& other); 00085 00086 /** 00087 * destructor 00088 */ 00089 ~parameters(); 00090 00091 /** 00092 * returns name of this type 00093 */ 00094 const char* getTypeName() const; 00095 00096 /** 00097 * copy the contents of a parameters object 00098 * @param other the parameters object to be copied 00099 * @return a reference to this parameters object 00100 */ 00101 parameters& copy(const parameters& other); 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& operator=(const parameters& other); 00109 00110 00111 /** 00112 * returns a pointer to a clone of the parameters 00113 */ 00114 virtual functor::parameters* clone() const; 00115 00116 /** 00117 * write the parameters in the given ioHandler 00118 * @param handler the ioHandler to be used 00119 * @param complete if true (the default) the enclosing begin/end will 00120 * be also written, otherwise only the data block will be written. 00121 * @return true if write was successful 00122 */ 00123 virtual bool write(ioHandler& handler,const bool complete=true) const; 00124 00125 /** 00126 * read the parameters from the given ioHandler 00127 * @param handler the ioHandler to be used 00128 * @param complete if true (the default) the enclosing begin/end will 00129 * be also written, otherwise only the data block will be written. 00130 * @return true if write was successful 00131 */ 00132 virtual bool read(ioHandler& handler,const bool complete=true); 00133 00134 # ifdef _LTI_MSC_6 00135 /** 00136 * this function is required by MSVC only, as a workaround for a 00137 * very awful bug, which exists since MSVC V.4.0, and still by 00138 * V.6.0 with all bugfixes (so called "service packs") remains 00139 * there... This method is also public due to another bug, so please 00140 * NEVER EVER call this method directly: use read() instead 00141 */ 00142 bool readMS(ioHandler& handler,const bool complete=true); 00143 00144 /** 00145 * this function is required by MSVC only, as a workaround for a 00146 * very awful bug, which exists since MSVC V.4.0, and still by 00147 * V.6.0 with all bugfixes (so called "service packs") remains 00148 * there... This method is also public due to another bug, so please 00149 * NEVER EVER call this method directly: use write() instead 00150 */ 00151 bool writeMS(ioHandler& handler,const bool complete=true) const; 00152 # endif 00153 00154 // ------------------------------------------------ 00155 // the parameters 00156 // ------------------------------------------------ 00157 00158 /** 00159 * Number of classes the graph need to be separated into. 00160 * This value must be greater of equal 2. 00161 * 00162 * Default value: 2 00163 */ 00164 int k; 00165 00166 }; 00167 00168 /** 00169 * default constructor 00170 */ 00171 multiclassNormalizedCuts(); 00172 00173 /** 00174 * Construct a functor using the given parameters 00175 */ 00176 multiclassNormalizedCuts(const parameters& par); 00177 00178 /** 00179 * copy constructor 00180 * @param other the object to be copied 00181 */ 00182 multiclassNormalizedCuts(const multiclassNormalizedCuts& other); 00183 00184 /** 00185 * destructor 00186 */ 00187 virtual ~multiclassNormalizedCuts(); 00188 00189 /** 00190 * returns the name of this type ("multiclassNormalizedCuts") 00191 */ 00192 virtual const char* getTypeName() const; 00193 00194 /** 00195 * Compute the k-way partitioning of a graph with affinity matrix 00196 * "weights". 00197 * 00198 * @param weights the affinity matrix containing the weights between each 00199 * two nodes of the graph. This square matrix must also be 00200 * symmetric. The elements weights.at(i,j) denote the 00201 * similarity between the nodes i and j and must be positive 00202 * or equal zero. Please note that this are similarity 00203 * metrics and \b not distance metrics. 00204 * In the original paper is denoted with W. 00205 * @param xstar (X* in the paper) is a vector containing the label 00206 * of each node. Each values will be between 0 and k-1, 00207 * where k is the value given in the parameters object. 00208 * @return true if apply successful or false otherwise. 00209 */ 00210 bool apply(const dmatrix& weights, 00211 ivector& xstar) const; 00212 00213 /** 00214 * Compute the k-way partitioning of a graph with affinity matrix 00215 * "weights". 00216 * 00217 * @param k number of classes to be found, i.e. k subgraphs will be 00218 * detected. 00219 * @param weights the affinity matrix containing the weights between each 00220 * two nodes of the graph. This square matrix must also be 00221 * symmetric. The elements must be positive or equal zero, 00222 * and correspond to similarity metrics (not distance 00223 * metrics). In the original paper is denoted with W 00224 * @param xstar (X* in the paper) is a vector containing the label 00225 * of each node. The values will be between 0 and k-1 where 00226 * k is the value given as argument (the value k in the 00227 * parameters object will be ignored). 00228 * @return true if apply successful or false otherwise. 00229 */ 00230 bool apply(const int k, 00231 const dmatrix& weights, 00232 ivector& xstar) const; 00233 00234 /** 00235 * Compute the k-way partitioning of a graph with affinity matrix 00236 * "weights". 00237 * 00238 * @param k number of classes to be found, i.e. k subgraphs will be 00239 * detected. 00240 * @param weights the affinity matrix containing the weights between each 00241 * two nodes of the graph. This square matrix must also be 00242 * symmetric. The elements must be positive or equal zero, 00243 * and correspond to similarity metrics (not distance 00244 * metrics). In the original paper is denoted with W 00245 * @param degree the degree vector, defined as the sum of elements of each 00246 * row of the weights. 00247 * @param xstar (X* in the paper) is a vector containing the label 00248 * of each node. The values will be between 0 and k-1 where 00249 * k is the value given as argument (the value k in the 00250 * parameters object will be ignored). 00251 * @return true if apply successful or false otherwise. 00252 */ 00253 bool apply(const int k, 00254 const dmatrix& weights, 00255 const dvector& degree, 00256 ivector& xstar) const; 00257 00258 /** 00259 * copy data of "other" functor. 00260 * @param other the functor to be copied 00261 * @return a reference to this functor object 00262 */ 00263 multiclassNormalizedCuts& copy(const multiclassNormalizedCuts& other); 00264 00265 /** 00266 * alias for copy member 00267 * @param other the functor to be copied 00268 * @return a reference to this functor object 00269 */ 00270 multiclassNormalizedCuts& operator=(const multiclassNormalizedCuts& other); 00271 00272 /** 00273 * returns a pointer to a clone of this functor. 00274 */ 00275 virtual functor* clone() const; 00276 00277 /** 00278 * returns used parameters 00279 */ 00280 const parameters& getParameters() const; 00281 00282 00283 protected: 00284 /** 00285 * Merge regions using the "k-Way normalized cuts" method of Stella Yu 00286 * (See S. Yu "Computational Models of Perceptual Organization" Tech. 00287 * Report CMU-RI-TR-03-14, Carnegie Mellon University, May 2003. 00288 * 00289 * The main difference with the original paper is that the graph is not 00290 * formed of single connected pixels, but regions using a simpler faster 00291 * method to over-partition the image. 00292 * 00293 * The resulting regions mask will contain only \a k labels. 00294 * 00295 * @param W the weights matrix (or affinity matrix) containing the 00296 * weight (similarity and NOT distance) between each to nodes 00297 * of the graph representation. 00298 * @param D degree vector containing the sum of elements of each row of W 00299 * @param k number of classes the final partition should have. 00300 * @param Xstar the resulting vector contains for each corresponding node 00301 * a label name (from 0 to k-1) for the corresponding label. 00302 */ 00303 bool kWayNormalizedCut(const dmatrix& W, 00304 const dvector& D, 00305 const int k, 00306 ivector& Xstar) const; 00307 00308 /** 00309 * Compute the degree vector of the given affinity matrix W. 00310 * It is defined as the sum of the rows. 00311 */ 00312 void computeDegree(const dmatrix& W, dvector& D) const; 00313 00314 }; 00315 } 00316 00317 #endif