latest version v1.9 - last update 10 Apr 2010 |
00001 /* 00002 * Copyright (C) 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 .......: ltiMedianCut.h 00027 * authors ....: Norman Pfeil 00028 * organization: LTI, RWTH Aachen 00029 * creation ...: 18.5.2001 00030 * revisions ..: $Id: ltiMedianCut.h,v 1.10 2006/02/08 11:27:36 ltilib Exp $ 00031 */ 00032 00033 #ifndef _LTI_MEDIAN_CUT_H_ 00034 #define _LTI_MEDIAN_CUT_H_ 00035 00036 #include "ltiHistogram.h" 00037 #include "ltiTypes.h" 00038 #include "ltiColorQuantization.h" 00039 #include "ltiMath.h" 00040 00041 #include <list> 00042 00043 namespace lti { 00044 00045 /** 00046 * This class does color quantization with median-cut-algorithm 00047 * (Heckbert, MIT 1980) 00048 * 00049 * Description of the algorithm: 00050 * http://www-lehre.informatik.uni-osnabrueck.de/~cg/2000/skript/10_7_Erzeugung_einer.html 00051 */ 00052 class medianCut : public colorQuantization { 00053 public: 00054 /** 00055 * the parameters for the class medianCut 00056 */ 00057 class parameters : public colorQuantization::parameters { 00058 public: 00059 /** 00060 * default constructor 00061 */ 00062 parameters(); 00063 00064 /** 00065 * copy constructor 00066 * @param other the parameters object to be copied 00067 */ 00068 parameters(const parameters& other); 00069 00070 /** 00071 * destructor 00072 */ 00073 ~parameters(); 00074 00075 /** 00076 * returns name of this type 00077 */ 00078 const char* getTypeName() const; 00079 00080 /** 00081 * copy the contents of a parameters object 00082 * @param other the parameters object to be copied 00083 * @return a reference to this parameters object 00084 */ 00085 parameters& copy(const parameters& other); 00086 00087 /** 00088 * copy the contents of a parameters object 00089 * @param other the parameters object to be copied 00090 * @return a reference to this parameters object 00091 */ 00092 parameters& operator=(const parameters& other); 00093 00094 00095 /** 00096 * returns a pointer to a clone of the parameters 00097 */ 00098 virtual functor::parameters* clone() const; 00099 00100 /** 00101 * write the parameters in the given ioHandler 00102 * @param handler the ioHandler to be used 00103 * @param complete if true (the default) the enclosing begin/end will 00104 * be also written, otherwise only the data block will be written. 00105 * @return true if write was successful 00106 */ 00107 virtual bool write(ioHandler& handler,const bool complete=true) const; 00108 00109 /** 00110 * write the parameters in the given ioHandler 00111 * @param handler the ioHandler to be used 00112 * @param complete if true (the default) the enclosing begin/end will 00113 * be also written, otherwise only the data block will be written. 00114 * @return true if write was successful 00115 */ 00116 virtual bool read(ioHandler& handler,const bool complete=true); 00117 00118 # ifdef _LTI_MSC_6 00119 /** 00120 * this function is required by MSVC only, as a workaround for a 00121 * very awful bug, which exists since MSVC V.4.0, and still by 00122 * V.6.0 with all bugfixes (so called "service packs") remains 00123 * there... This method is also public due to another bug, so please 00124 * NEVER EVER call this method directly: use read() instead 00125 */ 00126 bool readMS(ioHandler& handler,const bool complete=true); 00127 00128 /** 00129 * this function is required by MSVC only, as a workaround for a 00130 * very awful bug, which exists since MSVC V.4.0, and still by 00131 * V.6.0 with all bugfixes (so called "service packs") remains 00132 * there... This method is also public due to another bug, so please 00133 * NEVER EVER call this method directly: use write() instead 00134 */ 00135 bool writeMS(ioHandler& handler,const bool complete=true) const; 00136 # endif 00137 00138 // ------------------------------------------------ 00139 // the parameters 00140 // ------------------------------------------------ 00141 // If you add more parameters manually, do not forget to do following: 00142 // 1. indicate in the default constructor the default values 00143 // 2. make sure that the copy member also copy your new parameters 00144 // 3. make sure that the read and write members also read and 00145 // write your parameters 00146 00147 00148 /** 00149 * preQuant: number of colors per dimension for pre-Quantization 00150 * for example: 32 means ==> 0-31 for r,g,b ==> 32^3 = 32768 colors 00151 * 00152 * Range: 1-256 00153 * 00154 * Default: 32 00155 * 00156 * It is recommended not to change this value. 00157 */ 00158 int preQuant; 00159 00160 }; 00161 00162 /** 00163 * default constructor 00164 */ 00165 medianCut(); 00166 00167 /** 00168 * copy constructor 00169 * @param other the object to be copied 00170 */ 00171 medianCut(const medianCut& other); 00172 00173 /** 00174 * destructor 00175 */ 00176 virtual ~medianCut(); 00177 00178 /** 00179 * returns the name of this type ("medianCut") 00180 */ 00181 virtual const char* getTypeName() const; 00182 00183 /** 00184 * This apply method calculates a channel8, containing the indices of the 00185 * quantized colors. Will deliver empty channel8, if more than 256 colors!! 00186 * 00187 * @param src image with the source data. 00188 * @param dest channel8 where the result will be left. 00189 * @return true if successful, false otherwise 00190 */ 00191 bool apply(const image &src, channel8 &dest) const; 00192 00193 00194 /** 00195 * This apply method calculates a channel8, containing the indices of the 00196 * quantized colors AND the palette. Will deliver empty channel8, if more 00197 * than 256 colors!! But palette will be O.K. 00198 * 00199 * @param src image with the source data. 00200 * @param dest channel8 where the result will be left. 00201 * @param thePalette palette appendant to dest. 00202 * @return true if successful, false otherwise 00203 */ 00204 bool apply(const image &src, 00205 channel8 &dest, 00206 palette &thePalette) const; 00207 00208 /** 00209 * This apply method calculates a quantized image (on place) 00210 * 00211 * @param srcdest image with the source data. The result will be left here too. 00212 * @return true if successful, false otherwise 00213 */ 00214 bool apply(image& srcdest) const; 00215 00216 /** 00217 * This apply method calculates a quantized image (on copy) 00218 * 00219 * @param src image with the source data. 00220 * @param dest image where the result will be left. 00221 * @return true if successful, false otherwise 00222 */ 00223 bool apply(const image& src,image& dest) const; 00224 00225 /** 00226 * This apply method calculates a a quantized image and respective palette. 00227 * 00228 * @param src image with the source data. 00229 * @param dest color quantized image 00230 * @param thePalette color palette used 00231 */ 00232 bool apply(const image& src, 00233 image& dest, 00234 palette& thePalette) const; 00235 00236 /** 00237 * copy data of "other" functor. 00238 * @param other the functor to be copied 00239 * @return a reference to this functor object 00240 */ 00241 medianCut& copy(const medianCut& other); 00242 00243 /** 00244 * returns a pointer to a clone of this functor. 00245 */ 00246 virtual functor* clone() const; 00247 00248 /** 00249 * returns used parameters 00250 */ 00251 const parameters& getParameters() const; 00252 00253 private: 00254 /** 00255 * The function, where quantization takes place! 00256 * 00257 * @param src image with the source data. 00258 * @param dest quantized image. 00259 * @param mask channel8 with labeled regions (will only be set if < 256 colors). 00260 * @param thePalette the quantized palette. 00261 * @return true if successful, false otherwise 00262 */ 00263 bool performQuantization(const image& src, 00264 image& dest, 00265 channel8& mask, 00266 palette &thePalette) const; 00267 00268 /** 00269 * struct for holding box information 00270 */ 00271 struct boxInfo { 00272 /** 00273 * constructor 00274 */ 00275 boxInfo() { 00276 colorFrequency=0; 00277 } 00278 00279 /** 00280 * give corners of box 00281 */ 00282 boxInfo(const lti::rgbPixel& minVal, 00283 const lti::rgbPixel& maxVal) { 00284 min=minVal; 00285 max=maxVal; 00286 } 00287 00288 /** 00289 * first corner with lowest coordinates in RGB 00290 */ 00291 lti::rgbPixel min; 00292 00293 /** 00294 * second corner with highest coordinates in RGB 00295 */ 00296 lti::rgbPixel max; 00297 00298 /** 00299 * how many pixels are in the box 00300 */ 00301 long int colorFrequency; 00302 00303 /** 00304 * mean value 00305 */ 00306 double mean[3]; 00307 /** 00308 * variance 00309 */ 00310 double var[3]; 00311 00312 /** 00313 * number of colors (?) 00314 */ 00315 int colors; 00316 }; 00317 00318 /** 00319 * boxInfo.min and .max must be specified, when using this function, then 00320 * missing information in boxInfo is computed (mean, var, colorFrequency, 00321 * colors) and box boundaries (min,max) are set to the smallest size, that 00322 * still encloses all entries in the specified range of the histogram. 00323 */ 00324 void computeBoxInfo(const histogram& hist, 00325 boxInfo& theBox) const; 00326 }; 00327 00328 00329 } 00330 00331 #endif