EMLocalRegistrationCostFunction.h
Go to the documentation of this file.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 #ifndef _EMLOCALREGISTRATIONCOSTFUNCTION_H_INCLUDED
00026 #define _EMLOCALREGISTRATIONCOSTFUNCTION_H_INCLUDED 1
00027
00028 #include "assert.h"
00029
00030 #include "vtkEMSegment.h"
00031 #include "vtkMultiThreader.h"
00032 #include "vtkImageEMGenericClass.h"
00033 #include "EMLocalInterface.h"
00034 #include "assert.h"
00035
00036
00037
00038
00039
00040
00041
00042
00043 #define EMLOCALREGISTRATION_MAX_PENALITY float(1.0e20)
00044
00045
00046
00047
00048
00049 typedef struct {
00050 double Result;
00051 int Real_VoxelStart[3];
00052 int Boundary_VoxelOffset;
00053
00054 int ROI_NumVoxels;
00055
00056
00057
00058 } EMLocalRegistrationCostFunction_MultiThreadedParameters;
00059
00060
00061
00062
00063
00064 typedef struct {
00065 float** ClassToAtlasRotationMatrix;
00066 float** ClassToAtlasTranslationVector;
00067
00068
00069
00070 int ROI_MinZ;
00071 int ROI_MinY;
00072 int ROI_MinX;
00073
00074 int ROI_MaxZ;
00075 int ROI_MaxY;
00076 int ROI_MaxX;
00077
00078
00079 int Boundary_OffsetY;
00080 int Boundary_OffsetZ;
00081
00082
00083 double MinWeightAtlasCost;
00084
00085 double MinGaussianCost;
00086 } EMLocalRegistrationCostFunction_IterationSpecificVariables;
00087
00088
00089
00090
00091
00092
00093 class EMLocalRegistrationCostFunction_ROI {
00094 public:
00095 char *MAP;
00096 int MinCoord[3];
00097 int MaxCoord[3];
00098 char ClassOutside;
00099 void CreateMAP(int size);
00100
00101 EMLocalRegistrationCostFunction_ROI() { this->MAP = NULL; memset(MaxCoord,0,sizeof(int)*3); memset(MinCoord,0,sizeof(int)*3);ClassOutside = -1;}
00102 ~EMLocalRegistrationCostFunction_ROI() {if (this->MAP) {delete[] this->MAP; this->MAP = NULL;}}
00103 };
00104
00105
00106
00107
00108
00109 class VTK_EMSEGMENT_EXPORT EMLocalRegistrationCostFunction {
00110 public:
00111
00112
00113
00114
00115 void InitializeCostFunction();
00116
00117
00118
00119 float ComputeCostFunction(const double* parameters) const;
00120
00121
00122 void FinalizeCostFunction(double* Parameters, int NumOfFunctionEvaluations);
00123
00124
00125 void CostFunction_Sum_WeightxProbability(int Real_VoxelStart[3], int Boundary_VoxelOffset, int ROI_NumVoxels, double &result);
00126
00127
00128
00129 int GetROI_MinZ(){return this->ParaDepVar->ROI_MinZ;}
00130 int GetROI_MinY(){return this->ParaDepVar->ROI_MinY;}
00131 int GetROI_MinX(){return this->ParaDepVar->ROI_MinX;}
00132 int GetROI_MaxZ(){return this->ParaDepVar->ROI_MaxZ;}
00133 int GetROI_MaxY(){return this->ParaDepVar->ROI_MaxY;}
00134 int GetROI_MaxX(){return this->ParaDepVar->ROI_MaxX;}
00135
00136 int GetBoundary_OffsetY(){return this->ParaDepVar->Boundary_OffsetY;}
00137 int GetBoundary_OffsetZ(){return this->ParaDepVar->Boundary_OffsetZ;}
00138
00139
00140 void SetBoundary(int MinX, int MinY, int MinZ, int MaxX, int MaxY, int MaxZ);
00141 int* GetBoundary_Min() {return this->Boundary_Min;}
00142 int* GetBoundary_Max() {return this->Boundary_Max;}
00143 int GetBoundary_LengthX() {return this->Boundary_LengthX;}
00144 int GetBoundary_LengthY() {return this->Boundary_LengthY;}
00145 int GetBoundary_LengthXYZ() {return this->Boundary_LengthXYZ;}
00146
00147
00148 void SetImage_Length(int LengthX, int LengthY, int LengthZ);
00149 int* GetImage_Length() {return this->Image_Length;}
00150
00151
00152 float GetImage_MidX() {return this->Image_MidX;}
00153 float GetImage_MidY() {return this->Image_MidY;}
00154 float GetImage_MidZ() {return this->Image_MidZ;}
00155
00156
00157 void SetDimensionOfParameter(int inNumberOfParameterSets, int inTwoDFlag, int inRigidFlag);
00158
00159 int GetRigidFlag() {return this->RigidFlag;}
00160 int GetTwoDFlag() {return this->TwoDFlag;}
00161
00162 void SetNumberOfParameterSets(int init) {this->NumberOfParameterSets = init;}
00163 int GetNumberOfParameterSets() {return this->NumberOfParameterSets;}
00164 int GetNumberOfParameterPerSet() {return this->NumberOfParameterPerSet;}
00165 int GetNumberOfTotalParameters() { return this->NumberOfParameterSets*this->NumberOfParameterPerSet; }
00166
00167
00168 void SetROI_Weight(EMLocalRegistrationCostFunction_ROI* init) {this->ROI_Weight = init;}
00169 void SetROI_ProbData(EMLocalRegistrationCostFunction_ROI* init) {this->ROI_ProbData = init;}
00170
00171 void InitializeParameters();
00172
00173 void ClassInvCovariance_Define(classType* ClassListType, void** ClassList);
00174 void ClassInvCovariance_Print();
00175 void ClassInvCovariance_Delete();
00176
00177 void DefineRegistrationParametersForThreadedCostFunction(int ROI_MinX, int ROI_MinY, int ROI_MinZ, int ROI_MaxX, int ROI_MaxY, int ROI_MaxZ) const ;
00178
00179
00180
00181
00182
00183
00184
00185 void SetGlobalToAtlasRotationMatrix(float* init) {this->GlobalToAtlasRotationMatrix = init;}
00186 void SetGlobalToAtlasTranslationVector(float* init) {this->GlobalToAtlasTranslationVector = init;}
00187
00188 void SetSuperClassToAtlasRotationMatrix(float *init) {this->SuperClassToAtlasRotationMatrix = init;}
00189 void SetSuperClassToAtlasTranslationVector(float *init) {this->SuperClassToAtlasTranslationVector = init;}
00190
00191 void SetIndependentSubClassFlag(int* init) {this->IndependentSubClassFlag = init;}
00192 void SetClassSpecificRegistrationFlag(int*init) {this->ClassSpecificRegistrationFlag = init;}
00193
00194 int GetRegistrationType() {return RegistrationType;}
00195 void SetRegistrationType(int init) {this->RegistrationType = init;}
00196
00197 void SetInterpolationType(int init) {this->InterpolationType = init;}
00198 void SetGenerateBackgroundProbability(int init) { this->GenerateBackgroundProbability = init;}
00199 void SetNumberOfTrainingSamples(int vtkNotUsed(init))
00200 {this->NumberOfTrainingSamples = NumberOfTrainingSamples;}
00201
00202 void SetBoundary_NumberOfROIVoxels(int init) {this->Boundary_NumberOfROIVoxels = init;}
00203
00204
00205 void DebugOn(){this->Debug = 1;}
00206 void DebugOff(){this->Debug = 0;}
00207
00208
00209
00210 void SetEMHierarchyParameters(EMLocal_Hierarchical_Class_Parameters init) {this->EMHierarchyParameters.Copy(init);}
00211 void Setweights(float** init) {this->weights = init;}
00212 void SetBoundary_ROIVector(unsigned char * init) {this->Boundary_ROIVector = init;}
00213
00214 void MultiThreadDelete();
00215 void MultiThreadDefine(int DisableFlag);
00216
00217
00218
00219 void SpatialCostFunctionOn();
00220 double* GetSpatialCostFunction() {return this->SpatialCostFunction;}
00221 void SpatialCostFunctionOff();
00222
00223
00224 EMLocalRegistrationCostFunction() {this->InitializeParameters();}
00225 ~EMLocalRegistrationCostFunction();
00226
00227
00228 int GetNumberOfThreads() { return this->NumberOfThreads;}
00229 EMLocalRegistrationCostFunction_MultiThreadedParameters* GetMultiThreadedParameters() {return MultiThreadedParameters;}
00230
00231 int GetProbDataType() { return this->EMHierarchyParameters.ProbDataType;}
00232 void SetProbDataPtr(void** init) {this->ProbDataPtr = init;}
00233 void** GetProbDataPtr() {return this->ProbDataPtr;}
00234 void* GetProbDataPtr(int index) {return this->ProbDataPtr[index];}
00235
00236 int GetDebug() {return this->Debug;}
00237
00238 unsigned char* GetBoundary_ROIVector() {return this->Boundary_ROIVector;}
00239 int GetBoundary_NumberOfROIVoxels() {return this->Boundary_NumberOfROIVoxels;}
00240
00241 int GetNumClasses() const {return this->EMHierarchyParameters.NumClasses; }
00242 int* GetNumChildClasses() {return this->EMHierarchyParameters.NumChildClasses; }
00243 int GetNumTotalTypeCLASS() {return this->EMHierarchyParameters.NumTotalTypeCLASS;}
00244 int* GetProbDataIncY() {return this->EMHierarchyParameters.ProbDataIncY;}
00245 int* GetProbDataIncZ() {return this->EMHierarchyParameters.ProbDataIncZ;}
00246 float* GetProbDataMinusWeight() {return this->EMHierarchyParameters.ProbDataMinusWeight;}
00247 float* GetProbDataWeight () {return this->EMHierarchyParameters.ProbDataWeight;}
00248
00249 EMLocalRegistrationCostFunction_ROI* GetROI_Weight() {return this->ROI_Weight;}
00250 EMLocalRegistrationCostFunction_ROI* GetROI_ProbData() {return this->ROI_ProbData;}
00251
00252 int GetGenerateBackgroundProbability() {return this->GenerateBackgroundProbability;}
00253 int* GetClassSpecificRegistrationFlag() {return this->ClassSpecificRegistrationFlag;}
00254
00255 vtkMultiThreader* GetThreader() { return this->Threader;}
00256
00257 double **GetClassInvCovariance() {return this->ClassInvCovariance;}
00258
00259 double GetMinCost() const {return this->ParaDepVar->MinWeightAtlasCost + this->ParaDepVar->MinGaussianCost;}
00260 double GetMinWeightAtlasCost() {return this->ParaDepVar->MinWeightAtlasCost;}
00261 double GetMinGaussianCost() {return this->ParaDepVar->MinGaussianCost;}
00262
00263 void ResetMinWeightAtlasCost() {this->ParaDepVar->MinWeightAtlasCost = EMLOCALREGISTRATION_MAX_PENALITY; }
00264 void ResetMinCost() {this->ResetMinWeightAtlasCost();this->ParaDepVar->MinGaussianCost =0;}
00265
00266 float** Getweights() { return this->weights; }
00267
00268 int* GetIndependentSubClassFlag() {return this->IndependentSubClassFlag;}
00269 int GetNumberOfTrainingSamples() {return this->NumberOfTrainingSamples;}
00270
00271 int GetInterpolationType() {return this->InterpolationType;}
00272
00273 private:
00274
00275 void ScaleRotationValues(double *FinalParameters);
00276
00277 void **ProbDataPtr;
00278
00279
00280
00281 int Image_Length[3];
00282
00283
00284
00285
00286 float Image_MidX;
00287 float Image_MidY;
00288 float Image_MidZ;
00289
00290
00291
00292 int Boundary_Min[3];
00293 int Boundary_Max[3];
00294
00295 int Boundary_LengthX;
00296 int Boundary_LengthY;
00297 int Boundary_LengthXYZ;
00298
00299
00300 unsigned char *Boundary_ROIVector;
00301
00302 int Boundary_NumberOfROIVoxels;
00303
00304
00305
00306
00307
00308
00309 EMLocalRegistrationCostFunction_ROI* ROI_Weight;
00310 EMLocalRegistrationCostFunction_ROI* ROI_ProbData;
00311
00312
00313
00314
00315
00316
00317 int NumberOfParameterSets;
00318
00319
00320 int NumberOfParameterPerSet;
00321
00322
00323 int TwoDFlag;
00324 int RigidFlag;
00325
00326
00327
00328
00329
00330 float *GlobalToAtlasRotationMatrix;
00331 float *GlobalToAtlasTranslationVector;
00332
00333 float *SuperClassToAtlasRotationMatrix;
00334 float *SuperClassToAtlasTranslationVector;
00335
00336
00337 EMLocalRegistrationCostFunction_IterationSpecificVariables *ParaDepVar;
00338
00339
00340
00341
00342
00343 int *IndependentSubClassFlag;
00344
00345
00346 int *ClassSpecificRegistrationFlag;
00347
00348
00349
00350 int RegistrationType;
00351
00352
00353
00354 int InterpolationType;
00355
00356 int GenerateBackgroundProbability;
00357
00358
00359 int Debug;
00360
00361
00362
00363 double *SpatialCostFunction;
00364
00365
00366
00367
00368
00369
00370
00371
00372 double **ClassInvCovariance;
00373
00374
00375
00376 int NumberOfThreads;
00377 EMLocalRegistrationCostFunction_MultiThreadedParameters* MultiThreadedParameters;
00378 vtkMultiThreader *Threader;
00379
00380
00381
00382 EMLocal_Hierarchical_Class_Parameters EMHierarchyParameters;
00383 float** weights;
00384 int NumberOfTrainingSamples;
00385 };
00386
00387 template <class T>
00388 inline void EMLocalRegistrationCostFunction_DefineROI_ProbDataValues(EMLocalRegistrationCostFunction* self, T** ProbDataPtr) {
00389
00390 assert(((T**) self->GetProbDataPtr()) == ProbDataPtr);
00391
00392 int *Image_Length = self->GetImage_Length();
00393
00394 EMLocalRegistrationCostFunction_ROI* ROI_ProbData = self->GetROI_ProbData();
00395 assert(ROI_ProbData);
00396 if (!ROI_ProbData->MAP) ROI_ProbData->CreateMAP(self->GetImage_Length()[0]*self->GetImage_Length()[1]*self->GetImage_Length()[2]);
00397
00398 ROI_ProbData->MinCoord[0] = Image_Length[0];
00399 ROI_ProbData->MinCoord[1] = Image_Length[1];
00400 ROI_ProbData->MinCoord[2] = Image_Length[2];
00401 ROI_ProbData->MaxCoord[0] = ROI_ProbData->MaxCoord[1] = ROI_ProbData->MaxCoord[2] = 0;
00402 ROI_ProbData->ClassOutside = -1;
00403
00404
00405 char ProbDataFlagX;
00406 int ProbDataFlagY;
00407 int ProbDataFlagZ = 0;
00408
00409 int NumTotalTypeCLASS = self->GetNumTotalTypeCLASS();
00410 int NumClasses = self->GetNumClasses();
00411 int* NumChildClasses = self->GetNumChildClasses();
00412
00413 int ClassStart = self->GetGenerateBackgroundProbability();
00414 T** ProbDataPtrCopy = new T*[NumTotalTypeCLASS];
00415 char *MAP = ROI_ProbData->MAP;
00416
00417 int* ProbDataIncY = self->GetProbDataIncY();
00418 int* ProbDataIncZ = self->GetProbDataIncZ();
00419
00420 int index = 0;
00421 ProbDataPtrCopy[0] = ProbDataPtr[0];
00422 for (int i = ClassStart; i < NumClasses; i++) {
00423 ProbDataFlagX = 0;
00424 for (int k = 0; k < NumChildClasses[i]; k++) {
00425 ProbDataPtrCopy[index] = ProbDataPtr[index];
00426 if (!ProbDataPtrCopy[index] || *ProbDataPtrCopy[index] > 0.0) ProbDataFlagX = 1;
00427 index ++;
00428 }
00429 if (ProbDataFlagX) {
00430 if (ROI_ProbData->ClassOutside > -1) {
00431 ROI_ProbData->ClassOutside = -3;
00432 break;
00433 }
00434 else ROI_ProbData->ClassOutside = i;
00435 }
00436 }
00437
00438 for (int z = 0; z < Image_Length[2] ; z++) {
00439
00440 ProbDataFlagZ = 0;
00441 for (int y = 0; y < Image_Length[1] ; y++) {
00442 ProbDataFlagY = 0;
00443
00444 for (int x = 0; x < Image_Length[0]; x++) {
00445 index = (self->GetGenerateBackgroundProbability() ? NumChildClasses[0] : 0);
00446 *MAP = -1;
00447 for (int i = ClassStart; i < NumClasses; i++) {
00448 ProbDataFlagX = 0;
00449 for (int k = 0; k < NumChildClasses[i]; k++) {
00450 if (!ProbDataPtrCopy[index] || *ProbDataPtrCopy[index] > 0.0) ProbDataFlagX = 1;
00451 index ++;
00452 }
00453 if (ProbDataFlagX) {
00454 if (*MAP > -1) {
00455 *MAP = -1;
00456 break;
00457 } else *MAP = i;
00458 }
00459 }
00460 if (*MAP != ROI_ProbData->ClassOutside) {
00461 ProbDataFlagY = 1;
00462 ProbDataFlagZ = 1;
00463 if (ROI_ProbData->MinCoord[0] > x) {
00464 ROI_ProbData->MinCoord[0] = x;
00465 }
00466 if (ROI_ProbData->MaxCoord[0] < x) {
00467 ROI_ProbData->MaxCoord[0] = x;
00468 }
00469 }
00470 MAP++;
00471 for (int j=0; j < NumTotalTypeCLASS; j++) { if (ProbDataPtrCopy[j]) ProbDataPtrCopy[j] ++; }
00472 }
00473 if (ProbDataFlagY) {
00474 if (ROI_ProbData->MinCoord[1] > y) ROI_ProbData->MinCoord[1] = y;
00475 if (ROI_ProbData->MaxCoord[1] < y) ROI_ProbData->MaxCoord[1] = y;
00476 }
00477 for (int j=0; j < NumTotalTypeCLASS; j++) { if (ProbDataPtrCopy[j]) ProbDataPtrCopy[j] += ProbDataIncY[j]; }
00478 }
00479 if (ProbDataFlagZ) {
00480 if (ROI_ProbData->MinCoord[2] > z) ROI_ProbData->MinCoord[2] = z;
00481 ROI_ProbData->MaxCoord[2] = z;
00482 }
00483 for (int j=0; j < NumTotalTypeCLASS; j++) { if (ProbDataPtrCopy[j]) ProbDataPtrCopy[j] += ProbDataIncZ[j]; }
00484 }
00485
00486
00487
00488 delete[] ProbDataPtrCopy;
00489 }
00490
00491
00492
00493
00494
00495
00496
00497 #endif