From: dsarrut Date: Tue, 15 Feb 2011 11:31:34 +0000 (+0000) Subject: morpho math filter X-Git-Tag: v1.2.0~243 X-Git-Url: https://git.creatis.insa-lyon.fr/pubgit/?a=commitdiff_plain;h=f34c74217b4f8efb36d1fa7f7d65992f1eefd2a4;p=clitk.git morpho math filter --- diff --git a/segmentation/clitkMorphoMath.cxx b/segmentation/clitkMorphoMath.cxx index 9952fe5..97b6705 100755 --- a/segmentation/clitkMorphoMath.cxx +++ b/segmentation/clitkMorphoMath.cxx @@ -17,36 +17,33 @@ ======================================================================-====*/ #ifndef clitkMorphoMath_cxx #define clitkMorphoMath_cxx -/** - ================================================= - * @file clitkMorphoMath.cxx - * @author Jef Vandemeulebroucke - * @date 5 May 2009 - * - * @brief - =================================================*/ // clitk include #include "clitkMorphoMath_ggo.h" #include "clitkMorphoMathGenericFilter.h" -#include "clitkIO.h" -#include "clitkImageCommon.h" +//-------------------------------------------------------------------- int main(int argc, char * argv[]) { - //Init command line + // Init command line GGO(clitkMorphoMath,args_info); CLITK_INIT; - //Creation of a generic filter - clitk::MorphoMathGenericFilter::Pointer genericFilter = clitk::MorphoMathGenericFilter::New(); + // Creation of a generic filter + typedef clitk::MorphoMathGenericFilter FilterType; + FilterType::Pointer filter = FilterType::New(); - //Passing the arguments to the generic filter - genericFilter->SetArgsInfo(args_info); + // Passing the arguments to the generic filter + filter->SetArgsInfo(args_info); - //update - genericFilter->Update(); - return 0; -} + try { + filter->Update(); + } catch(std::runtime_error e) { + std::cout << e.what() << std::endl; + } + + return EXIT_SUCCESS; +} // This is the end, my friend +//-------------------------------------------------------------------- #endif diff --git a/segmentation/clitkMorphoMath.ggo b/segmentation/clitkMorphoMath.ggo index 3006d18..c754f5e 100755 --- a/segmentation/clitkMorphoMath.ggo +++ b/segmentation/clitkMorphoMath.ggo @@ -5,27 +5,24 @@ package "clitk" version "Perform standard morphological operations" -option "config" - "Config file" string no - - -section "Run Time" - -option "verbose" v "Verbose" flag off - +option "config" - "Config file" string no +option "verbose" v "Verbose" flag off +option "imagetypes" - "Display allowed image types" flag off section "Input" -option "input" i "Input grid filename" string yes -option "output" o "Output grid filename" string yes +option "input" i "Input grid filename" string yes +option "output" o "Output grid filename" string yes section "Processing Parameters" -option "type" t "0=Erode, 1=Dilate, 2=Close (erode(dilate(x))), 3=Open (dilate(erode(x)))" int no default="0" +option "type" t "0=Erode, 1=Dilate, 2=Close (erode(dilate(x))), 3=Open (dilate(erode(x))), 4=CondErode, 5=CondDilate" int no default="0" option "fg" - "Foreground value" float no default="1" option "bg" - "Background value (0,1,3: filling value)" float no default="0" option "bound" b "0-1: Set borders to foreground/ 2:safe borders " flag off option "radius" r "Use binary ball element with given radius" int no multiple default="1" +option "radiusInMM" m "Use binary ball element with given radius in mm (rounded to nearest voxel value)" double no multiple default="1" - +option "extend" - "Extend the image size according to radius" flag off diff --git a/segmentation/clitkMorphoMathFilter.h b/segmentation/clitkMorphoMathFilter.h new file mode 100644 index 0000000..9422b58 --- /dev/null +++ b/segmentation/clitkMorphoMathFilter.h @@ -0,0 +1,140 @@ +/*========================================================================= + 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://oncora1.lyon.fnclcc.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 + ======================================================================-====*/ + +#ifndef CLITKMORPHOMATHFILTER_H +#define CLITKMORPHOMATHFILTER_H + +// clitk include +#include "clitkIO.h" +#include "clitkImageCommon.h" +#include "clitkMorphoMath_ggo.h" +#include "clitkConditionalBinaryErodeImageFilter.h" +#include "clitkConditionalBinaryDilateImageFilter.h" + +// itk include +#include +#include +#include +#include +#include +#include +#include + +namespace clitk { + + //-------------------------------------------------------------------- + template + class ITK_EXPORT MorphoMathFilter: + public clitk::FilterBase, + public itk::ImageToImageFilter + { + + public: + /** Standard class typedefs. */ + typedef itk::ImageToImageFilter Superclass; + typedef MorphoMathFilter Self; + typedef itk::SmartPointer Pointer; + typedef itk::SmartPointer ConstPointer; + + /** Method for creation through the object factory. */ + itkNewMacro(Self); + + /** Run-time type information (and related methods). */ + itkTypeMacro(AddRelativePositionConstraintToLabelImageFilter, ImageToImageFilter); + FILTERBASE_INIT; + + /** Some convenient typedefs. */ + typedef typename ImageType::ConstPointer ImageConstPointer; + typedef typename ImageType::Pointer ImagePointer; + typedef typename ImageType::RegionType RegionType; + typedef typename ImageType::PixelType PixelType; + typedef typename ImageType::SpacingType SpacingType; + typedef typename ImageType::SizeType SizeType; + typedef typename ImageType::IndexType IndexType; + typedef typename ImageType::PointType PointType; + typedef float InternalPixelType; + typedef itk::Image InternalImageType; + + /** ImageDimension constants */ + itkStaticConstMacro(ImageDimension, unsigned int, ImageType::ImageDimension); + typedef itk::Image FloatImageType; + + /** Operation types */ + typedef enum { Erode = 0, Dilate = 1, + Close = 2, Open = 3, + CondErode = 4, CondDilate = 5 + } OperationTypeEnumeration; + + /** Options */ + itkGetConstMacro(VerboseFlag, bool); + itkSetMacro(VerboseFlag, bool); + itkBooleanMacro(VerboseFlag); + + itkGetConstMacro(BackgroundValue, PixelType); + itkSetMacro(BackgroundValue, PixelType); + + itkGetConstMacro(ForegroundValue, PixelType); + itkSetMacro(ForegroundValue, PixelType); + + void SetOperationType(int type); + + itkGetConstMacro(RadiusInMM, PointType); + void SetRadiusInMM(PointType & p); + + itkGetConstMacro(Radius, SizeType); + void SetRadius(SizeType & r); + + itkGetConstMacro(BoundaryToForegroundFlag, bool); + itkSetMacro(BoundaryToForegroundFlag, bool); + itkBooleanMacro(BoundaryToForegroundFlag); + + itkGetConstMacro(ExtendSupportFlag, bool); + itkSetMacro(ExtendSupportFlag, bool); + itkBooleanMacro(ExtendSupportFlag); + + protected: + MorphoMathFilter(); + virtual ~MorphoMathFilter(); + + bool m_VerboseFlag; + PixelType m_BackgroundValue; + PixelType m_ForegroundValue; + PointType m_RadiusInMM; + SizeType m_Radius; + bool m_RadiusInMMIsSet; + bool m_RadiusIsSet; + OperationTypeEnumeration m_OperationType; + bool m_BoundaryToForegroundFlag; + bool m_ExtendSupportFlag; + typename InternalImageType::Pointer input; + + virtual void GenerateOutputInformation(); + virtual void GenerateInputRequestedRegion(); + virtual void GenerateData(); + + private: + MorphoMathFilter(const Self&); //purposely not implemented + void operator=(const Self&); //purposely not implemented + }; + +} // end namespace clitk +#ifndef ITK_MANUAL_INSTANTIATION +#include "clitkMorphoMathFilter.txx" +#endif + +#endif //#define CLITKMORPHOMATHFILTER_H diff --git a/segmentation/clitkMorphoMathFilter.txx b/segmentation/clitkMorphoMathFilter.txx new file mode 100644 index 0000000..5d0b502 --- /dev/null +++ b/segmentation/clitkMorphoMathFilter.txx @@ -0,0 +1,283 @@ +/*========================================================================= + 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://oncora1.lyon.fnclcc.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); +} +//-------------------------------------------------------------------- + + +//-------------------------------------------------------------------- +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:: +SetOperationType(int type) +{ + switch (type) { + case 0: m_OperationType = Erode; return; + case 1: m_OperationType = Dilate; return; + case 2: m_OperationType = Open; return; + case 3: m_OperationType = Close; return; + case 4: m_OperationType = CondErode; return; + case 5: m_OperationType = CondDilate; return; + default: 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)"); + } +} +//-------------------------------------------------------------------- + + +//-------------------------------------------------------------------- +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)); + + //--------------------------------- + // 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; i<3; i++) { + lower[i] = upper[i] = 2*(m_Radius[i]+1); + } + padFilter->SetPadLowerBound(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()); +} +//-------------------------------------------------------------------- + + diff --git a/segmentation/clitkMorphoMathGenericFilter.h b/segmentation/clitkMorphoMathGenericFilter.h index 23a8c23..e182264 100755 --- a/segmentation/clitkMorphoMathGenericFilter.h +++ b/segmentation/clitkMorphoMathGenericFilter.h @@ -14,85 +14,58 @@ - BSD See included LICENSE.txt file - CeCILL-B http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html -======================================================================-====*/ -#ifndef clitkMorphoMathGenericFilter_h -#define clitkMorphoMathGenericFilter_h -/** - ================================================= - * @file clitkMorphoMathGenericFilter.h - * @author Jef Vandemeulebroucke - * @date 5 May 2009 - * - * @brief - * - =================================================*/ + ======================================================================-====*/ +#ifndef CLITKMORPHOMATHGENERICFILTER_H +#define CLITKMORPHOMATHGENERICFILTER_H // clitk include #include "clitkIO.h" #include "clitkImageCommon.h" +#include "clitkImageToImageGenericFilter.h" #include "clitkMorphoMath_ggo.h" -#include "clitkConditionalBinaryErodeImageFilter.h" -#include "clitkConditionalBinaryDilateImageFilter.h" +#include "clitkMorphoMathFilter.h" -// itk include -#include "itkLightObject.h" -#include "itkBinaryErodeImageFilter.h" -#include "itkBinaryDilateImageFilter.h" -#include "itkBinaryMorphologicalClosingImageFilter.h" -#include "itkBinaryMorphologicalOpeningImageFilter.h" -#include "itkBinaryBallStructuringElement.h" -#include "itkCastImageFilter.h" - -namespace clitk { +//-------------------------------------------------------------------- +namespace clitk +{ - //==================================================================== - class MorphoMathGenericFilter: public itk::LightObject + template + class ITK_EXPORT MorphoMathGenericFilter: + public clitk::ImageToImageGenericFilter > { public: - //================================================ - typedef MorphoMathGenericFilter Self; - typedef itk::LightObject Superclass; - typedef itk::SmartPointer Pointer; + typedef MorphoMathGenericFilter Self; + typedef ImageToImageGenericFilter > Superclass; + typedef itk::SmartPointer Pointer; typedef itk::SmartPointer ConstPointer; - //================================================ + //-------------------------------------------------------------------- + MorphoMathGenericFilter(); itkNewMacro(Self); + itkTypeMacro(MorphoMathGenericFilter, LightObject); - //==================================================================== - // Set methods - void SetArgsInfo(const args_info_clitkMorphoMath a) - { - m_ArgsInfo=a; - m_InputFileName=m_ArgsInfo.input_arg; - m_Verbose=m_ArgsInfo.verbose_flag; - } + //-------------------------------------------------------------------- + void SetArgsInfo(const ArgsInfoType & a); + template + void SetOptionsFromArgsInfoToFilter(FilterType * f) ; - //==================================================================== - // Update - virtual void Update(); + //-------------------------------------------------------------------- + // Main function called each time the filter is updated + template + void UpdateWithInputImageType(); protected: - const char * GetNameOfClass() const { return "MorphoMathGenericFilter"; } - - //==================================================================== - // Constructor & Destructor - MorphoMathGenericFilter(); - ~MorphoMathGenericFilter(){;} - - //==================================================================== - //Protected member functions - template void UpdateWithDim(std::string PixelType); - template void UpdateWithDimAndPixelType(); - - - args_info_clitkMorphoMath m_ArgsInfo; - bool m_Verbose; - std::string m_InputFileName; - - }; + template void InitializeImageType(); + ArgsInfoType mArgsInfo; + + private: + MorphoMathGenericFilter(const Self&); //purposely not implemented + void operator=(const Self&); //purposely not implemented + }; // end class } // end namespace clitk + #ifndef ITK_MANUAL_INSTANTIATION #include "clitkMorphoMathGenericFilter.txx" #endif diff --git a/segmentation/clitkMorphoMathGenericFilter.txx b/segmentation/clitkMorphoMathGenericFilter.txx index cb641de..6692969 100755 --- a/segmentation/clitkMorphoMathGenericFilter.txx +++ b/segmentation/clitkMorphoMathGenericFilter.txx @@ -14,219 +14,112 @@ - BSD See included LICENSE.txt file - CeCILL-B http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html -======================================================================-====*/ -#ifndef clitkMorphoMathGenericFilter_txx -#define clitkMorphoMathGenericFilter_txx -/** - ================================================= - * @file clitkMorphoMathGenericFilter.txx - * @author Jef Vandemeulebroucke - * @date 5 May 2009 - * - * @brief - * - =================================================*/ - - -namespace clitk + ======================================================================-====*/ + +#include "clitkSegmentationUtils.h" + +//-------------------------------------------------------------------- +template +clitk::MorphoMathGenericFilter:: +MorphoMathGenericFilter(): + ImageToImageGenericFilter >("MorphMath") { + // Default values + cmdline_parser_clitkMorphoMath_init(&mArgsInfo); + InitializeImageType<2>(); + InitializeImageType<3>(); + InitializeImageType<4>(); +} +//-------------------------------------------------------------------- + + +//-------------------------------------------------------------------- +template +template +void clitk::MorphoMathGenericFilter:: +InitializeImageType() +{ + ADD_DEFAULT_IMAGE_TYPES(Dim); + //ADD_IMAGE_TYPE(Dim, short); // +} +//-------------------------------------------------------------------- + +//-------------------------------------------------------------------- +template +void clitk::MorphoMathGenericFilter:: +SetArgsInfo(const ArgsInfoType & a) +{ + mArgsInfo=a; + SetIOVerbose(mArgsInfo.verbose_flag); + if (mArgsInfo.imagetypes_flag) this->PrintAvailableImageTypes(); + if (mArgsInfo.input_given) AddInputFilename(mArgsInfo.input_arg); + if (mArgsInfo.output_given) AddOutputFilename(mArgsInfo.output_arg); +} +//-------------------------------------------------------------------- + + +//-------------------------------------------------------------------- +template +template +void clitk::MorphoMathGenericFilter:: +SetOptionsFromArgsInfoToFilter(FilterType * f) +{ + f->SetVerboseFlag(mArgsInfo.verbose_flag); + f->SetExtendSupportFlag(mArgsInfo.extend_flag); + f->SetOperationType(mArgsInfo.type_arg); + f->SetBackgroundValue(mArgsInfo.bg_arg); + f->SetForegroundValue(mArgsInfo.fg_arg); + f->SetBoundaryToForegroundFlag(mArgsInfo.bound_flag); + if (mArgsInfo.radius_given && mArgsInfo.radiusInMM_given) { + clitkExceptionMacro("Please give --radius OR --radiusInMM, not both."); + } - //============================================================================== - // Update with the number of dimensions - //============================================================================== - template - void - MorphoMathGenericFilter::UpdateWithDim(std::string PixelType) - { - if(PixelType == "short"){ - if (m_Verbose) std::cout << "Launching filter in "<< Dimension <<"D and signed short..." << std::endl; - UpdateWithDimAndPixelType(); - } - // else if(PixelType == "unsigned_short"){ - // if (m_Verbose) std::cout << "Launching filter in "<< Dimension <<"D and unsigned_short..." << std::endl; - // UpdateWithDimAndPixelType(); - // } - - else if (PixelType == "unsigned_char"){ - if (m_Verbose) std::cout << "Launching filter in "<< Dimension <<"D and unsigned_char..." << std::endl; - UpdateWithDimAndPixelType(); + if (mArgsInfo.radiusInMM_given) { + typename FilterType::PointType p; + if (mArgsInfo.radiusInMM_given) { + ConvertOptionMacro(mArgsInfo.radiusInMM, p, FilterType::ImageDimension, false); + f->SetRadiusInMM(p); } - - // else if (PixelType == "char"){ - // if (m_Verbose) std::cout << "Launching filter in "<< Dimension <<"D and signed_char..." << std::endl; - // UpdateWithDimAndPixelType(); - // } - else { - if (m_Verbose) std::cout << "Launching filter in "<< Dimension <<"D and float..." << std::endl; - UpdateWithDimAndPixelType(); + } + else { + typename FilterType::SizeType r; + if (mArgsInfo.radius_given) { + ConvertOptionMacro(mArgsInfo.radius, r, FilterType::ImageDimension, false); + f->SetRadius(r); } } - - - //============================================================================== - // Update with the pixel type - //============================================================================== - template - void - MorphoMathGenericFilter::UpdateWithDimAndPixelType() - { - - //--------------------------------- - // Define the images - //--------------------------------- - typedef float InternalPixelType; - typedef itk::Image InputImageType; - typedef itk::Image InternalImageType; - typedef itk::Image OutputImageType; - typedef itk::ImageFileReader FileReaderType; - typename FileReaderType::Pointer fileReader=FileReaderType::New(); - fileReader->SetFileName(m_InputFileName); - typedef itk::CastImageFilter InputCastImageFilterType; - typename InputCastImageFilterType::Pointer caster = InputCastImageFilterType::New(); - caster->SetInput(fileReader->GetOutput()); - caster->Update(); - typename InternalImageType::Pointer input =caster->GetOutput(); - - - //--------------------------------- - // Find the type of action - //--------------------------------- - typedef itk::ImageToImageFilter ImageFilterType; - typename ImageFilterType::Pointer filter; - - typedef itk::BinaryBallStructuringElement KernelType; - KernelType structuringElement; - typename InternalImageType::SizeType radius; - if (m_ArgsInfo.radius_given==Dimension) - for (unsigned int i=0;i FilterType; - typename FilterType::Pointer m = FilterType::New(); - m->SetBackgroundValue(m_ArgsInfo.bg_arg); - m->SetForegroundValue(m_ArgsInfo.fg_arg); - m->SetBoundaryToForeground(m_ArgsInfo.bound_flag); - m->SetKernel(structuringElement); - - filter=m; - if(m_Verbose) std::cout<<"Using the erode filter..."< FilterType; - typename FilterType::Pointer m = FilterType::New(); - m->SetBackgroundValue(m_ArgsInfo.bg_arg); - m->SetForegroundValue(m_ArgsInfo.fg_arg); - m->SetBoundaryToForeground(m_ArgsInfo.bound_flag); - m->SetKernel(structuringElement); - - filter=m; - if(m_Verbose) std::cout<<"Using the dilate filter..."< FilterType; - typename FilterType::Pointer m = FilterType::New(); - m->SetForegroundValue(m_ArgsInfo.fg_arg); - m->SetSafeBorder(m_ArgsInfo.bound_flag); - m->SetKernel(structuringElement); - - filter=m; - if(m_Verbose) std::cout<<"Using the closing filter..."< FilterType; - typename FilterType::Pointer m = FilterType::New(); - m->SetBackgroundValue(m_ArgsInfo.bg_arg); - m->SetForegroundValue(m_ArgsInfo.fg_arg); - m->SetKernel(structuringElement); - - filter=m; - if(m_Verbose) std::cout<<"Using the opening filter..."< FilterType; - typename FilterType::Pointer m = FilterType::New(); - m->SetBackgroundValue(m_ArgsInfo.bg_arg); - m->SetForegroundValue(m_ArgsInfo.fg_arg); - m->SetBoundaryToForeground(m_ArgsInfo.bound_flag); - m->SetKernel(structuringElement); - - filter=m; - if(m_Verbose) std::cout<<"Using the conditional erode filter..."< FilterType; - typename FilterType::Pointer m = FilterType::New(); - m->SetBackgroundValue(m_ArgsInfo.bg_arg); - m->SetForegroundValue(m_ArgsInfo.fg_arg); - m->SetBoundaryToForeground(m_ArgsInfo.bound_flag); - m->SetKernel(structuringElement); - - filter=m; - if(m_Verbose) std::cout<<"Using the conditional dilate filter..."<SetInput(input); +} + +//-------------------------------------------------------------------- +// Update with the number of dimensions and the pixeltype +//-------------------------------------------------------------------- +template +template +void clitk::MorphoMathGenericFilter:: +UpdateWithInputImageType() +{ + // Reading input + typename ImageType::Pointer input = this->template GetInput(0); + + // Create filter + typedef clitk::MorphoMathFilter FilterType; + typename FilterType::Pointer filter = FilterType::New(); - try - { - filter->Update(); - } - catch( itk::ExceptionObject & err ) - { - std::cerr << "ExceptionObject caught executing the filter!" << std::endl; - std::cerr << err << std::endl; - return; - } + // Set the filter (needed for example for threaded monitoring) + this->SetFilterBase(filter); + // Set global Options + filter->SetInput(input); + SetOptionsFromArgsInfoToFilter(filter); + + // Go ! + filter->Update(); + + // Write/Save results + typename ImageType::Pointer output = filter->GetOutput(); + this->template SetNextOutput(output); +} +//-------------------------------------------------------------------- - //--------------------------------- - // Write the output - //--------------------------------- - typedef itk::CastImageFilter< InternalImageType, OutputImageType > OutputCastImageFilterType; - typename OutputCastImageFilterType::Pointer oCaster = OutputCastImageFilterType::New(); - oCaster->SetInput(filter->GetOutput()); - typedef itk::ImageFileWriter FileWriterType; - typename FileWriterType::Pointer writer=FileWriterType::New(); - writer->SetInput(oCaster->GetOutput()); - writer->SetFileName(m_ArgsInfo.output_arg); - writer->Update(); - - } -}//end namespace - -#endif //#define clitkMorphoMathGenericFilter_txx