itkFM3D.cxx
Go to the documentation of this file.00001
00002
00003
00004 #include "itkFM3D.h"
00005
00006
00007 #include "itkImage.h"
00008 #include "itkImageFileReader.h"
00009 #include "itkImageFileWriter.h"
00010 #include "itkSigmoidImageFilter.h"
00011 #include "itkFastMarchingImageFilter.h"
00012 #include "itkCurvatureAnisotropicDiffusionImageFilter.h"
00013 #include "itkBinaryThresholdImageFilter.h"
00014 #include "itkVTKImageToImageFilter.h"
00015 #include "itkImageToVTKImageFilter.h"
00016 #include "itkVTKImageIO.h"
00017
00018 #include "vtkOtsuImageData.h"
00019 #include "vtkImageCast.h"
00020 #include "vtkMetaImageWriter.h"
00021
00022 #include "vtkDataSetWriter.h"
00023
00024
00025 #include <wx/arrstr.h>
00026 #include <wx/process.h>
00027
00028
00029 itkFM3D::itkFM3D(){
00030 usefilter = 1;
00031 alpha = 0.5;
00032 beta = 128.0;
00033 stopTime = 10;
00034 x = 0;
00035 y = 0;
00036 z = 0;
00037 }
00038
00039 itkFM3D::~itkFM3D(){
00040
00041 }
00042
00043 void itkFM3D::AddSeed(int x, int y, int z){
00044 this->x = x;
00045 this->y = y;
00046 this->z = z;
00047 }
00048
00049 void itkFM3D::SetAlpha(double alpha){
00050
00051 this->alpha = alpha;
00052 }
00053
00054 void itkFM3D::SetBeta(double beta){
00055 this->beta = beta;
00056 }
00057
00058
00059 void itkFM3D::SetStopTime(double stopTime){
00060 this->stopTime = stopTime;
00061 }
00062
00063 void itkFM3D::CurvatureAnisotropicFiltertOn(){
00064 usefilter = 1;
00065 }
00066
00067 void itkFM3D::CurvatureAnisotropicFilterOff(){
00068 usefilter = 0;
00069 }
00070
00071
00072 vtkImageData* itkFM3D::segment(vtkImageData *volume){
00073
00074 volume->SetSpacing(1,1,1);
00075
00076
00077 double spc[3];
00078 volume->GetSpacing(spc);
00079
00080
00081 double xx = x;
00082 double yy = y;
00083 double zz = z;
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099 typedef double InternalPixelType;
00100 const unsigned int DIMENSION_IMAGEN = 3;
00101 typedef itk::Image< InternalPixelType, DIMENSION_IMAGEN > InternalImageType;
00102
00103
00104
00105
00106
00107
00108
00109 vtkImageCast *ic = vtkImageCast::New();
00110 ic->SetInput(volume);
00111 ic->SetOutputScalarTypeToDouble();
00112 ic->Update();
00113 typedef itk::VTKImageToImageFilter<InternalImageType> VtkToItkFilterType;
00114 VtkToItkFilterType::Pointer toItk = VtkToItkFilterType::New();
00115 toItk->SetInput( ic->GetOutput() );
00116 toItk->Update();
00117 const InternalImageType* itkImageData = toItk->GetOutput();
00118 itkImageData->Register();
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139 typedef itk::CurvatureAnisotropicDiffusionImageFilter<
00140 InternalImageType,
00141 InternalImageType > SmoothingFilterType;
00142
00143 SmoothingFilterType::Pointer filtroAnisotropico = SmoothingFilterType::New();
00144
00145
00146
00147
00148
00149 typedef itk::SigmoidImageFilter<
00150 InternalImageType,
00151 InternalImageType > SigmoidFilterType;
00152
00153 SigmoidFilterType::Pointer sigmoid = SigmoidFilterType::New();
00154
00155 sigmoid->SetOutputMinimum( 0.0 );
00156 sigmoid->SetOutputMaximum( 255 );
00157 sigmoid->SetAlpha(this->alpha);
00158 sigmoid->SetBeta(this->beta);
00159
00160
00161
00162
00163
00164 typedef itk::FastMarchingImageFilter< InternalImageType,
00165 InternalImageType > FastMarchingFilterType;
00166 FastMarchingFilterType::Pointer fastMarching = FastMarchingFilterType::New();
00167
00168
00169
00170 typedef itk::BinaryThresholdImageFilter<InternalImageType, InternalImageType > ThresholdingFilterType;
00171 ThresholdingFilterType::Pointer thresholdFilter = ThresholdingFilterType::New();
00172
00173
00174
00175
00176
00177 typedef FastMarchingFilterType::NodeContainer NodeContainer;
00178 typedef FastMarchingFilterType::NodeType NodeType;
00179 NodeContainer::Pointer seeds = NodeContainer::New();
00180
00181 InternalImageType::IndexType seedPosition;
00182 seedPosition[0] = this->x;
00183 seedPosition[1] = this->y;
00184 seedPosition[2] = this->z;
00185
00186 NodeType node;
00187 const double seedValue = 0.0;
00188
00189 node.SetValue( seedValue );
00190 node.SetIndex( seedPosition );
00191
00192 seeds->Initialize();
00193 seeds->InsertElement( 0, node );
00194
00195
00196
00197 fastMarching->SetTrialPoints( seeds );
00198 fastMarching->SetOutputSize(itkImageData->GetBufferedRegion().GetSize() );
00199 fastMarching->SetStoppingValue(this->stopTime);
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238 if (usefilter == 1){
00239
00240
00241 sigmoid->SetInput(itkImageData);
00242 }
00243 else {
00244 sigmoid->SetInput(itkImageData);
00245 }
00246
00247
00248 fastMarching->SetInput(sigmoid->GetOutput());
00249
00250
00251
00252
00253
00254
00255
00256
00257
00258
00259
00260
00261
00262
00263
00264 typedef itk::ImageToVTKImageFilter<InternalImageType> ItkToVtkFilterType;
00265
00266
00267
00268
00269
00270
00271
00272
00273
00274
00275
00276
00277
00278
00279
00280
00281
00282
00283
00284
00285
00286
00287
00288
00289
00290
00291
00292
00293 try
00294 {
00295 fastMarching->Update();
00296
00297
00298
00299 printf("Writers (ruido,sigmoide, mapatiempos) procesados \n");
00300 }
00301 catch( itk::ExceptionObject & excep )
00302 {
00303 std::cerr << "Ocurrio un problema" << std::endl;
00304 std::cerr << excep << std::endl;
00305 }
00306
00307
00308
00309
00310
00311
00312
00313
00314 printf("Procesando el mapa de tiempos....");
00315
00316
00317
00318
00319
00320
00321
00322
00323
00324 InternalImageType::Pointer mapa = fastMarching->GetOutput();
00325
00326 typedef itk::ImageRegionConstIterator< InternalImageType > ConstIteratorType;
00327
00328 ConstIteratorType constIterator( mapa, mapa->GetLargestPossibleRegion() );
00329
00330
00331
00332 double valor = 0.0;
00333 int indice = 0;
00334
00335 int histoTiempos[1000];
00336 int histoAcumulado[1000];
00337
00338 for (indice = 0; indice <= 1000; indice++){
00339 histoTiempos[indice] = 0;
00340 histoAcumulado[indice] = 0;
00341 }
00342
00343
00344
00345 for (constIterator.GoToBegin(); !constIterator.IsAtEnd(); ++ constIterator){
00346 valor = constIterator.Get();
00347 if (valor <= 10){
00348 indice = (int)(valor * 100);
00349 histoTiempos[indice]++;
00350
00351 }
00352 }
00353
00354
00355 for (indice = 1 ; indice <= 1000; indice++){
00356 histoAcumulado[indice]= histoAcumulado[indice-1]+ histoTiempos[indice];
00357 }
00358
00359
00360 int frecuenciaMinima = 1000000000;
00361 int indiceBuscado = -1;
00362 for (indice = 0; indice <= 1000; indice++){
00363 if (histoTiempos[indice] < frecuenciaMinima){
00364 frecuenciaMinima = histoTiempos[indice];
00365 indiceBuscado = indice;
00366 }
00367
00368 }
00369
00370
00371 double valorThreshold = (double)indiceBuscado / 100.0;
00372
00373 printf("VALOR DE SEGMENTACION: %.2f\n\n",valorThreshold);
00374
00375
00376
00377
00378
00379
00380
00381
00382
00383
00384 printf("Mapa de tiempos OK");
00385
00386
00387 thresholdFilter->SetOutsideValue( 0 );
00388 thresholdFilter->SetInsideValue( 255 );
00389 thresholdFilter->SetLowerThreshold(0);
00390 thresholdFilter->SetUpperThreshold(valorThreshold);
00391
00392 thresholdFilter->SetInput(fastMarching->GetOutput());
00393
00394
00395 try{
00396 thresholdFilter->Update();
00397
00398 }
00399 catch( itk::ExceptionObject & excep )
00400 {
00401 std::cerr << "Ocurrio un problema" << std::endl;
00402 std::cerr << excep << std::endl;
00403 }
00404
00405 ItkToVtkFilterType::Pointer resultado = ItkToVtkFilterType::New();
00406 resultado->SetInput(thresholdFilter->GetOutput());
00407 vtkImageData* salida = resultado->GetOutput();
00408 resultado->Register();
00409 salida->Update();
00410 salida->ReleaseDataFlagOff();
00411
00412
00413
00414
00415 return salida;
00416 }