vtkOtsuImageData.cxx

Go to the documentation of this file.
00001 #include "vtkOtsuImageData.h"
00002 
00003          vtkOtsuImageData::vtkOtsuImageData(){
00004                  volume = NULL;
00005                  P = NULL;
00006 
00007         }
00008 
00009          vtkOtsuImageData::~vtkOtsuImageData(){
00010                 
00011         }
00012 
00013         int vtkOtsuImageData::calculateOptimalThreshold(vtkImageData* voi){
00014                 
00015                 int extent[6];
00016                 int static TAM = 10000;
00017                 
00018                 
00019                 this->volume = voi; //VOI
00020                 this->volume->GetExtent(extent);
00021 
00022                 printf("otsu: extent = x (%d %d) y (%d %d) z(%d %d)\n",extent[0],extent[1],extent[2],extent[3],extent[4],extent[5]);
00023 
00024                                         
00025                 double valor = 0.0;
00026                 int index = 0;
00027                 int T = 0;
00028                 int max=0;
00029                 int voxelCount = 0;
00030         
00031                 //1. Inicializacion
00032                 this->H = new int[TAM];
00033                 this->P = new double[TAM];
00034                 this->SI = new double[TAM];
00035                 for (index = 0; index <TAM; index++){
00036                         this->H[index] = 0;
00037                         this->P[index] = 0.0;   //probabilidad acumulada
00038                         this->SI[index] = 0.0;
00039                 }
00040                 
00041                 //2. Calcular el Histograma
00042                 
00043                 int i, j, k;
00044                 for(i=extent[0];i<=extent[1];i++){
00045                         for(j=extent[2];j<=extent[3];j++){
00046                                 for(k=extent[4];k<=extent[5];k++){
00047                                         
00048                                                                                 
00049                                         valor = this->volume->GetScalarComponentAsDouble(i,j,k,0);
00050                                         if (valor > max){
00051                                                 max = (int)valor;
00052                                         }
00053                                         
00054                                         index  = (int)valor;
00055                                         this->H[index]++;
00056                                         voxelCount++;
00057                                         
00058                                 }
00059                         }
00060                 }
00061 
00062                 printf("otsu: voxelCount = %d\n",voxelCount);
00063                 
00064                 //3. Normalizacion de la funcion P
00065                 for(index=0;index<=max;index++){
00066                         this->P[index] = (double)this->H[index] / (double)voxelCount;
00067                         //printf("H[%d] = %d\n",index, H[index]);
00068                 }
00069 
00070 
00071 
00072 
00073                 //FILE *logger = NULL;
00074         
00075                 //if ((logger = freopen("c:\\Diego\\Andes\\Tesis\\ITK\\FM_3D\\bin\\Debug\\thotsu.txt","w", stdout)) == NULL)
00076                         //exit(-1);
00077 
00078                 //printf("THRESHOLD; VARIANZA INTERCLASE\n");
00079 
00080 
00081                 //4. Recorrer los diferentes niveles de intensidad
00082 
00083                 for(T=0;T<=max;T++){
00084 
00085                         //printf("T= %d, ",T);
00086                         
00087                         //4.1 Calcular las probabilidades acumuladas de las clases A y B
00088                         double Q1 = 0.0;
00089                         double Q2 = 0.0;
00090                         for(index=0;index<=max;index++){
00091                                 if (index < T){
00092                                         Q1 = Q1 + this->P[index];
00093                                 }
00094                                 else{
00095                                         Q2 = Q2 + this->P[index];
00096                                 }
00097                         }
00098 
00099                         //4.2 Calcular las medias de las clases A y B
00100                         double U1 = 0.0;
00101                         double U2 = 0.0;
00102                         for (index=0;index<=max;index++){
00103                                 if (index < T){
00104                                         U1 = U1 + ((double)(index * this->P[index])/(double)Q1);
00105                                 }
00106                                 else{
00107                                         U2 = U2 + ((double)(index * this->P[index])/(double)Q2);
00108                                 }
00109                         }
00110 
00111                         
00112                         //4.3 Calcular las varianzas individuales
00113                         double S1 = 0.0;
00114                         double S2 = 0.0;
00115                         for (index=0;index<=max;index++){
00116                                 if (index < T){
00117                                         S1 = S1 + ((index - U1)*(index - U1)*(this->P[index]/(double)Q1));
00118                                 }
00119                                 else{
00120                                         S2 = S2 + ((index - U2)*(index - U2)*(this->P[index]/(double)Q2));
00121                                 }
00122                         }
00123 
00124                         //4.4 Calcular SI: La varianza intraclases y seleccionar el valor minimo
00125                         this->SI[T] = Q1*S1 + Q2*S2;
00126                         //printf("VI = %.2f\n",SI[T]);
00127                 }
00128                 
00129             int optimalThreshold        = 0;
00130                 double optimalCriteria  = VTK_LARGE_FLOAT;
00131 
00132         
00133 
00134                 for (int index2=0;index2<=max;index2++){
00135                         
00136                         if (SI[index2] < optimalCriteria){
00137                                 optimalCriteria = SI[index2];
00138                                 optimalThreshold = index2;
00139                                 //printf("ENTRO %d %d\n",index2,optimalThreshold);
00140                         }
00141                         //printf("SI[%d] = %.2f, optimo = %d\n",index2,SI[index2],optimalThreshold);
00142                 }
00143                 //fclose(logger);
00144                 //logger = NULL;
00145 
00146                 printf(">>>%d<<<\n", optimalThreshold);
00147         
00148                 return optimalThreshold;
00149         }
00150 
00151 
00152 

Generated on 18 Mar 2010 for creaMaracasVisu_lib by  doxygen 1.6.1