mirror of
https://gitlab.ethz.ch/gfattori/glocalize.git
synced 2026-05-03 13:44:26 +02:00
246 lines
7.4 KiB
C++
246 lines
7.4 KiB
C++
#include "gSkullRemoval.h"
|
|
//
|
|
///**
|
|
// * This will be setup as a callback for a progress event on an ITK
|
|
// * filter.
|
|
// */
|
|
//struct ProgressDisplay
|
|
//{
|
|
//
|
|
//public:
|
|
// QString observedProcess;
|
|
//
|
|
// ProgressDisplay(itk::ProcessObject* process,QString observed): m_Process(process) {
|
|
// observedProcess=observed;
|
|
// }
|
|
//
|
|
// void Display()
|
|
// {
|
|
// float progress = m_Process->GetProgress()*100.0;
|
|
// cout <<progress<<endl;
|
|
// }
|
|
// itk::ProcessObject::Pointer m_Process;
|
|
//};
|
|
|
|
gSkullRemoval::gSkullRemoval()
|
|
{
|
|
|
|
aborted=false;
|
|
vtkImporter1=vtkSmartPointer<vtkImageImport>::New();
|
|
vtkExporter = vtkSmartPointer<vtkImageExport>::New();
|
|
smoothing = CurvatureFlowImageFilterType::New();
|
|
connectedThreshold = ConnectedFilterType::New();
|
|
binaryErodeFilter = ErodeFilterType::New();
|
|
binaryDilateFilter = DilateFilterType::New();
|
|
itkExporter1 = ExportFilter1Type::New();
|
|
itkImporter = ImportFilterType::New();
|
|
caster_writer_inv =CastingwriterINVFilterType::New();
|
|
maskNegatedFilter = MaskNegatedFilterType::New();
|
|
caster_writer = CastingwriterFilterType::New();
|
|
caster = CastingFilterType::New();
|
|
|
|
/*TEST*/
|
|
m_RedrawCommand = RedrawCommandType::New();
|
|
m_RedrawCommand->SetCallbackFunction( this, &gSkullRemoval::ProcessEvent );
|
|
m_RedrawCommand->SetCallbackFunction( this, &gSkullRemoval::ConstProcessEvent );
|
|
|
|
this->Observe(smoothing.GetPointer());
|
|
this->Observe(connectedThreshold.GetPointer());
|
|
this->Observe(binaryErodeFilter.GetPointer());
|
|
this->Observe(binaryDilateFilter.GetPointer());
|
|
this->Observe(maskNegatedFilter.GetPointer());
|
|
}
|
|
gSkullRemoval::~gSkullRemoval()
|
|
{
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void gSkullRemoval::runFilter(vtkImageData* inputVolume,double thr_low, double thr_up)
|
|
{
|
|
|
|
|
|
vtkExporter->SetInputData(inputVolume);
|
|
ConnectPipelines(vtkExporter.GetPointer(), itkImporter);
|
|
caster_writer_inv->SetInput(itkImporter->GetOutput());
|
|
smoothing->SetInput( caster_writer_inv->GetOutput() );
|
|
connectedThreshold->SetInput( smoothing->GetOutput() );
|
|
connectedThreshold->Update();
|
|
caster->SetInput( connectedThreshold->GetOutput() );
|
|
smoothing->SetNumberOfIterations( 10 );
|
|
smoothing->SetTimeStep( 0.125 );
|
|
const InternalPixelType lowerThreshold = thr_low ;
|
|
const InternalPixelType upperThreshold = thr_up;
|
|
connectedThreshold->SetLower( lowerThreshold );
|
|
connectedThreshold->SetUpper( upperThreshold );
|
|
connectedThreshold->SetReplaceValue( 255 );
|
|
InternalImageType::Pointer inputImage = caster_writer_inv->GetOutput();
|
|
InternalImageType::SizeType size = inputImage->GetBufferedRegion().GetSize();
|
|
InternalImageType::IndexType start = inputImage->GetBufferedRegion().GetIndex();
|
|
|
|
// set a seed by default in the center of the image.
|
|
InternalImageType::IndexType seed;
|
|
seed[0] = start[0] + size[0] / 2;
|
|
seed[1] = start[1] + size[1] / 2;
|
|
seed[2] = start[2] + size[2] / 2;
|
|
connectedThreshold->SetSeed( seed );
|
|
|
|
StructuringElementType structuringElement;
|
|
structuringElement.SetRadius( 10 );
|
|
structuringElement.CreateStructuringElement();
|
|
binaryDilateFilter->SetKernel( structuringElement );
|
|
binaryErodeFilter->SetKernel( structuringElement );
|
|
binaryDilateFilter->SetInput( caster->GetOutput() );
|
|
binaryErodeFilter->SetInput( binaryDilateFilter->GetOutput() );
|
|
binaryDilateFilter->SetDilateValue( 255 );
|
|
binaryErodeFilter->SetErodeValue( 255 );
|
|
|
|
// if (dcmoutput == 1) writer->SetInput(binaryErodeFilter->GetOutput());
|
|
maskNegatedFilter->SetInput1(caster_writer_inv->GetOutput());
|
|
maskNegatedFilter->SetInput2(binaryErodeFilter->GetOutput());
|
|
|
|
// maskNegatedFilter->Update();
|
|
|
|
//caster_writer->SetInput(maskNegatedFilter->GetOutput());
|
|
// if (dcmoutput == 1){ writer_internal->SetFileName( "/home/gio/dati/output/out_mask.dcm" );
|
|
// writer_internal->SetInput(caster_writer->GetOutput());};
|
|
|
|
bool execeptCaught = false;
|
|
|
|
itkImporter->ReleaseDataFlagOn();
|
|
//caster_writer_inv->ReleaseDataFlagOn();
|
|
connectedThreshold->ReleaseDataFlagOn();
|
|
binaryDilateFilter->ReleaseDataFlagOn();
|
|
binaryErodeFilter->ReleaseDataFlagOn();
|
|
|
|
//maskNegatedFilter->ReleaseDataFlagOn();
|
|
|
|
try {
|
|
maskNegatedFilter.GetPointer()->Update();
|
|
}
|
|
catch( itk::ExceptionObject & excep )
|
|
{
|
|
if (aborted) std::cerr << "Abort caught!" << std::endl;
|
|
else {
|
|
execeptCaught=true;
|
|
emit errMsg(QString(excep.GetDescription()));
|
|
std::cerr << "Exception caught !" << std::endl;
|
|
std::cerr << excep << std::endl;
|
|
}
|
|
};
|
|
|
|
if (!aborted && execeptCaught==false){
|
|
|
|
|
|
// fixedVolume->DeepCopy(inputVolume);
|
|
|
|
// try
|
|
// {
|
|
// // if (dcmoutput == 1) {writer->Update();
|
|
// // writer_internal->Update();};
|
|
// fixedVolume->DeepCopy(vtkImporter1->GetOutput());
|
|
// cout<<"finito itk"<<endl;
|
|
// }
|
|
// catch( itk::ExceptionObject & excep )
|
|
// {
|
|
// std::cerr << "Exception caught !" << std::endl;
|
|
// std::cerr << excep << std::endl;
|
|
// };
|
|
|
|
|
|
//typedef itk::ImageFileWriter< InternalImageType > WriterType;
|
|
//WriterType::Pointer writer = WriterType::New();
|
|
//writer->SetFileName("out.mha");
|
|
//writer->SetInput( maskNegatedFilter->GetOutput() );
|
|
//writer->Update();
|
|
//
|
|
|
|
|
|
itkExporter1->SetInput( maskNegatedFilter->GetOutput() );
|
|
ConnectPipelines(itkExporter1.GetPointer(), vtkImporter1);
|
|
vtkImporter1->Update();
|
|
|
|
emit skull_mask_end(vtkImporter1->GetOutput());
|
|
//fixedVolume->DeepCopy();
|
|
}
|
|
}
|
|
|
|
|
|
void gSkullRemoval::abortSignal()
|
|
{
|
|
aborted=true;
|
|
// cout<<"arrivato segnale di abort in itk: setto gli abort generate data"<<endl;
|
|
|
|
vtkImporter1->AbortExecuteOn();
|
|
maskNegatedFilter.GetPointer()->AbortGenerateDataOn();
|
|
binaryErodeFilter.GetPointer()->AbortGenerateDataOn();
|
|
binaryDilateFilter.GetPointer()->AbortGenerateDataOn();
|
|
connectedThreshold.GetPointer()->AbortGenerateDataOn();
|
|
smoothing.GetPointer()->AbortGenerateDataOn();
|
|
|
|
// cout<<"fatto"<<endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*TEST*/
|
|
|
|
|
|
|
|
/** Manage a Progress event */
|
|
void
|
|
gSkullRemoval::ProcessEvent( itk::Object * caller,
|
|
const itk::EventObject & event )
|
|
{
|
|
if( typeid( itk::ProgressEvent ) == typeid( event ) )
|
|
{
|
|
::itk::ProcessObject::Pointer process =
|
|
dynamic_cast< itk::ProcessObject *>( caller );
|
|
if(caller == smoothing)
|
|
emit skull_mask_upd("2/5 : Surface smoothing...", process->GetProgress());
|
|
|
|
if(caller == connectedThreshold)
|
|
emit skull_mask_upd("1/5 : Region growing...", process->GetProgress());
|
|
|
|
if(caller == binaryErodeFilter)
|
|
emit skull_mask_upd("4/5 : Morphological erode...", process->GetProgress());
|
|
|
|
if(caller ==binaryDilateFilter)
|
|
emit skull_mask_upd("3/5 : Morphological dilate...", process->GetProgress());
|
|
|
|
if(caller == maskNegatedFilter)
|
|
emit skull_mask_upd("5/5 : Volume masking...", process->GetProgress());
|
|
|
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
gSkullRemoval::ConstProcessEvent( const itk::Object * caller,
|
|
const itk::EventObject & event )
|
|
{
|
|
if( typeid( itk::ProgressEvent ) == typeid( event ) )
|
|
{
|
|
itk::ProcessObject::ConstPointer process =
|
|
dynamic_cast< const itk::ProcessObject *>( caller );
|
|
cout<< "B"<< process->GetProgress() <<endl;
|
|
}
|
|
}
|
|
|
|
|
|
/** Manage a Progress event */
|
|
void
|
|
gSkullRemoval::Observe( itk::Object *caller )
|
|
{
|
|
caller->AddObserver( itk::ProgressEvent(), m_RedrawCommand.GetPointer() );
|
|
}
|