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