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

Convolution.hxx

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  */
00019 #ifndef _Convolution_hxx
00020 #define _Convolution_hxx
00021 
00022 template <class ImageType>
00023 inline void
00024 IP3D::Convolution(const ImageType& imToConvolute, const Filter& imFilter, ImageType& imResult)
00025 {
00026     ImageProcessorArgs<ImageType> args(ImArgs(imToConvolute),imResult);
00027     args.SameSize();
00028     ImageType &imDest=args.GetDest();
00029 
00030     typename ImageType::const_iteratorXYZ pIm;
00031     typename ImageType::iteratorFast pDest;
00032     Filter::const_iteratorXYZ pFilter;
00033     
00034     int rx = imFilter.GetCenter().x;
00035     int ry = imFilter.GetCenter().y;
00036     int rz = imFilter.GetCenter().z;
00037 
00038     for (pDest = imDest.begin(), pIm = imToConvolute.begin(); pDest!=imDest.end(); pDest++, pIm++)
00039     {
00040         (*pDest) = Zero<typename ImageType::value_type>();
00041         for (pFilter = imFilter.begin(); pFilter !=imFilter.end(); pFilter++)
00042         {
00043             (*pDest) += (typename ImageType::value_type) 
00044                 ((*pFilter)*imToConvolute.SafeValue(pIm.x - (pFilter.x - rx) , 
00045                                                          pIm.y - (pFilter.y - ry) , 
00046                                                          pIm.z - (pFilter.z - rz) ) );
00047         }
00048 //          if (!(rand()%1000)){
00049 //              cout << "Value: " << *pDest << endl;
00050 //              getchar();
00051 //          }
00052     }
00053     //args.Finished();
00054 }
00055 
00056 // gcc3 complex doen't support float * comlex<double>
00057 //#ifdef _FFT_hpp
00058 namespace IP3D
00059 {
00060 template <>
00061 inline void
00062 Convolution<Image3Dcomplex >(const Image3Dcomplex& imToConvolute, const Filter& imFilter, Image3Dcomplex& imResult)
00063 {
00064     ImageProcessorArgs<Image3Dcomplex> args(ImArgs(imToConvolute),imResult);
00065     args.SameSize();
00066     Image3Dcomplex &imDest=args.GetDest();
00067 
00068     Image3Dcomplex::const_iteratorXYZ pIm;
00069     Image3Dcomplex::iteratorFast pDest;
00070     Filter::const_iteratorXYZ pFilter;
00071     
00072     int rx = imFilter.GetCenter().x;
00073     int ry = imFilter.GetCenter().y;
00074     int rz = imFilter.GetCenter().z;
00075 
00076     for (pDest = imDest.begin(), pIm = imToConvolute.begin(); pDest!=imDest.end(); pDest++, pIm++)
00077     {
00078         (*pDest) = Zero<Image3Dcomplex::value_type>();
00079         for (pFilter = imFilter.begin(); pFilter !=imFilter.end(); pFilter++)
00080         {
00081             (*pDest) += (Image3Dcomplex::value_type) 
00082                 (((double)(*pFilter))*imToConvolute.SafeValue(pIm.x - (pFilter.x - rx) , 
00083                                                          pIm.y - (pFilter.y - ry) , 
00084                                                          pIm.z - (pFilter.z - rz) ) );
00085         }
00086 //          if (!(rand()%1000)){
00087 //              cout << "Value: " << *pDest << endl;
00088 //              getchar();
00089 //          }
00090     }
00091     //args.Finished();
00092 }
00093 //#endif //_FFT_hpp
00094 }
00095 
00096 template <class ImageType>
00097 void
00098 IP3D::ConvolutionFFT(const ImageType& imToConvolute, const Filter& imFilter, ImageType& imResult)
00099 {
00100     ImageProcessorArgs<ImageType> args(ImArgs(imToConvolute),imResult);
00101     args.SameSize();
00102 //      ImageType &imDest=args.GetDest();
00103 
00104     ThrowError("IP3D::ConvolutionFFT Error: not yet implemented");
00105     
00106     //args.Finished();
00107 
00108 }
00109 
00110 template <class ImageType>
00111 void
00112 IP3D::SeparableConvolution(const ImageType& imToConvolute, const SeparableFilter& filter, ImageType& imResult)
00113 {
00114     ImageProcessorArgs<ImageType> args(ImArgs(imToConvolute),imResult);
00115     args.SameSize();
00116     ImageType &imDest=args.GetDest();
00117 
00118     typename ImageType::value_type zero;
00119     SetZero<typename ImageType::value_type>::Set(zero);
00120     imDest.Fill(zero);
00121 
00122     int rx = filter.iCenter.x;
00123     int ry = filter.iCenter.y;
00124     int rz = filter.iCenter.z;
00125 
00126     typename ImageType::iteratorXYZ p;
00127     ImageProgress tracker("SeparableConvolution (3x)",p);
00128 
00129     // Convolute X direction
00130 //  ImageType xRes(imDest.Size());
00131     ImageType &xRes=imDest;
00132 //  cout << "Convolution in X direction:" << endl;
00133     for (p = xRes.begin(); p != xRes.end(); p++)
00134     {
00135         *p = Zero<typename ImageType::value_type>();
00136 
00137         for (int n=0; n< filter.size.width; n++)
00138         {
00139             (*p) += (typename ImageType::value_type) 
00140                 (filter.xcomponent[n]*imToConvolute.SafeValue(p.x - (n - rx), 
00141                                                               p.y, 
00142                                                               p.z ));
00143         }
00144     }
00145 //  cout << "Convolution in Y direction:" << endl;
00146     // Convolute Y direction
00147     ImageType yRes(imDest.Size());
00148     for (p = yRes.begin(); p != yRes.end(); p++)
00149     {
00150         *p = Zero<typename ImageType::value_type>();
00151         for (int n=0; n< filter.size.height; n++)
00152         {
00153             (*p) += (typename ImageType::value_type) 
00154                 (filter.ycomponent[n]*xRes.SafeValue(p.x, 
00155                                                      p.y - (n - ry), 
00156                                                      p.z ));
00157         }
00158     }
00159 
00160 //      cout << "Convolution in Z direction:" << endl;
00161     // Convolute Z direction
00162     for (p = imDest.begin(); p != imDest.end(); p++)
00163     {
00164         *p = Zero<typename ImageType::value_type>();
00165         for (int n=0; n< filter.size.depth; n++)
00166         {
00167             (*p) += (typename ImageType::value_type) 
00168                 (filter.zcomponent[n]*yRes.SafeValue(p.x, 
00169                                                      p.y, 
00170                                                      p.z  - (n - rz)));
00171         }
00172     }
00173 }
00174 
00175 
00176 #endif // _Convolution_hxx

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