#include <itkFM3D.h>
Public Member Functions | |
itkFM3D () | |
virtual | ~itkFM3D () |
void | AddSeed (int x, int y, int z) |
void | SetAlpha (double alpha) |
void | SetBeta (double beta) |
void | SetStopTime (double stopTime) |
void | CurvatureAnisotropicFiltertOn () |
void | CurvatureAnisotropicFilterOff () |
vtkImageData * | segment (vtkImageData *volume) |
double | GetEstimatedOtsuTreshold () |
Protected Attributes | |
FILE * | logger |
Private Attributes | |
double | alpha |
double | beta |
double | stopTime |
int | usefilter |
int | x |
int | y |
int | z |
double | estimatedOtsuThreshold |
Clase para segmentar una imagen usando fast marching
La imagen de velocidad se obtiene aplicando una transformacion sigmoide a cada voxel. Dicha transformacion esta regida por los parámetros alfa (pendiente) y beta(centro de pivotaje)
Definition at line 9 of file itkFM3D.h.
itkFM3D::itkFM3D | ( | ) |
Definition at line 29 of file itkFM3D.cxx.
itkFM3D::~itkFM3D | ( | ) | [virtual] |
Definition at line 39 of file itkFM3D.cxx.
void itkFM3D::AddSeed | ( | int | x, | |
int | y, | |||
int | z | |||
) |
Definition at line 43 of file itkFM3D.cxx.
Referenced by wxSegmentationFM3DWidget::OnBtnSegment().
void itkFM3D::CurvatureAnisotropicFilterOff | ( | ) |
Definition at line 67 of file itkFM3D.cxx.
References usefilter.
00067 { 00068 usefilter = 0; 00069 }
void itkFM3D::CurvatureAnisotropicFiltertOn | ( | ) |
Definition at line 63 of file itkFM3D.cxx.
References usefilter.
00063 { 00064 usefilter = 1; 00065 }
double itkFM3D::GetEstimatedOtsuTreshold | ( | ) |
vtkImageData * itkFM3D::segment | ( | vtkImageData * | volume | ) |
Definition at line 72 of file itkFM3D.cxx.
References alpha, beta, stopTime, usefilter, x, y, and z.
Referenced by wxSegmentationFM3DWidget::OnBtnSegment().
00072 { 00073 00074 volume->SetSpacing(1,1,1); 00075 00076 00077 double spc[3]; 00078 volume->GetSpacing(spc); 00079 00080 00081 double xx = x; // * spc[0]; 00082 double yy = y; // * spc[1]; 00083 double zz = z; // * spc[2]; 00084 00085 00086 /*FILE *ff; 00087 ff=fopen("programa/Ups.bat","w"); 00088 fprintf(ff,"vtkViewer imagen.vtk %d %d %d %f %f %f \n",(int)xx,(int)yy,(int)zz,(float)alpha,(float)beta,(float)stopTime); 00089 fclose(ff); 00090 int code = wxExecute("programa/Ups.bat", wxEXEC_SYNC);*/ 00091 00092 00093 00094 00095 00096 00097 //DEFINICION IMAGEN ENTRADA 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 // CONVERTIR LA IMAGEN DE ENTRADA A ITK PARA SER PROCESADA 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 //typedef itk::ImageFileReader<InternalImageType> ReaderType; 00121 //ReaderType::Pointer reader = ReaderType::New(); 00122 //reader->SetFileName("imagen.vtk"); 00123 00124 //reader->Update(); 00125 00126 //InternalImageType* itkImageData = null; //reader->GetOutput(); 00127 00128 00129 00130 00131 //DEFINICION WRITER 00132 //-------------------------------------------------------------------------------------------- 00133 // typedef itk::ImageFileWriter<InternalImageType> WriterType; 00134 00135 00136 00137 //DEFINICION DEL FILTRADO ANISOTROPICO 00138 //-------------------------------------------------------------------------------------------- 00139 typedef itk::CurvatureAnisotropicDiffusionImageFilter< 00140 InternalImageType, 00141 InternalImageType > SmoothingFilterType; 00142 00143 SmoothingFilterType::Pointer filtroAnisotropico = SmoothingFilterType::New(); 00144 00145 00146 00147 //DEFINICION DEL FILTRO SIGMOIDE 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 //DECLARACION DEL FILTRO DE FAST MARCHING 00163 //-------------------------------------------------------------------------------------------- 00164 typedef itk::FastMarchingImageFilter< InternalImageType, 00165 InternalImageType > FastMarchingFilterType; 00166 FastMarchingFilterType::Pointer fastMarching = FastMarchingFilterType::New(); 00167 00168 //DECLARACION DEL FILTRO THRESHOLDING BINARIO 00169 //-------------------------------------------------------------------------------------------- 00170 typedef itk::BinaryThresholdImageFilter<InternalImageType, InternalImageType > ThresholdingFilterType; 00171 ThresholdingFilterType::Pointer thresholdFilter = ThresholdingFilterType::New(); 00172 00173 00174 //-------------------------------------------------------------------------------------------- 00175 // ESTABLECIMIENTO DE LA SEMILLA DEL FAST MARCHING 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; // POSICION X DE LA SEMILLA 00183 seedPosition[1] = this->y; // POSICION Y DE LA SEMILLA 00184 seedPosition[2] = this->z; // POSICION Z DE LA SEMILLA 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 //COLOCAR VARIAS SEMILLAS 00197 fastMarching->SetTrialPoints( seeds ); 00198 fastMarching->SetOutputSize(itkImageData->GetBufferedRegion().GetSize() ); 00199 fastMarching->SetStoppingValue(this->stopTime); 00200 00201 00202 //DEFINICION WRITER FILTRADO ANISOTROPICO 00203 //WriterType::Pointer writerFiltroRuido = WriterType::New(); 00204 //writerFiltroRuido->SetFileName("filtrado.vtk"); 00205 00206 00207 //DEFINICION WRITER SIGMOIDE 00208 //-------------------------------------------------------------------------------------------- 00209 //WriterType::Pointer writerSigmoide = WriterType::New(); 00210 //writerSigmoide->SetFileName("sigmoid.vtk"); 00211 00212 00213 00214 //DEFINICION WRITER FAST MARCHING MAPA DE TIEMPOS 00215 //-------------------------------------------------------------------------------------------- 00216 /*typedef itk::VTKImageIO ImageIOType; 00217 ImageIOType::Pointer vtkIO = ImageIOType::New(); 00218 vtkIO->SetFileTypeToASCII(); 00219 00220 WriterType::Pointer writerMapaTiempos = WriterType::New(); 00221 writerMapaTiempos->SetFileName("fastmarch.vtk"); 00222 writerMapaTiempos->SetImageIO(vtkIO);*/ 00223 00224 00225 //DEFINICION WRITER THRESHOLD BINARIO 00226 //-------------------------------------------------------------------------------------------- 00227 // WriterType::Pointer writerThreshold = WriterType::New(); 00228 // writerThreshold->SetFileName("binario.vtk"); 00229 //writerThreshold->SetImageIO(vtkIO); 00230 00231 00232 //-------------------------------------------------------------------------------------------- 00233 // CONSTRUCCION DEL PIPELINE DE PROCESAMIENTO 00234 //-------------------------------------------------------------------------------------------- 00235 00236 // READER -> FILTRO ANISOTROPICO 00237 //FILTRO ANISOTROPICO -> SIGMOIDE 00238 if (usefilter == 1){ 00239 //filtroAnisotropico->SetInput(itkImageData); 00240 //sigmoid->SetInput(filtroAnisotropico->GetOutput()); 00241 sigmoid->SetInput(itkImageData); 00242 } 00243 else { 00244 sigmoid->SetInput(itkImageData); 00245 } 00246 00247 // SIGMOIDE -> FAST MARCHING 00248 fastMarching->SetInput(sigmoid->GetOutput()); 00249 00250 00251 // FAST MARCHING -> WRITER 00252 //writerFiltroRuido->SetInput(filtroAnisotropico->GetOutput()); 00253 //writerSigmoide->SetInput(sigmoid->GetOutput()); 00254 //writerMapaTiempos->SetInput(fastMarching->GetOutput()); 00255 //writerFiltroRuido->Update(); 00256 00257 00258 //---------------------------------------------------------------------------------------- 00259 //Ejecucion de OTSU 00260 //---------------------------------------------------------------------------------------- 00261 00262 //DEFINICION DE IMAGEN VTK FILTRADA CON ANISOTROPICO 00263 //-------------------------------------------------------------------------------------------- 00264 typedef itk::ImageToVTKImageFilter<InternalImageType> ItkToVtkFilterType; 00265 00266 00267 00268 /*vtkOtsuImageData* otsu = new vtkOtsuImageData(); 00269 00270 if(this->usefilter){ 00271 printf("Calculando Th Otsu sobre la imagen filtrada\n"); 00272 ItkToVtkFilterType::Pointer toVtk = ItkToVtkFilterType::New(); 00273 toVtk->SetInput(filtroAnisotropico->GetOutput()); 00274 toVtk->Update(); 00275 vtkImageData* imagenFiltrada = toVtk->GetOutput(); 00276 estimatedOtsuThreshold = otsu->calculateOptimalThreshold(imagenFiltrada); 00277 } 00278 else{ 00279 printf("Calculando Th Otsu sobre la imagen original\n"); 00280 estimatedOtsuThreshold = otsu->calculateOptimalThreshold(volume); 00281 } 00282 00283 printf("BETA = %.0f, OTSU = %d\n",beta,estimatedOtsuThreshold);*/ 00284 00285 //sigmoid->SetBeta((double)ThresholdOtsu); 00286 00287 //---------------------------------------------------------------------------------------- 00288 00289 00290 //-------------------------------------------------------------------------------------------- 00291 // EJECUCION DEL PIPELINE 00292 //-------------------------------------------------------------------------------------------- 00293 try 00294 { 00295 fastMarching->Update(); 00296 //writerFiltroRuido->Update(); 00297 //writerSigmoide->Update(); 00298 //writerMapaTiempos->Update(); 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 // LEYENDO MAPA DE TIEMPOS 00310 //--------------------------------------------------------- 00311 // En esta parte escribimos el mapa de tiempos a un archivo plano para 00312 // poder estudiarlo en Excel. 00313 00314 printf("Procesando el mapa de tiempos...."); 00315 00316 //ACTIVAR PARA DEBUG 00317 //----------------------------- 00318 //FILE *logger = NULL; 00319 // if ((logger = freopen("mapatiempos.txt","w", stdout)) == NULL) 00320 // exit(-1); 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 // inicializar histogramas 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 // calcular histograma 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 //calcular histograma acumulado 00355 for (indice = 1 ; indice <= 1000; indice++){ 00356 histoAcumulado[indice]= histoAcumulado[indice-1]+ histoTiempos[indice]; 00357 } 00358 00359 //buscar la frecuencia minima = derivada de la funcion de frecuencia acumulada 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 //for (indice = 0; indice <= 1000; indice++){ 00377 // printf("%.2f:%d:%d\n",(double)indice/100.0, histoTiempos[indice], histoAcumulado[indice]); 00378 //} 00379 00380 00381 //fclose(logger); 00382 //logger = NULL; 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 // writerThreshold->SetInput(thresholdFilter->GetOutput()); 00394 00395 try{ 00396 thresholdFilter->Update(); 00397 //writerThreshold->Update(); 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 }
void itkFM3D::SetAlpha | ( | double | alpha | ) |
Definition at line 49 of file itkFM3D.cxx.
Referenced by wxSegmentationFM3DWidget::OnBtnSegment().
void itkFM3D::SetBeta | ( | double | beta | ) |
Definition at line 54 of file itkFM3D.cxx.
Referenced by wxSegmentationFM3DWidget::OnBtnSegment().
void itkFM3D::SetStopTime | ( | double | stopTime | ) |
Definition at line 59 of file itkFM3D.cxx.
double itkFM3D::alpha [private] |
double itkFM3D::beta [private] |
double itkFM3D::estimatedOtsuThreshold [private] |
FILE* itkFM3D::logger [protected] |
double itkFM3D::stopTime [private] |
int itkFM3D::usefilter [private] |
Definition at line 31 of file itkFM3D.h.
Referenced by CurvatureAnisotropicFilterOff(), CurvatureAnisotropicFiltertOn(), itkFM3D(), and segment().
int itkFM3D::x [private] |
int itkFM3D::y [private] |
int itkFM3D::z [private] |