latest version v1.9 - last update 10 Apr 2010 |
00001 /* 00002 * Copyright (C) 1999, 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 .......: ltiSnake.h 00027 * authors ....: Pablo Alvarado, Peter Gerber, Axel Berner 00028 * organization: LTI, RWTH Aachen 00029 * creation ...: 15.04.99 00030 * revisions ..: $Id: ltiSnake.h,v 1.9 2006/02/08 11:51:43 ltilib Exp $ 00031 */ 00032 00033 #ifndef _LTI_SNAKE_H 00034 #define _LTI_SNAKE_H 00035 00036 #include "ltiContour.h" 00037 #include "ltiImage.h" 00038 #include "ltiTypes.h" 00039 #include "ltiSegmentation.h" 00040 #include "ltiGenericMatrix.h" 00041 00042 namespace lti { 00043 00044 /** 00045 * Snake segmentation. 00046 * 00047 * This is a very old class that do not implement the "real" snakes 00048 * but a form of connected region growing algorithms (a very simple 00049 * case of geometrical snakes). 00050 * 00051 * If you have implemented a real snake functor, we would be glad to 00052 * replace this one with it. 00053 * 00054 * \deprecated This is not a real snake functor. 00055 */ 00056 class snake : public segmentation { 00057 public: 00058 /** 00059 * Parameters for the snakes 00060 */ 00061 class parameters: public segmentation::parameters { 00062 public: 00063 /** 00064 * default constructor 00065 */ 00066 parameters(); 00067 /** 00068 * destructor 00069 */ 00070 virtual ~parameters(); 00071 /** 00072 * creates a clone of this object 00073 */ 00074 virtual functor::parameters* clone() const; 00075 /** 00076 * become the other object 00077 */ 00078 parameters& copy(const parameters& other); 00079 /** 00080 * name of this type 00081 */ 00082 virtual const char* getTypeName() const; 00083 00084 /** 00085 * write the parameters in the given ioHandler 00086 * @param handler the ioHandler to be used 00087 * @param complete if true (the default) the enclosing begin/end will 00088 * be also written, otherwise only the data block will be written. 00089 * @return true if write was successful 00090 */ 00091 virtual bool write(ioHandler& handler, 00092 const bool complete=true) const; 00093 00094 /** 00095 * write the parameters in the given ioHandler 00096 * @param handler the ioHandler to be used 00097 * @param complete if true (the default) the enclosing begin/end will 00098 * be also written, otherwise only the data block will be written. 00099 * @return true if write was successful 00100 */ 00101 virtual bool read(ioHandler& handler,const bool complete=true); 00102 00103 # ifdef _LTI_MSC_6 00104 /** 00105 * this function is required by MSVC only, as a workaround for a 00106 * very awful bug, which exists since MSVC V.4.0, and still by 00107 * V.6.0 with all bugfixes (so called "service packs") remains 00108 * there... This method is public due to another bug!, so please 00109 * NEVER EVER call this method directly 00110 */ 00111 bool readMS(ioHandler& handler,const bool complete=true); 00112 00113 /** 00114 * this function is required by MSVC only, as a workaround for a 00115 * very awful bug, which exists since MSVC V.4.0, and still by 00116 * V.6.0 with all bugfixes (so called "service packs") remains 00117 * there... This method is public due to another bug!, so please 00118 * NEVER EVER call this method directly 00119 */ 00120 bool writeMS(ioHandler& handler,const bool complete=true) const; 00121 # endif 00122 00123 // ----------------------- 00124 // the parameters 00125 // ----------------------- 00126 00127 /** 00128 * placement of the reference color patch. This is a rectangular 00129 * region that will be ignored by the initialization of the snake 00130 * 00131 * Default value: rectangle(180,0,255,30) 00132 */ 00133 rectangle patchPlace; 00134 00135 /** 00136 * dimension of the linear filter used to identify the object 00137 * (in pixel) This is the number of pixels (on a line 00138 * perpendicular to the actual snake) that will be considered to 00139 * evaluate if the active contour should be moved or not. 00140 * 00141 * Default value: 5 00142 */ 00143 int lfilter; 00144 00145 /** 00146 * average 00147 * 00148 * These are the threshold values used to determine if a new pixel belongs 00149 * to the outer region or not. The values for each color channel should be 00150 * between 0.0f and 255.0f 00151 * 00152 * Default value: frgbPixel(10.f,10.f,10.f) 00153 */ 00154 frgbPixel average; 00155 00156 /** 00157 * variance 00158 * 00159 * These are the threshold values used to determine if a new pixel belongs 00160 * to the outer region or not. The values for each color channel should be 00161 * between 0.0f and 255.0f 00162 * 00163 * Default value: frgbPixel(10.f,10.f,10.f) 00164 */ 00165 frgbPixel variance; 00166 00167 /** 00168 * Allow the snake to split into two independent contours. 00169 * 00170 * Default value: false 00171 */ 00172 bool splitting; 00173 00174 00175 }; 00176 00177 /** 00178 * default constructor 00179 */ 00180 snake(); 00181 00182 /** 00183 * copy constructor 00184 */ 00185 snake(const snake& other) 00186 : segmentation() {copy(other);} 00187 00188 /** 00189 * destructor 00190 */ 00191 ~snake(); 00192 00193 /** 00194 * copy data of "other" functor 00195 */ 00196 snake& copy(const snake& other); 00197 00198 /** 00199 * clone member 00200 */ 00201 virtual functor* clone() const; 00202 00203 /** 00204 * OnCopy apply 00205 */ 00206 bool apply(const image& imgIn,areaPoints& newAreaPoints); 00207 00208 /** 00209 * get parameters 00210 */ 00211 const parameters& getParameters() const; 00212 00213 /** 00214 * get parameters 00215 */ 00216 parameters& getParameters(); 00217 00218 /** 00219 * Structure for a snake element 00220 */ 00221 class element : public ioObject { 00222 public: 00223 /** 00224 * chain code for the next element 00225 */ 00226 chainCode next; 00227 00228 /** 00229 * chain code for the previous element 00230 */ 00231 chainCode prev; 00232 00233 /** 00234 * flag to indicate if the element might be moved or not 00235 */ 00236 bool fixed; 00237 00238 /** 00239 * default constructor makes all elements moveable 00240 */ 00241 element() : fixed(false) {}; 00242 00243 /** 00244 * default constructor makes all elements moveable 00245 */ 00246 element(const int& i) : fixed(false) {}; 00247 00248 // just required to instantiate an lti::matrix 00249 bool operator==(const element& other) const { 00250 return this == &other; 00251 } 00252 00253 operator int() { 00254 return static_cast<int>(next.getDirection()); 00255 } 00256 00257 operator ubyte() { 00258 return static_cast<ubyte>(next.getDirection()); 00259 } 00260 00261 operator const int() const { 00262 return static_cast<int>(next.getDirection()); 00263 } 00264 00265 operator const ubyte() const { 00266 return static_cast<ubyte>(next.getDirection()); 00267 } 00268 00269 /** @name Storable interface 00270 * Members for the storable interface 00271 */ 00272 //@{ 00273 /** 00274 * read the matrix from the given ioHandler. The complete flag indicates 00275 * if the enclosing begin and end should be also be readed 00276 */ 00277 virtual bool read(ioHandler& handler,const bool complete=true); 00278 00279 /** 00280 * write the matrix in the given ioHandler. The complete flag indicates 00281 * if the enclosing begin and end should be also be written or not 00282 */ 00283 virtual bool write(ioHandler& handler,const bool complete=true) const; 00284 //@} 00285 00286 }; 00287 00288 protected: 00289 /** 00290 * set a snake 00291 * The firstSnake must contain a valid borderPoints list!! 00292 */ 00293 void setSnake(const borderPoints& aSnake); 00294 00295 /** uses two chainCodes in the order prev,next to check if 00296 there is an object left or right of the element */ 00297 inline bool objectRight(const chainCode& prev, 00298 const chainCode& next) const; 00299 00300 /** returns the snake as areaPoints */ 00301 bool getContour(areaPoints& newAreaPoints); 00302 00303 // here is valid to use a lti::vector, because no dynamic memory 00304 // allocation is done by "element"! 00305 genericMatrix<element> snakeField; // Snake 00306 00307 /** 00308 * image rows and columns 00309 */ 00310 point size; 00311 00312 /** 00313 * pointer to the image 00314 */ 00315 const image* img; 00316 00317 /** 00318 * 00319 */ 00320 pointList anchorPoint; 00321 00322 private: 00323 /** 00324 * Set reference snake 00325 */ 00326 void initReference(borderPoints& colorReference); 00327 00328 /** 00329 * Set object snake without the possible reference-color-patch 00330 */ 00331 void initObject(borderPoints& firstSnake); 00332 00333 /** 00334 * Let the snake find an object 00335 */ 00336 void findObjects(); 00337 00338 /** 00339 * Move one snake element. 00340 */ 00341 bool move(point pos); 00342 00343 /** 00344 * Check if the element at 'p' is free, pos is the asking element 00345 */ 00346 bool freeAndAllowed(const point pos, const point p); 00347 00348 /** 00349 * Delete snake if it consists of two elements 00350 */ 00351 bool AreLastTwo(point& pos, pointList::iterator& actAnchor); 00352 00353 /** 00354 * split snake if it has contact to itself 00355 */ 00356 bool TryToSplit(point& pos, pointList::iterator& actAnchor); 00357 00358 }; 00359 00360 } 00361 #endif