00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038 #include "CUDA/CudaImage.H"
00039 #include "Util/Assert.H"
00040 #include "CUDA/cudadefs.h"
00041 #include "CUDA/CudaConvolutions.H"
00042 #include "CUDA/CudaMathOps.H"
00043 #include "CUDA/CudaShapeOps.H"
00044 #include "CUDA/CudaKernels.H"
00045 #include "Image/fancynorm.H"
00046 #include "CUDA/CudaNorm.H"
00047 #include "CudaDevices.H"
00048 #include "wrap_c_cuda.h"
00049
00050
00051
00052 CudaImage<float> cudaMaxNormalize(const CudaImage<float>& src,
00053 const float mi, const float ma, const MaxNormType normtyp,
00054 int nbiter, const CudaImage<float> *lrexcit)
00055 {
00056
00057
00058 switch(normtyp)
00059 {
00060 case VCXNORM_FANCY:
00061 return cudaMaxNormalizeFancy(src, mi, ma, nbiter, 1.0, lrexcit);
00062 break;
00063 default:
00064 LFATAL("Unhandled normalization type: %d", int(normtyp));
00065 }
00066 return CudaImage<float>();
00067 }
00068
00069
00070
00071 CudaImage<float> cudaMaxNormalizeFancy(const CudaImage<float>& src, const float mi, const float ma,
00072 const int nbiter, const float weakness,
00073 const CudaImage<float>* lrexcit)
00074 {
00075
00076
00077 ASSERT(src.initialized());
00078 ASSERT(src.getMemoryPolicy() != HOST_MEMORY);
00079
00080 MemoryPolicy mp = src.getMemoryPolicy();
00081 int dev = src.getMemoryDevice();
00082 const int w = src.getWidth();
00083 const int h = src.getHeight();
00084 int siz = std::max(w, h);
00085 int maxhw = std::max(0, std::min(w, h) / 2 - 1);
00086
00087
00088 CudaImage<float> maxim = CudaImage<float>(1,1,NO_INIT,mp,dev);
00089 CudaImage<float> result = src;
00090
00091 cudaInplaceRectify(result);
00092
00093
00094 if (mi != 0.0F || ma != 0.0F) cudaInplaceNormalize(result, mi, ma);
00095
00096
00097
00098 float esig = (float(siz) * FANCYESIG) * 0.01F;
00099 float isig = (float(siz) * FANCYISIG) * 0.01F;
00100 CudaImage<float> gExc = cudaGaussian(mp,dev,FANCYCOEX/(esig*sqrt(2.0*M_PI)) * weakness, esig, maxhw);
00101 CudaImage<float> gInh = cudaGaussian(mp,dev,FANCYCOIN/(isig*sqrt(2.0*M_PI)) * weakness, isig, maxhw);
00102
00103 for (int i = 0; i < nbiter; ++i)
00104 {
00105 if (lrexcit)
00106 {
00107
00108 CudaImage<float> tmp = cudaDownSize(result, w / (1<<LRLEVEL), h / (1<<LRLEVEL));
00109 tmp = cudaConvolve(tmp, *lrexcit, CONV_BOUNDARY_ZERO);
00110 tmp += 1.0F;
00111 cudaInplaceClamp(tmp, 1.0F, 1.25F);
00112 tmp = cudaRescale(tmp, w, h);
00113 result *= tmp;
00114 }
00115
00116 CudaImage<float> excit = cudaSepFilter(result, gExc, gExc, CONV_BOUNDARY_CLEAN);
00117 CudaImage<float> inhib = cudaSepFilter(result, gInh, gInh, CONV_BOUNDARY_CLEAN);
00118 excit -= inhib;
00119 cudaGetMax(result, maxim);
00120
00121 result += excit;
00122
00123 result += cudaGetScalar(maxim)*(-0.01F * FANCYINHI) ;
00124
00125 cudaInplaceRectify(result);
00126
00127
00128 }
00129 return result;
00130 }