zxhproj v 2.2
zxhproj
|
00001 00002 /*========================================================================= 00003 00004 Program: ZXH Registration Software 00005 Author: Xiahai Zhuang 00006 Module: $RCSfle: zxhMetricMI.h $ 00007 Language: C++ 00008 Date: $Date: From 2004-01 $ 00009 Version: $Revision: 1.0, 2.0, 2.1 $ 00010 00011 =========================================================================*/ 00012 #ifndef zxhMetricMI_h 00013 #define zxhMetricMI_h 00014 00015 #include "zxh.h" 00016 #include "zxhMetricBase.h" 00017 //#include "zxhTransformFFDBase.h" 00018 00019 #ifndef ZXHMMIComputeKernelFunctionInLocalRegion 00020 #define ZXHMMIComputeKernelFunctionInLocalRegion 1.0 // 0.5 00021 #endif 00022 00035 00036 class zxhMetricMI : 00037 public zxhMetricBase 00038 { 00039 public: 00041 zxhMetricMI(void); 00043 ~zxhMetricMI(void); 00044 00046 virtual std::string GetMetricType() {if(GetIsMMI()) return "_SEMI_MI_" ; else return "_MI_"; } ; 00047 00049 const static int m_stc_iPaddedBins = BSplineHistogramPaddingBins; 00050 00052 virtual zxhMetricBase* Clone(zxhMetricBase*&pRet); 00053 00055 virtual bool PreComputeImageGraylevel(); 00056 00058 virtual bool SetImage(zxhImageData*pImageTest,zxhImageData*pImageRef); 00059 00064 virtual int ComputeHistogramFromPDF4D(zxhlfloat * &afPDF,zxhlfloat&fTotalFreq, 00065 float xfTestFrom,float xfTestTo,float yfTestFrom,float yfTestTo, 00066 float zfTestFrom,float zfTestTo,float tfTestFrom,float tfTestTo); 00068 virtual int ComputeHistogramMaskRegionOnTest( zxhImageData*pMask, zxhlfloat * &afPDF, zxhlfloat &fFreq ); 00070 virtual int ComputeHistogram(zxhlfloat*&afHistogram, zxhlfloat&fTotalFreq); 00072 virtual int GetValue(zxhlfloat& fValue); 00074 virtual zxhlfloat GetMIValue( const zxhlfloat*afHistogram, const zxhlfloat fTotalFreq ); 00076 virtual void AddHistogram(zxhlfloat* afHistogramDes, const zxhlfloat* afHistogramSrc, int times=1 ); 00078 virtual void GetAddHistogram(zxhlfloat* afHistogramDes, zxhlfloat* afHistogramSrc1, zxhlfloat* afHistogramSrc2 ); 00079 00081 void SetNumberOfHistogramBins(int i=32) {m_iNumberOfHistogramBins=i;}; 00082 00084 int GetNumberOfHistogramBins() {return m_iNumberOfHistogramBins;}; 00085 00087 void SetPDFType( enum zxh::TypeHistogramCompute i ) {m_iPDFType=i;}; 00089 void SetMMILambda(float f) {m_fMMILambda=f;}; 00091 float GetMMILambda() {return m_fMMILambda;}; 00093 void SetMMISamplePhysical(float*f) 00094 { 00095 for( int id=0; id<m_iDimension; ++id ) 00096 m_afMMISamplePhysical[id] = f[id]; 00097 } 00098 const float * GetMMISamplePhysical() {return m_afMMISamplePhysical;}; 00100 void SetMMILocalRegionSizeInMM(float f) { m_fMMILocalRegionSizeInMM=f; }; 00102 void SetMMIKernelType( enum zxh::TypeSEMIKernelFunction type ) { m_iMMIKernelType = type;} 00104 void SetMMI( bool b ) 00105 { if(b) m_iComputePDType=zxh::MixtureProbabilityModel; 00106 else m_iComputePDType=zxh::NormalizedMutualInformation;}; 00107 bool GetIsMMI() 00108 { if(m_iComputePDType==zxh::MixtureProbabilityModel) return true; 00109 else return false;}; 00111 void SetMIType( enum zxh::TypeMutualInformation i ) {m_iMIType=i;}; 00113 enum zxh::TypeMutualInformation GetMIType() {return m_iMIType;}; 00114 00116 virtual std::string GetPrintString(); 00117 00119 virtual bool SetMetricFromStream(std::ifstream & ifs); 00120 00122 virtual int ComputeConstanceForGradient() ; 00123 00124 protected: 00125 00127 inline float SEMIKernelDistanceToWeight(float dis ) 00128 { 00129 if( dis*2>=m_fMMILocalRegionSizeInMM ) return 0; 00130 switch( m_iMMIKernelType ) 00131 { 00132 case zxh::SEMIGaussian: return zxh::NGaussian(dis*6.0/m_fMMILocalRegionSizeInMM); break; 00133 case zxh::SEMIBSpline: return zxh::BSpline(dis*4/m_fMMILocalRegionSizeInMM); break; 00134 case zxh::SEMIBSplineZeroOrder: return 1; break; 00135 default: std::cerr<<"error: wrong entry calling semi distance to weight\n"; break; 00136 } 00137 return 0 ; 00138 } 00139 00141 enum zxh::TypeMutualInformation m_iComputePDType; 00143 /* 0~1 : mixture pdf (E(x)=1) 00144 * -1~0: pixel-wise weighted pdf (E(x)=Gaussian) 00145 * 2 : 0.5LMI+0.5GMI 00146 */ 00147 float m_fMMILambda ; 00149 float m_afMMISamplePhysical[ImageDimensionMax] ; 00150 00152 float m_fMMILocalRegionSizeInMM ; 00154 enum zxh::TypeSEMIKernelFunction m_iMMIKernelType ; 00156 zxhImageData m_imgMMItestImage; 00158 zxhImageData m_imgMMIrefImage; 00160 zxhImageDataT<float> m_imgMMIWeighttestref ; 00161 00164 enum zxh::TypeHistogramCompute m_iPDFType; 00165 00167 enum zxh::TypeMutualInformation m_iMIType; 00168 00169 protected: 00171 int m_iNumberOfHistogramBins; 00173 PixelTypeDefault m_aiImageTestGraySpan[2]; 00175 PixelTypeDefault m_aiImageRefGraySpan[2]; 00177 zxhlfloat m_fBinSizeTestImage ; 00179 zxhlfloat m_fBinSizeRefImage ; 00180 00182 zxhlfloat m_fEntropyT; 00184 zxhlfloat m_fEntropyR; 00186 zxhlfloat m_fEntropyJ; 00188 zxhlfloat m_fMutualInformation ; 00190 zxhlfloat m_fNormalisedMutualInformation ; 00192 zxhlfloat *m_afPDFt; 00194 zxhlfloat *m_afPDFr; 00196 zxhlfloat *m_afPDFj; 00197 00199 zxhlfloat *m_afHistogram; 00201 zxhlfloat m_fTotalFreq; 00203 zxhlfloat *m_afC_Ht; 00205 zxhlfloat *m_afC_Hr; 00207 zxhlfloat *m_afC_Hj; 00208 00210 inline int GetLengthOfHistogramDim() 00211 { 00212 if(m_iPDFType==zxh::PDFType_ParzenWindow_BSpline) 00213 return (m_iNumberOfHistogramBins+m_stc_iPaddedBins); 00214 return m_iNumberOfHistogramBins; 00215 } 00217 inline void IntensityBoundBk(float & gray, const short aiImageGraySpan[] ) const 00218 { 00219 gray = MAX(gray, aiImageGraySpan[0]); 00220 gray = gray>(aiImageGraySpan[1]-1)?aiImageGraySpan[0]:gray; 00221 } 00222 inline void IntensityBoundBk(zxhlfloat &gray, const short aiImageGraySpan[] ) const 00223 { 00224 gray = MAX(gray, aiImageGraySpan[0]) ; 00225 gray = gray>(aiImageGraySpan[1]-1)?aiImageGraySpan[0]:gray; 00226 } 00227 inline void IntensityBound(zxhlfloat &gray, const short aiImageGraySpan[] ) const 00228 { 00229 gray = MAX(gray, aiImageGraySpan[0]) ; 00230 gray = MIN(gray,(aiImageGraySpan[1]-1)); 00231 } 00232 void IntensityBound(float &gray, const short aiImageGraySpan[] ) const 00233 { 00234 gray = MAX(gray, aiImageGraySpan[0]) ; 00235 gray = MIN(gray,(aiImageGraySpan[1]-1)); 00236 } 00238 virtual void EvaluateOnJointHistogram(zxhlfloat &fTotalFreq,zxhlfloat *afPDF, int iLengthOfHistogramDim, 00239 zxhlfloat fGrayTest, zxhlfloat fGrayRef , zxhlfloat weight=1); 00240 00241 00243 //virtual void EvaluateOnSumingEntropyDerivative( zxhlfloat &dEntropyRef, zxhlfloat &dEntropyJoint, 00244 // float fGrayTest, float fGrayRef, float fGrayRefForward, float fGrayRefBackward, float length2) ; 00246 //virtual void EvaluateOnPixelForce( zxhlfloat *afForce, int iLengthOfHistogramDim, 00247 // float * gridCoTest, float * gridCoRef ); 00249 virtual void EvaluateOnPixelSource2TargetForce( zxhlfloat *afForce, int iLengthOfHistogramDim, 00250 const float * gridCoTest, const float * gridCoRef ) const ; 00251 00253 inline float BSplinei(int iOrd, float u) 00254 { 00255 zxhlfloat v; 00256 switch(iOrd) 00257 { 00258 case -1: 00259 v=1.0-u; 00260 return v*v*v/6.0; 00261 case 0: 00262 v=u*u; 00263 return 0.5*u*v-v+2.0/3.0; 00264 case 1: 00265 v=u*u; 00266 return (-3.0*u*v+3.0*v+3.0f*u+1)/6.0; 00267 case 2:return u*u*u/6.0; 00268 } 00269 return -1; 00270 } 00272 inline float DerivativeOfBSplinei(int iOrd,float u) 00273 { // -1,0,1,2 00274 zxhlfloat v; 00275 switch(iOrd) 00276 { 00277 case -1: 00278 v=1.0-u; 00279 return v*v*(-0.5); 00280 case 0:return (3.0*u-4.0)*u*0.5; 00281 case 1:return (u+0.5-1.5*u*u); 00282 case 2:return u*u*0.5; 00283 } 00284 return -1; 00285 }; 00287 /* if m_Lambda < 0, then regards using lMI +Lambda*gMI 00288 * **/ 00289 zxhlfloat ComputMMILocalRegionPDFValue( zxhlfloat*afHistL, zxhlfloat&fVl, 00290 const zxhlfloat *afHistG, const zxhlfloat fVg ); 00292 void EvaluateMMIpositionWeightedHistogram(int *roiFrom, int*roiTo, float* WorldCentre, zxhlfloat *afHistogram, zxhlfloat &fTotalFreq, 00293 int *excludeFrom, int*excludeTo ); 00294 void EvaluateMMIpositionWeightedHistogram(int *roiFrom, int*roiTo, float* WorldCentre, zxhlfloat *afHistogram, zxhlfloat &fTotalFreq, 00295 zxhImageData *pExcludeImageMask ); 00296 }; 00297 00298 #endif 00299