======================================================================-====*/
#ifndef clitkMorphoMath_cxx
#define clitkMorphoMath_cxx
-/**
- =================================================
- * @file clitkMorphoMath.cxx
- * @author Jef Vandemeulebroucke <jefvdmb@gmail.com>
- * @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<args_info_clitkMorphoMath> 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
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
--- /dev/null
+/*=========================================================================
+ 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 <itkLightObject.h>
+#include <itkBinaryErodeImageFilter.h>
+#include <itkBinaryDilateImageFilter.h>
+#include <itkBinaryMorphologicalClosingImageFilter.h>
+#include <itkBinaryMorphologicalOpeningImageFilter.h>
+#include <itkBinaryBallStructuringElement.h>
+#include <itkCastImageFilter.h>
+
+namespace clitk {
+
+ //--------------------------------------------------------------------
+ template<class ImageType>
+ class ITK_EXPORT MorphoMathFilter:
+ public clitk::FilterBase,
+ public itk::ImageToImageFilter<ImageType, ImageType>
+ {
+
+ public:
+ /** Standard class typedefs. */
+ typedef itk::ImageToImageFilter<ImageType, ImageType> Superclass;
+ typedef MorphoMathFilter<ImageType> Self;
+ typedef itk::SmartPointer<Self> Pointer;
+ typedef itk::SmartPointer<const Self> 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<InternalPixelType, ImageType::ImageDimension> InternalImageType;
+
+ /** ImageDimension constants */
+ itkStaticConstMacro(ImageDimension, unsigned int, ImageType::ImageDimension);
+ typedef itk::Image<float, ImageDimension> 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
--- /dev/null
+/*=========================================================================
+ 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<class ImageType>
+clitk::MorphoMathFilter<ImageType>::MorphoMathFilter():
+ clitk::FilterBase(),
+ itk::ImageToImageFilter<ImageType, ImageType>()
+{
+ this->SetNumberOfRequiredInputs(1);
+ SetBackgroundValue(0);
+ SetForegroundValue(1);
+ SetOperationType(0);
+ SizeType p;
+ p.Fill(1);
+ SetRadius(p);
+ SetBoundaryToForegroundFlag(false);
+}
+//--------------------------------------------------------------------
+
+
+//--------------------------------------------------------------------
+template<class ImageType>
+clitk::MorphoMathFilter<ImageType>::~MorphoMathFilter()
+{
+ // Nothing
+}
+//--------------------------------------------------------------------
+
+
+//--------------------------------------------------------------------
+template<class ImageType>
+void clitk::MorphoMathFilter<ImageType>::
+SetRadiusInMM(PointType & p)
+{
+ m_RadiusInMM = p;
+ m_RadiusInMMIsSet = true;
+ m_RadiusIsSet = false;
+}
+//--------------------------------------------------------------------
+
+
+//--------------------------------------------------------------------
+template<class ImageType>
+void clitk::MorphoMathFilter<ImageType>::
+SetRadius(SizeType & p)
+{
+ m_Radius = p;
+ m_RadiusIsSet = true;
+ m_RadiusInMMIsSet = false;
+}
+//--------------------------------------------------------------------
+
+
+//--------------------------------------------------------------------
+template<class ImageType>
+void clitk::MorphoMathFilter<ImageType>::
+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 <class ImageType>
+void
+clitk::MorphoMathFilter<ImageType>::
+GenerateInputRequestedRegion()
+{
+ // Call default
+ itk::ImageToImageFilter<ImageType, ImageType>::GenerateInputRequestedRegion();
+ // Get input pointers and set requested region to common region
+ ImagePointer input1 = dynamic_cast<ImageType*>(itk::ProcessObject::GetInput(0));
+ input1->SetRequestedRegion(input1->GetLargestPossibleRegion());
+}
+//--------------------------------------------------------------------
+
+
+//--------------------------------------------------------------------
+template <class ImageType>
+void
+clitk::MorphoMathFilter<ImageType>::
+GenerateOutputInformation()
+{
+ //---------------------------------
+ // Define the images
+ //---------------------------------
+ ImagePointer m_input = dynamic_cast<ImageType*>(itk::ProcessObject::GetInput(0));
+
+ //---------------------------------
+ // Cast into internalimagetype
+ //---------------------------------
+ typedef itk::CastImageFilter<ImageType, InternalImageType> 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; i<ImageType::ImageDimension; i++) {
+ m_Radius[i] = (uint)lrint(m_RadiusInMM[i]/input->GetSpacing()[i]);
+ }
+ }
+
+ //---------------------------------
+ // Extend the image if needed
+ //---------------------------------
+ if (GetExtendSupportFlag()) {
+ typedef itk::ConstantPadImageFilter<InternalImageType, InternalImageType> 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 <class ImageType>
+void
+clitk::MorphoMathFilter<ImageType>::
+GenerateData()
+{
+ //---------------------------------
+ // Build kernel
+ //---------------------------------
+ typedef itk::BinaryBallStructuringElement<PixelType,ImageDimension> 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<InternalImageType, InternalImageType> ImageFilterType;
+ typename ImageFilterType::Pointer filter;
+ switch(m_OperationType)
+ {
+ case Erode: {
+ typedef itk::BinaryErodeImageFilter<InternalImageType, InternalImageType , KernelType> 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..."<<std::endl;
+ break;
+ }
+
+ case Dilate:
+ {
+ typedef itk::BinaryDilateImageFilter<InternalImageType, InternalImageType , KernelType> 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..."<<std::endl;
+ break;
+ }
+
+ case Close:
+ {
+ typedef itk::BinaryMorphologicalClosingImageFilter<InternalImageType, InternalImageType , KernelType> 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..."<<std::endl;
+ break;
+ }
+
+ case Open:
+ {
+ typedef itk::BinaryMorphologicalOpeningImageFilter<InternalImageType, InternalImageType , KernelType> 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..."<<std::endl;
+ break;
+ }
+
+ case CondErode:
+ {
+ typedef clitk::ConditionalBinaryErodeImageFilter<InternalImageType, InternalImageType , KernelType> 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..."<<std::endl;
+ break;
+ }
+
+ case CondDilate:
+ {
+ typedef clitk::ConditionalBinaryDilateImageFilter<InternalImageType, InternalImageType , KernelType> 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..."<<std::endl;
+ break;
+ }
+
+ }
+
+
+ //---------------------------------
+ // Execute the filter
+ //---------------------------------
+ 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());
+}
+//--------------------------------------------------------------------
+
+
- 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 <jef@creatis.insa-lyon.fr>
- * @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 ArgsInfoType>
+ class ITK_EXPORT MorphoMathGenericFilter:
+ public clitk::ImageToImageGenericFilter<MorphoMathGenericFilter<ArgsInfoType> >
{
public:
- //================================================
- typedef MorphoMathGenericFilter Self;
- typedef itk::LightObject Superclass;
- typedef itk::SmartPointer<Self> Pointer;
+ typedef MorphoMathGenericFilter<ArgsInfoType> Self;
+ typedef ImageToImageGenericFilter<MorphoMathGenericFilter<ArgsInfoType> > Superclass;
+ typedef itk::SmartPointer<Self> Pointer;
typedef itk::SmartPointer<const Self> 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<class FilterType>
+ void SetOptionsFromArgsInfoToFilter(FilterType * f) ;
- //====================================================================
- // Update
- virtual void Update();
+ //--------------------------------------------------------------------
+ // Main function called each time the filter is updated
+ template<class ImageType>
+ void UpdateWithInputImageType();
protected:
- const char * GetNameOfClass() const { return "MorphoMathGenericFilter"; }
-
- //====================================================================
- // Constructor & Destructor
- MorphoMathGenericFilter();
- ~MorphoMathGenericFilter(){;}
-
- //====================================================================
- //Protected member functions
- template <unsigned int Dimension> void UpdateWithDim(std::string PixelType);
- template <unsigned int Dimension, class PixelType> void UpdateWithDimAndPixelType();
-
-
- args_info_clitkMorphoMath m_ArgsInfo;
- bool m_Verbose;
- std::string m_InputFileName;
-
- };
+ template<unsigned int Dim> 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
- 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 <jef@creatis.insa-lyon.fr>
- * @date 5 May 2009
- *
- * @brief
- *
- =================================================*/
-
-
-namespace clitk
+ ======================================================================-====*/
+
+#include "clitkSegmentationUtils.h"
+
+//--------------------------------------------------------------------
+template<class ArgsInfoType>
+clitk::MorphoMathGenericFilter<ArgsInfoType>::
+MorphoMathGenericFilter():
+ ImageToImageGenericFilter<MorphoMathGenericFilter<ArgsInfoType> >("MorphMath")
{
+ // Default values
+ cmdline_parser_clitkMorphoMath_init(&mArgsInfo);
+ InitializeImageType<2>();
+ InitializeImageType<3>();
+ InitializeImageType<4>();
+}
+//--------------------------------------------------------------------
+
+
+//--------------------------------------------------------------------
+template<class ArgsInfoType>
+template<unsigned int Dim>
+void clitk::MorphoMathGenericFilter<ArgsInfoType>::
+InitializeImageType()
+{
+ ADD_DEFAULT_IMAGE_TYPES(Dim);
+ //ADD_IMAGE_TYPE(Dim, short); //
+}
+//--------------------------------------------------------------------
+
+//--------------------------------------------------------------------
+template<class ArgsInfoType>
+void clitk::MorphoMathGenericFilter<ArgsInfoType>::
+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<class ArgsInfoType>
+template<class FilterType>
+void clitk::MorphoMathGenericFilter<ArgsInfoType>::
+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<unsigned int Dimension>
- 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<Dimension, signed short>();
- }
- // else if(PixelType == "unsigned_short"){
- // if (m_Verbose) std::cout << "Launching filter in "<< Dimension <<"D and unsigned_short..." << std::endl;
- // UpdateWithDimAndPixelType<Dimension, unsigned short>();
- // }
-
- else if (PixelType == "unsigned_char"){
- if (m_Verbose) std::cout << "Launching filter in "<< Dimension <<"D and unsigned_char..." << std::endl;
- UpdateWithDimAndPixelType<Dimension, unsigned char>();
+ 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<Dimension, signed char>();
- // }
- else {
- if (m_Verbose) std::cout << "Launching filter in "<< Dimension <<"D and float..." << std::endl;
- UpdateWithDimAndPixelType<Dimension, float>();
+ }
+ 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 <unsigned int Dimension, class InputPixelType>
- void
- MorphoMathGenericFilter::UpdateWithDimAndPixelType()
- {
-
- //---------------------------------
- // Define the images
- //---------------------------------
- typedef float InternalPixelType;
- typedef itk::Image<InputPixelType, Dimension> InputImageType;
- typedef itk::Image<InternalPixelType, Dimension> InternalImageType;
- typedef itk::Image<InputPixelType, Dimension> OutputImageType;
- typedef itk::ImageFileReader<InputImageType> FileReaderType;
- typename FileReaderType::Pointer fileReader=FileReaderType::New();
- fileReader->SetFileName(m_InputFileName);
- typedef itk::CastImageFilter<InputImageType, InternalImageType> 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<InternalImageType, InternalImageType> ImageFilterType;
- typename ImageFilterType::Pointer filter;
-
- typedef itk::BinaryBallStructuringElement<InputPixelType,Dimension > KernelType;
- KernelType structuringElement;
- typename InternalImageType::SizeType radius;
- if (m_ArgsInfo.radius_given==Dimension)
- for (unsigned int i=0;i<Dimension;i++)
- {radius[i]=m_ArgsInfo.radius_arg[i];}
- else
- for (unsigned int i=0;i<Dimension;i++)
- radius[i]=m_ArgsInfo.radius_arg[0];
-
- structuringElement.SetRadius(radius);
- structuringElement.CreateStructuringElement();
-
- switch(m_ArgsInfo.type_arg)
- {
-
- case 0:
- {
- typedef itk::BinaryErodeImageFilter<InternalImageType, InternalImageType , KernelType> 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..."<<std::endl;
- break;
- }
-
- case 1:
- {
- typedef itk::BinaryDilateImageFilter<InternalImageType, InternalImageType , KernelType> 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..."<<std::endl;
- break;
- }
-
- case 2:
- {
- typedef itk::BinaryMorphologicalClosingImageFilter<InternalImageType, InternalImageType , KernelType> 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..."<<std::endl;
- break;
- }
-
- case 3:
- {
- typedef itk::BinaryMorphologicalOpeningImageFilter<InternalImageType, InternalImageType , KernelType> 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..."<<std::endl;
- break;
- }
-
- case 4:
- {
- typedef clitk::ConditionalBinaryErodeImageFilter<InternalImageType, InternalImageType , KernelType> 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..."<<std::endl;
- break;
- }
-
- case 5:
- {
- typedef clitk::ConditionalBinaryDilateImageFilter<InternalImageType, InternalImageType , KernelType> 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..."<<std::endl;
- break;
- }
-
- }
-
-
- //---------------------------------
- // Execute the filter
- //---------------------------------
- filter->SetInput(input);
+}
+
+//--------------------------------------------------------------------
+// Update with the number of dimensions and the pixeltype
+//--------------------------------------------------------------------
+template<class ArgsInfoType>
+template<class ImageType>
+void clitk::MorphoMathGenericFilter<ArgsInfoType>::
+UpdateWithInputImageType()
+{
+ // Reading input
+ typename ImageType::Pointer input = this->template GetInput<ImageType>(0);
+
+ // Create filter
+ typedef clitk::MorphoMathFilter<ImageType> 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<FilterType>(filter);
+
+ // Go !
+ filter->Update();
+
+ // Write/Save results
+ typename ImageType::Pointer output = filter->GetOutput();
+ this->template SetNextOutput<ImageType>(output);
+}
+//--------------------------------------------------------------------
- //---------------------------------
- // Write the output
- //---------------------------------
- typedef itk::CastImageFilter< InternalImageType, OutputImageType > OutputCastImageFilterType;
- typename OutputCastImageFilterType::Pointer oCaster = OutputCastImageFilterType::New();
- oCaster->SetInput(filter->GetOutput());
- typedef itk::ImageFileWriter<OutputImageType> 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