zxhproj v 2.2
zxhproj

zxhRegistration.h

00001 /*=========================================================================
00002 
00003   Program:   ZXH Registration Software
00004   Author:    Xiahai Zhuang
00005   Module:    $RCSfle: zxhRegistration.h    $
00006   Language:  C++
00007   Date:      $Date: From  2004-01 $
00008   Version:   $Revision: 1.0, 2.0 $
00009 
00010 =========================================================================*/
00011 
00012 #ifndef zxhRegistration_h
00013 #define zxhRegistration_h
00014 
00015 #include <string>
00016 #include <iostream>
00017 #include <fstream>
00018 #include <stdio.h>
00019 #include <stdlib.h>
00020 #include "zxhImageData.h"
00021 #include "zxhImageParRec.h"
00022 #include "zxhImageGipl.h"
00023 #include "zxhTransformBase.h"
00024 #include "zxhTransformAffine.h"
00025 #include "zxhTransformFFD.h"
00026 #include "zxhTransformFields.h"
00027 #include "zxhImageModelingLinear.h"
00028 #include "zxhOptimizerGradient.h"
00029 #include "zxhMetricFFDBase.h"
00030 #include "zxhTransformLocalAffines.h"
00031 #include "zxhRegistrationStruct.h"
00032 
00033 class zxhOptimizerGradient;
00034 
00035 namespace zxh
00036 {
00037 
00039 void RegHELP() ;
00041 void RegArg( int argc, char* argv[], zxhRegistrationStruct& StructRegistration,
00042             int &dimension );
00044 void RegArgSetRoiForDim2Reg( zxhRegistrationStruct& StructRegistration, int &dimension ) ;
00046 void RegNewOptimiser( zxhRegistrationStruct& StructRegistration, zxhOptimizerGradient* &optimizer ) ;
00048 void RegOptimiseArg( int argc, char*argv[], zxhRegistrationStruct& StructRegistration );
00050 int RegMetricArg( int argc, char*argv[], zxhRegistrationStruct& StructRegistration, zxhMetricBase*&pMetric ) ;
00051 
00052 //void RegFFDArg( int argc, char* argv[],zxhRegistrationStruct& StructRegistration, zxhImageModelingLinearT<float,float> &ModelNVRef ) ;
00053 
00054 
00055 void SetDilateLocalRegionForIRBReg( zxhTransformLocalAffines* pTransform, float* afLength, bool Dilation ) ;
00057 void RegAffineArg( int argc, char*argv[],zxhRegistrationStruct& StructRegistration  );
00058 
00059 //void RegLocalAffinesArg( int argc, char*argv[], zxhRegistrationStruct& StructRegistration , zxhOptimizerGradient* &optimizer );
00060 
00062 void RegLocalAffinesBackwardTransformAllTestImages( zxhRegistrationStruct& StructRegistration, zxhTransformBase* &pInverseTransformTestImages ) ;
00064 void RegSet2DInflucence( int iTransType, int dimension,
00065                         zxhTransformBase* & pTransform,
00066                         zxhTransformBase* & pDifferential,
00067                         zxhTransformBase* & pInfluence );
00069 void SetInfluence2D( zxhTransformAffine * aff );
00071 void SetRegOptimizer(zxhOptimizerGradient* optimizer,zxhRegistrationStruct& StructRegistration);
00073 void RegSetTransformInfo( zxhRegistrationStruct& StructRegistration );
00075 void RegPreSetGlobalAffine( zxhRegistrationStruct& StructRegistration ) ;
00077 void InactivateFFDBorders( zxhTransformFFD* pDifferential );
00079 void RegSetDifferentialInfo( zxhRegistrationStruct& StructRegistration ) ; 
00081 bool RegComputeSimilarity(int argc, char * argv[]);
00083 int reg_option(int argc, char * argv[]);
00085 void SaveRegistrationMiddleResults( int argc, char* argv[], zxhRegistrationStruct&StructRegistration );
00087 void SaveRegistrationResult( zxhOptimizerBase*pOptimiser, zxhRegistrationStruct & StructRegistration,
00088                                 bool bComputeInverse, float fInvJacWeighting ); 
00090 void SaveTransform(zxhTransformBase*pTransform,const char*pFilename);
00092 void SaveString2File(std::string str2save,char*pfilename);
00094 void SaveString2File(std::string str2save,std::string filename);
00098 void BackwardTransformImageByList(zxhImageData*pSave, zxhImageData*pImg,
00099                                   zxhTransformBase*pList[],int nTransform,int interpolation,
00100                                   bool bBackground=true, PixelTypeDefault background=ZXH_Background);
00102 void ForwardNearestTransformImageByList(zxhImageModelingBase*pTest,zxhImageModelingBase*pRef,zxhImageData*pSave,
00103                                         zxhTransformBase*pList[],int nTransform,int empty);
00105 void ForwardBSplineTransformImageByList(zxhImageData*pTest,zxhImageData*pSave,
00106                                         zxhTransformBase*pList[],int nTransform,int empty);
00108 void FillEmptyVoxelByMean3D(zxhImageData*pImage,int empyvoxel,int nSearchSize, int background=0);
00110 bool ReadTransformFromFile(zxhTransformBase*&pTransform,std::string file);
00111 
00113 bool NewTransformFromTypeName(zxhTransformBase*&pTransform,std::string type);
00115 bool NewMetricFromTypeName(zxhMetricBase*&pMetric,std::string type);
00116 
00118 zxhTransformBase *  ConcatenateTransformsByExponent( zxhTransformBase*pTrans, int eK, const zxhImageInfo*pImageInfo) ;
00120 zxhTransformBase *  ConcatenateTransforms( zxhTransformBase* pTransList[], int nTrans, int dimension,
00121                                     const float * spacing, const float * extend,
00122                                     const float  *roiWorldFrom, const float  *roiWorldTo, const zxhImageInfo*pImgInfo ) ;
00124 zxhTransformBase *  RegriddingLocalAffines( zxhTransformBase* pTjAfter, zxhTransformLocalAffines* pLocalAffinesFirst,
00125                     zxhImageData*pTestImageTemplate,
00126                     float *roiWorldFrom=0, float *roiWorldTo=0 ) ;
00127 ;
00130 template <class PixelType>
00131 bool SetImageIntensityBetween(zxhImageDataT<PixelType>*pIn,
00132                               PixelType RangeFrom,PixelType RangeTo,
00133                               PixelType value, int op, PixelType background=ZXH_Background)
00134 {
00135     const int* size = pIn->GetImageSize();
00136     for( int it=0; it<size[3]; ++it )
00137     for(int iz=0; iz<size[2]; ++iz)
00138     for(int iy=0; iy<size[1]; ++iy)
00139     for(int ix=0; ix<size[0]; ++ix)
00140     {
00141         PixelType inten = pIn->GetPixelGreyscale( ix,iy,iz,it ) ;
00142         switch(op)
00143         {
00144         case 11:  //va
00145             if( inten>= RangeFrom && inten <= RangeTo )
00146                 pIn->SetPixelByGreyscale( ix,iy,iz,it, inten+value ) ;
00147             break;
00148         case 12:   //vs
00149             if( inten>= RangeFrom && inten <= RangeTo )
00150                 pIn->SetPixelByGreyscale( ix,iy,iz,it, value ) ;
00151             break;
00152         case 13:  //VS
00153             if( inten>= RangeFrom && inten <= RangeTo )
00154                 pIn->SetPixelByGreyscale( ix,iy,iz,it, value ) ;
00155             else pIn->SetPixelByGreyscale( ix,iy,iz,it, background ) ;
00156             break;
00157         default:break;
00158         }
00159     }
00160     return true;
00161 }
00162 
00165 template <class PixelType,class MaskType>
00166 bool MaskTestImageByTrans3D(zxhImageDataT<PixelType>*pIn, zxhImageDataT<MaskType>*pMask,
00167                           zxhTransformBase*pTrans,float value,int compare, float background,
00168                           int nRanges, float fRangeFrom[], float fRangeTo[])
00169 {
00170     int* size   = pIn->GetImageSize();
00171     zxhImageModelingLinearT<MaskType,float> model;
00172     model.SetImage( pMask );
00173     bool inside=false;
00174     bool samedimension= ImageNvoxelDimensionSame( pIn, pMask );
00175     PixelType Pixel;
00176     for(int iz=0; iz<size[2]; ++iz)
00177     for(int iy=0; iy<size[1]; ++iy)
00178     for(int ix=0; ix<size[0]; ++ix)
00179     {
00180         inside=false;
00181         if( nRanges== 0 )
00182             inside = true ;
00183         else
00184         {
00185             Pixel = pIn->GetPixelGreyscale(ix,iy,iz);
00186             for( int i=0; i<nRanges; ++i )
00187                 if( Pixel>=fRangeFrom[i] && Pixel<=fRangeTo[i] )
00188                 {inside = true;break;}
00189         }
00190         if( inside == false )
00191             continue ;
00192         float gray ;
00193         if( pTrans==0 && samedimension )
00194         {
00195             gray = pMask->GetPixelGreyscale(ix,iy,iz) ;
00196         }else
00197         {
00198             float fco [ImageDimensionMax]   = { float(ix), float(iy), float(iz), 0} ;
00199             float fco2[ImageDimensionMax]   = {0,0,0,0} ;
00200             pIn->AdjustToWorldical(fco);
00201             if(pTrans!=0)
00202                 pTrans->TransformWorldToWorld(fco,fco2);
00203             else
00204             {fco2[0]=fco[0]; fco2[1]=fco[1]; fco2[2]=fco[2]; fco2[3]=fco[3];}
00205 
00206             pMask->WorldToImage(fco2);
00207 
00208             gray = model.GetPixelFloatValueWithCheckBk(background, fco2[0],fco2[1],fco2[2] );
00209         }
00210 
00211         switch(compare)
00212         {
00213         case 0:  if( gray == value ) pIn->SetPixelByGreyscale(ix,iy,iz,0, PixelType(background) ); //=
00214             break;
00215         case 1:  if( gray >= value ) pIn->SetPixelByGreyscale(ix,iy,iz,0, PixelType(background) ); //>=
00216             break;
00217         case -1: if( gray <= value ) pIn->SetPixelByGreyscale(ix,iy,iz,0, PixelType(background) ); //<=
00218             break;
00219         case 2:  if( gray != value ) pIn->SetPixelByGreyscale(ix,iy,iz,0, PixelType(background) ); 
00220         default:break;
00221         }
00222 
00223     }
00224     return true;
00225 }
00228 template <class PixelType,class MaskType>
00229 bool MaskRefImageByTrans3D(zxhImageDataT<PixelType>*pIn, zxhImageDataT<MaskType>*pMask,
00230                           zxhTransformBase*pTrans,float value,int compare, float background )
00231 {
00232     zxhImageData RefMask ;
00233     RefMask.NewImage( pIn->GetDimension(), pIn->GetImageSize(), pIn->GetImageSpacing() ); CopyImageInfo( pIn, &RefMask ) ;
00234     int * size  = pMask->GetImageSize();
00235     for(int iz=0;iz<size[2];++iz)
00236     for(int iy=0;iy<size[1];++iy)
00237     for(int ix=0;ix<size[0];++ix)
00238     {
00239         PixelType gray = pMask->GetPixelGreyscale( ix, iy, iz, 0 ) ;
00240         bool inside = false;
00241         switch(compare)
00242         {
00243         case 0:if( gray == value ) inside = true ;//=
00244             break;
00245         case 1:if( gray >= value ) inside = true ;//>=
00246             break;
00247         case -1:if(gray <= value ) inside = true ;//<=
00248             break;
00249         default:break;
00250         }
00251         if ( inside )
00252         {
00253             float fco [ImageDimensionMax]   = {float(ix), float(iy), float(iz), 0} ;
00254             float fco2[ImageDimensionMax]   = {0,0,0,0} ;
00255             pMask->AdjustToWorldical(fco);
00256             if(pTrans!=0)
00257                 pTrans->TransformWorldToWorld(fco,fco2);
00258             else
00259                 {fco2[0]=fco[0]; fco2[1]=fco[1]; fco2[2]=fco[2]; fco2[3]=fco[3];}
00260             pIn->WorldToImage( &fco2[0] ) ;
00261             for ( int iix = int(fco2[0]) ; iix <= int(fco2[0])+1 ; ++iix )
00262             for ( int iiy = int(fco2[1]) ; iiy <= int(fco2[1])+1 ; ++iiy )
00263             for ( int iiz = int(fco2[2]) ; iiz <= int(fco2[2])+1 ; ++iiz )
00264             {
00265                 if ( pIn->InsideImage( iix, iiy, iiz, 0 ) )
00266                     RefMask.SetPixelByGreyscale( iix, iiy, iiz, 0, 111 );
00267             }
00268         }
00269     }
00270     size    = pIn->GetImageSize();
00271     for(int iz=0;iz<size[2];++iz)
00272     for(int iy=0;iy<size[1];++iy)
00273     for(int ix=0;ix<size[0];++ix)
00274     {
00275         if ( RefMask.GetPixelGreyscale ( ix, iy, iz, 0 ) == 111 )
00276             pIn->SetPixelByGreyscale ( ix, iy, iz, 0, PixelType(background) );
00277     }
00278     return true;
00279 }
00282 bool MaskFFDCtrPntStatus( zxhTransformFFDBase* pInfl, //influence of the optimiser for the FFD control points
00283                           zxhTransformBase* pTrans, // transform on the pInfl control points, could be null which means the mask is on the test image spacing before transformation
00284                           zxhImageData * pMask,
00285                           bool bActivate=false); // also to activate the control points within the mask
00286 
00288 float ComputeAverageDisplacement(
00289                 zxhImageData* pTest, int *iRoiTestFrom, int *iRoiTestTo, zxhImageData* pMaskTest,
00290                 zxhTransformBase** pTransformList, int nTransforms,
00291                 zxhImageData* pMaskRef );
00292 
00293 
00295 float GetMaskImageVolumeByResample(zxhImageData*pImg, float spacex, float spacey, float spacez );
00296 }// end namespace zxh
00297 #endif
 All Classes Namespaces Functions Variables Typedefs