Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members

Threshold.hpp

Go to the documentation of this file.
00001 /* ImLib3D
00002  * Copyright (c) 2001, ULP-IPB Strasbourg.
00003  *
00004  * This program is free software; you can redistribute it and/or modify
00005  * it under the terms of the GNU General Public License as published by
00006  * the Free Software Foundation; either version 2 of the License, or (at
00007  * your option) any later version.
00008  * 
00009  * This program is distributed in the hope that it will be useful, but
00010  * WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012  * General Public License for more details.
00013  * 
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software
00016  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00017  */
00020 #ifndef _Threshold_hpp
00021 #define _Threshold_hpp
00022 
00023 #include<ImLib3D/ImageProcessor.hpp>
00024 
00025 
00026 
00028 
00030 namespace IP3D
00031 {
00040     template <class ImageType>
00041     void SimpleThreshold(const ImageType& src,const typename ImageType::value_type &t,Mask3D& res)
00042     {
00043         res.Resize(src.Size());
00044         typename ImageType::const_iteratorFast sp;
00045         Mask3D::iteratorFast rp;
00046         for(sp=src.begin(),rp=res.begin();sp!=src.end();++sp,rp++){*rp= *sp>t ? 1 : 0;}
00047     }
00048 };
00049 
00050 
00051 
00053 
00055 namespace IP3D
00056 {
00066     template <class ImageType>
00067     void SimpleThresholds2(const ImageType& src, 
00068                       const typename ImageType::value_type &t1,
00069                       const typename ImageType::value_type &t2,
00070                       Mask3D& res)
00071     {
00072         res.Resize(src.Size());
00073         typename ImageType::const_iteratorFast sp;
00074         Mask3D::iteratorFast rp;
00075         for(sp=src.begin(),rp=res.begin();sp!=src.end();++sp,rp++){*rp= (*sp>=t1 && *sp<=t2) ? 1 : 0;}
00076     }
00077 
00078 };
00079 
00081 
00083 namespace IP3D
00084 {
00096     template <class ImageType>
00097     void LimitThreshold(const ImageType& src, 
00098                       const typename ImageType::value_type &t1,
00099                       const typename ImageType::value_type &t2,
00100                       ImageType& res)
00101     {
00102         res.Resize(src.Size());
00103         if (src.HasMask())
00104         {
00105             res.AddMask()=src.Mask();
00106             typename ImageType::const_iteratorFastMasked sp;
00107             typename ImageType::iteratorFastMasked rp;
00108             for(sp=src.begin(),rp=res.begin();sp!=src.end();++sp,rp++){*rp= min(max(*sp,t1),t2);}
00109         }
00110         else
00111         {
00112             typename ImageType::const_iteratorFast sp;
00113             typename ImageType::iteratorFast rp;
00114             for(sp=src.begin(),rp=res.begin();sp!=src.end();++sp,rp++){*rp= min(max(*sp,t1),t2);}
00115         }
00116     }
00117 
00118 };
00119 
00120 
00122 
00124 namespace IP3D
00125 {
00139     template <class ImageType>
00140     void OtsuThresholds(const ImageType& src, Mask3D& res,int nbClasses=2,vector<double> *pthresholds=NULL,int nbBins=1000);
00141 };
00142 
00143 
00144 namespace IP3D
00145 {
00148         void UniModalThreshold(const Image3Df& src, double& threshold, Mask3D* res=NULL);
00149 };
00150 
00151 
00152 
00153 
00154 #ifdef DEPRECATED
00155 
00157 class Optimizer1D
00158 {
00159     Function1Dd &f;
00160     double precision;
00161 public:
00163     double Solve(double x0,double ygoal=0,double x1=0,double x2=0)
00164     {
00165         if(x2==0 && x1==0){x2=x0+precision;x1=x0-precision;}
00166         mprintf("starting interval %f - %f    : x0=%f\n",x1,x2,x0);
00167 
00168         double y1=f(x1);
00169         double y2=f(x2);
00170         mprintf("first pos: %f %f  - %f %f\n",x1,y1,x2,y2);
00171         for(int i=0;y2==y1 && i<100;i++)
00172         {
00173             x2=x1+(x2-x1)*2;
00174             x1=x2+(x1-x2)*2;
00175             y1=f(x1);
00176             y2=f(x2);
00177             mprintf("y1==y2!expanded to %f %f  - %f %f  \n",x1,y1,x2,y2);
00178         }
00179         if(y2==y1){ThrowError("Optimizer1D:Solve failed y2==y1");}
00180         while(fabs(x2-x1)>precision)
00181         {
00182             // new estimate of solution
00183             double x=x1+(ygoal-y1)*(x2-x1)/(y2-y1);
00184             double y=f(x);
00185             mprintf("on %f %f  - %f %f   new estimate:%f %f\n",x1,y1,x2,y2,x,y);
00186             // replace worst point by new estimate
00187             if(y==y1){x1=x;}
00188             else      
00189             if(y==y2){x2=x;}
00190             else
00191             if(fabs(x2-x)>fabs(x1-x) && y!=y1)
00192             {x2=x;y2=y;}
00193             else
00194             {x1=x;y1=y;}
00195             if(y1==y2){ThrowError("Optimizer1D:Solve failed y2==y1 in opt loop");}
00196         }       
00197         return (x2+x1)/2;
00198     }
00199     Optimizer1D(Function1Dd &_f,double _precision):
00200         f(_f),precision(_precision)
00201     {
00202     }
00203 };
00204 
00205 #endif// DEPRECATED
00206 
00207 
00208 
00209 #include<ImLib3D/Threshold.hxx>
00210 
00211 #endif // _Threshold_hpp

Generated on Fri Jun 17 13:36:07 2005 for ImLib3D by  doxygen 1.4.2