/*========================================================================= Program: vv http://www.creatis.insa-lyon.fr/rio/vv Authors belong to: - University of LYON http://www.universite-lyon.fr/ - Léon Bérard cancer center http://www.centreleonberard.fr - CREATIS CNRS laboratory http://www.creatis.insa-lyon.fr This software is distributed WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the copyright notices for more information. It is distributed under dual licence - BSD See included LICENSE.txt file - CeCILL-B http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html ===========================================================================**/ //-------------------------------------------------------------------- template clitk::MorphoMathFilter::MorphoMathFilter(): clitk::FilterBase(), itk::ImageToImageFilter() { this->SetNumberOfRequiredInputs(1); SetBackgroundValue(0); SetForegroundValue(1); SetOperationType(0); SizeType p; p.Fill(1); SetRadius(p); SetBoundaryToForegroundFlag(false); VerboseFlagOff(); } //-------------------------------------------------------------------- //-------------------------------------------------------------------- template clitk::MorphoMathFilter::~MorphoMathFilter() { // Nothing } //-------------------------------------------------------------------- //-------------------------------------------------------------------- template void clitk::MorphoMathFilter:: SetRadiusInMM(PointType & p) { m_RadiusInMM = p; m_RadiusInMMIsSet = true; m_RadiusIsSet = false; } //-------------------------------------------------------------------- //-------------------------------------------------------------------- template void clitk::MorphoMathFilter:: SetRadius(SizeType & p) { m_Radius = p; m_RadiusIsSet = true; m_RadiusInMMIsSet = false; } //-------------------------------------------------------------------- //-------------------------------------------------------------------- template void clitk::MorphoMathFilter:: SetRadius(int r) { for(uint i=0; i void clitk::MorphoMathFilter:: SetOperationType(int type) { if(type<0 || type>5) clitkExceptionMacro("Operation type must be between 0-5 (0=Erode, 1=Dilate, 2=Close (erode(dilate(x))), 3=Open (dilate(erode(x))), 4=CondErode, 5=CondDilate)"); m_OperationType = OperationTypeEnumeration(type); } //-------------------------------------------------------------------- //-------------------------------------------------------------------- template void clitk::MorphoMathFilter:: GenerateInputRequestedRegion() { // Call default itk::ImageToImageFilter::GenerateInputRequestedRegion(); // Get input pointers and set requested region to common region ImagePointer input1 = dynamic_cast(itk::ProcessObject::GetInput(0)); input1->SetRequestedRegion(input1->GetLargestPossibleRegion()); } //-------------------------------------------------------------------- //-------------------------------------------------------------------- template void clitk::MorphoMathFilter:: GenerateOutputInformation() { //--------------------------------- // Define the images //--------------------------------- ImagePointer m_input = dynamic_cast(itk::ProcessObject::GetInput(0)); const unsigned int dim = ImageType::ImageDimension; //--------------------------------- // Cast into internalimagetype //--------------------------------- typedef itk::CastImageFilter InputCastImageFilterType; typename InputCastImageFilterType::Pointer caster = InputCastImageFilterType::New(); caster->SetInput(m_input); caster->Update(); input =caster->GetOutput(); //--------------------------------- // Compute the radius in pixel //--------------------------------- if (m_RadiusInMMIsSet) { for(uint i=0; iGetSpacing()[i]); } } //--------------------------------- // Extend the image if needed //--------------------------------- if (GetExtendSupportFlag()) { typedef itk::ConstantPadImageFilter PadFilterType; typename PadFilterType::Pointer padFilter = PadFilterType::New(); padFilter->SetInput(input); typename ImageType::SizeType lower; typename ImageType::SizeType upper; for(uint i=0; iSetPadLowerBound(lower); padFilter->SetPadUpperBound(upper); padFilter->Update(); if (GetVerboseFlag()) std::cout << "Extend the image to 2x " << lower << std::endl; input = padFilter->GetOutput(); } // Set output size ImagePointer outputImage = this->GetOutput(0); outputImage->SetRegions(input->GetLargestPossibleRegion()); } //-------------------------------------------------------------------- //-------------------------------------------------------------------- template void clitk::MorphoMathFilter:: GenerateData() { //--------------------------------- // Build kernel //--------------------------------- typedef itk::BinaryBallStructuringElement KernelType; KernelType structuringElement; if (GetVerboseFlag()) { std::cout << "Radius in pixel : " << m_Radius << std::endl; } structuringElement.SetRadius(m_Radius); structuringElement.CreateStructuringElement(); //--------------------------------- // Switch according to operation type //--------------------------------- typedef itk::ImageToImageFilter ImageFilterType; typename ImageFilterType::Pointer filter; switch(m_OperationType) { case Erode: { typedef itk::BinaryErodeImageFilter FilterType; typename FilterType::Pointer m = FilterType::New(); m->SetBackgroundValue(this->GetBackgroundValue()); m->SetForegroundValue(this->GetForegroundValue()); m->SetBoundaryToForeground(GetBoundaryToForegroundFlag()); m->SetKernel(structuringElement); filter=m; if (GetVerboseFlag()) std::cout<<"Using the erode filter..."< FilterType; typename FilterType::Pointer m = FilterType::New(); m->SetBackgroundValue(this->GetBackgroundValue()); m->SetForegroundValue(this->GetForegroundValue()); m->SetBoundaryToForeground(GetBoundaryToForegroundFlag()); m->SetKernel(structuringElement); filter=m; if (GetVerboseFlag()) std::cout<<"Using the dilate filter..."< FilterType; typename FilterType::Pointer m = FilterType::New(); m->SetForegroundValue(this->GetForegroundValue()); m->SetSafeBorder(GetBoundaryToForegroundFlag()); m->SetKernel(structuringElement); filter=m; if (GetVerboseFlag()) std::cout<<"Using the closing filter..."< FilterType; typename FilterType::Pointer m = FilterType::New(); m->SetBackgroundValue(this->GetBackgroundValue()); m->SetForegroundValue(this->GetForegroundValue()); m->SetKernel(structuringElement); filter=m; if (GetVerboseFlag()) std::cout<<"Using the opening filter..."< FilterType; typename FilterType::Pointer m = FilterType::New(); m->SetBackgroundValue(this->GetBackgroundValue()); m->SetForegroundValue(this->GetForegroundValue()); m->SetBoundaryToForeground(GetBoundaryToForegroundFlag()); m->SetKernel(structuringElement); filter=m; if (GetVerboseFlag()) std::cout<<"Using the conditional erode filter..."< FilterType; typename FilterType::Pointer m = FilterType::New(); m->SetBackgroundValue(this->GetBackgroundValue()); m->SetForegroundValue(this->GetForegroundValue()); m->SetBoundaryToForeground(GetBoundaryToForegroundFlag()); m->SetKernel(structuringElement); filter=m; if (GetVerboseFlag()) std::cout<<"Using the conditional dilate filter..."<SetInput(input); filter->Update(); //--------------------------------- // Write the output //--------------------------------- typedef itk::CastImageFilter< InternalImageType, ImageType > OutputCastImageFilterType; typename OutputCastImageFilterType::Pointer oCaster = OutputCastImageFilterType::New(); oCaster->SetInput(filter->GetOutput()); oCaster->Update(); this->SetNthOutput(0, oCaster->GetOutput()); //this->GraftOutput(oCaster->GetOutput()); // NO } //--------------------------------------------------------------------