vtkOtsu.cxx
Go to the documentation of this file.00001 #include "vtkOtsu.h"
00002
00003 vtkOtsu::vtkOtsu(){
00004 volume = NULL;
00005 region = NULL;
00006
00007 }
00008
00009 vtkOtsu::~vtkOtsu(){
00010
00011 }
00012
00013
00014
00015
00016 double vtkOtsu::calculateOptimalThreshold(vtkImageData* volume, vtkImageData *region){
00017
00018 this->volume = volume;
00019 this->region = region;
00020
00021 double valor = 0.0;
00022 double media = 0.0;
00023 double valorEnRegion = 0.0;
00024
00025
00026 int extent[6];
00027
00028 int index = 0;
00029
00030
00031 this->volume->GetExtent(extent);
00032 this->max=0.0;
00033 this->sigmaT = 0.0;
00034
00035
00036 this->voxelCount = 0;
00037
00038
00039
00040
00041 this->histogram = new int[1000];
00042 this->criterias = new double[1000];
00043 for (index = 0; index <1000; index++){
00044 this->histogram[index] = 0;
00045 this->criterias[index] = 0.0;
00046 }
00047
00048
00049
00050 int i, j, k;
00051 for(i=extent[0];i<=extent[1];i++){
00052 for(j=extent[2];j<=extent[3];j++){
00053 for(k=extent[4];k<=extent[5];k++){
00054 valorEnRegion = this->region->GetScalarComponentAsDouble(i,j,k,0);
00055 if (valorEnRegion == 0.0){
00056 valor = this->volume->GetScalarComponentAsDouble(i,j,k,0);
00057 if (valor > this->max){
00058 this->max = valor;
00059 }
00060 index = (int)valor;
00061 this->histogram[index]++;
00062 media += valor;
00063 this->voxelCount++;
00064 }
00065 }
00066 }
00067 }
00068
00069 media = (double)media/(double)this->voxelCount;
00070
00071
00072 for(index=0;index<=max;index++){
00073 this->sigmaT += (index - media)*(index - media)* this->histogram[index] / (double)this->voxelCount;
00074 }
00075
00076
00077
00078 int optimalThreshold = 0;
00079 double optimalCriteria = 0.0;
00080
00081 for (index=0;index<=max;index++){
00082 criterias[index] = this->getCriteria(index);
00083 if (criterias[index] > optimalCriteria){
00084 optimalCriteria = criterias[index];
00085 optimalThreshold = index;
00086 }
00087 }
00088 return optimalThreshold;
00089
00090
00091 }
00092
00093
00094 double vtkOtsu::getCriteria(double threshold){
00095
00096 int extent[6];
00097 int i,j,k;
00098 double valor = 0.0;
00099 double valorEnRegion = 0.0;
00100
00101 this->volume->GetExtent(extent);
00102
00103
00104 double mediaA=0.0;
00105 double wA = 0.0;
00106 int countA = 0;
00107
00108 double mediaB=0.0;
00109 double wB = 0.0;
00110 int countB = 0;
00111
00112 for(i=extent[0];i<=extent[1];i++){
00113 for(j=extent[2];j<=extent[3];j++){
00114 for(k=extent[4];k<=extent[5];k++){
00115 valorEnRegion = this->region->GetScalarComponentAsDouble(i,j,k,0);
00116 if (valorEnRegion == 0.0){
00117 valor = this->volume->GetScalarComponentAsDouble(i,j,k,0);
00118 if (valor < threshold){
00119 mediaA += valor;
00120 countA++;
00121
00122 }
00123 else{
00124 mediaB += valor;
00125 countB++;
00126 }
00127 }
00128 }
00129 }
00130 }
00131
00132 mediaA = (double)mediaA / (double)countA;
00133 wA = (double)countA / (double)this->voxelCount;
00134
00135 mediaB = (double)mediaB / (double)countB;
00136 wB = (double)countB / (double)this->voxelCount;
00137
00138
00139
00140
00141 double sigmaA=0.0;
00142 double sigmaB=0.0;
00143
00144 int index = 0;
00145
00146 for(index=0;index<=threshold;index++){
00147 sigmaA += (index - mediaA)*(index - mediaA)*this->histogram[index]/(double)countA;
00148 }
00149
00150
00151 for(index=threshold;index<=this->max;index++){
00152 sigmaB += (index - mediaB)*(index - mediaB)*this->histogram[index]/(double)countB;
00153 }
00154
00155
00156
00157 double sigmaAB = wA*wB*(mediaA-mediaB)*(mediaA-mediaB);
00158 double criteria = (double)sigmaAB/(double)sigmaT;
00159
00160 return criteria;
00161
00162 }