From: dsarrut Date: Fri, 22 Oct 2010 07:36:34 +0000 (+0000) Subject: new FillMask with itk filter X-Git-Tag: v1.2.0~324 X-Git-Url: https://git.creatis.insa-lyon.fr/pubgit/?a=commitdiff_plain;h=ed06e00b83f159033fdb9f0982a409833d9bffab;p=clitk.git new FillMask with itk filter --- diff --git a/segmentation/CMakeLists.txt b/segmentation/CMakeLists.txt index f97e7e5..4b6b2dc 100644 --- a/segmentation/CMakeLists.txt +++ b/segmentation/CMakeLists.txt @@ -19,11 +19,11 @@ IF(CLITK_BUILD_SEGMENTATION) TARGET_LINK_LIBRARIES(clitkConnectedComponentLabeling clitkCommon ITKIO) WRAP_GGO(clitkFillMask_GGO_C clitkFillMask.ggo) - ADD_EXECUTABLE(clitkFillMask clitkFillMask.cxx clitkFillMaskGenericFilter.cxx ${clitkFillMask_GGO_C}) + ADD_EXECUTABLE(clitkFillMask clitkFillMask.cxx ${clitkFillMask_GGO_C}) TARGET_LINK_LIBRARIES(clitkFillMask clitkCommon ITKIO) ADD_EXECUTABLE(clitkExtractPatient clitkExtractPatient.cxx ${clitkExtractPatient_GGO_C}) - TARGET_LINK_LIBRARIES(clitkExtractPatient clitkCommon ITKIO) + TARGET_LINK_LIBRARIES(clitkExtractPatient clitkCommon ITKIO clitkSegmentationGgoLib) ADD_EXECUTABLE(clitkExtractLung clitkExtractLung.cxx ${clitkExtractLung_GGO_C}) TARGET_LINK_LIBRARIES(clitkExtractLung clitkSegmentationGgoLib clitkCommon ITKIO) @@ -34,7 +34,7 @@ IF(CLITK_BUILD_SEGMENTATION) WRAP_GGO(clitkExtractBones_GGO_C clitkExtractBones.ggo) ADD_EXECUTABLE(clitkExtractBones clitkExtractBones.cxx ${clitkExtractBones_GGO_C}) - TARGET_LINK_LIBRARIES(clitkExtractBones clitkCommon ITKIO) + TARGET_LINK_LIBRARIES(clitkExtractBones clitkCommon ITKIO clitkSegmentationGgoLib) WRAP_GGO(clitkExtractMediastinum_GGO_C clitkExtractMediastinum.ggo) ADD_EXECUTABLE(clitkExtractMediastinum clitkExtractMediastinum.cxx ${clitkExtractMediastinum_GGO_C}) diff --git a/segmentation/clitkFillMask.cxx b/segmentation/clitkFillMask.cxx index 6fa4849..977747d 100644 --- a/segmentation/clitkFillMask.cxx +++ b/segmentation/clitkFillMask.cxx @@ -10,16 +10,6 @@ ------------------------------------------------------------------------*/ -/* ================================================= - * @file clitkFillMask.cxx - * @author - * @date - * - * @brief - * - ===================================================*/ - - // clitk #include "clitkFillMask_ggo.h" #include "clitkIO.h" @@ -34,12 +24,17 @@ int main(int argc, char * argv[]) { CLITK_INIT; // Filter - clitk::FillMaskGenericFilter::Pointer genericFilter=clitk::FillMaskGenericFilter::New(); + typedef clitk::FillMaskGenericFilter FilterType; + FilterType::Pointer filter = FilterType::New(); - genericFilter->SetArgsInfo(args_info); - genericFilter->Update(); + filter->SetArgsInfo(args_info); + + try { + filter->Update(); + } catch(std::runtime_error e) { + std::cerr << e.what() << std::endl; + } return EXIT_SUCCESS; -}// end main - +} // This is the end, my friend //-------------------------------------------------------------------- diff --git a/segmentation/clitkFillMaskGenericFilter.h b/segmentation/clitkFillMaskGenericFilter.h index 8a9b479..5a018bf 100644 --- a/segmentation/clitkFillMaskGenericFilter.h +++ b/segmentation/clitkFillMaskGenericFilter.h @@ -1,99 +1,63 @@ -#ifndef clitkFillMaskGenericFilter_h -#define clitkFillMaskGenericFilter_h +/*========================================================================= + Program: vv http://www.creatis.insa-lyon.fr/rio/vv -/* ================================================= - * @file clitkFillMaskGenericFilter.h - * @author - * @date - * - * @brief - * - ===================================================*/ + 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. -// clitk include -#include "clitkFillMask_ggo.h" -#include "clitkImageCommon.h" -#include "clitkExtractImageFilter.h" + It is distributed under dual licence -//itk include -#include "itkLightObject.h" -#include "itkJoinSeriesImageFilter.h" -#include "itkBinaryThresholdImageFilter.h" -#include "itkConnectedComponentImageFilter.h" -#include "itkRelabelComponentImageFilter.h" -#include "itkThresholdImageFilter.h" -#include "itkPermuteAxesImageFilter.h" -#include "itkExtractImageFilter.h" -#include "itkCastImageFilter.h" + - BSD See included LICENSE.txt file + - CeCILL-B http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html +======================================================================-====*/ +#ifndef CLITKFILLMASKGENERICFILTER_H +#define CLITKFILLMASKGENERICFILTER_H + +// clitk +#include "clitkImageToImageGenericFilter.h" +#include "clitkFillMaskFilter.h" + +//-------------------------------------------------------------------- namespace clitk { - - class ITK_EXPORT FillMaskGenericFilter : public itk::LightObject + template + class ITK_EXPORT FillMaskGenericFilter : + public ImageToImageGenericFilter > { public: - //---------------------------------------- - // ITK - //---------------------------------------- - typedef FillMaskGenericFilter Self; - typedef itk::LightObject Superclass; - 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( FillMaskGenericFilter, LightObject ); - - - //---------------------------------------- - // Typedefs - //---------------------------------------- - - - //---------------------------------------- - // Set & Get - //---------------------------------------- - void SetArgsInfo(const args_info_clitkFillMask & a) - { - m_ArgsInfo=a; - m_Verbose=m_ArgsInfo.verbose_flag; - m_InputFileName=m_ArgsInfo.input_arg; - } - - - //---------------------------------------- - // Update - //---------------------------------------- - void Update(); - - protected: - - //---------------------------------------- - // Constructor & Destructor - //---------------------------------------- + //-------------------------------------------------------------------- FillMaskGenericFilter(); - ~FillMaskGenericFilter() {}; - - - //---------------------------------------- - // Templated members - //---------------------------------------- - template void UpdateWithPixelType(); + //-------------------------------------------------------------------- + typedef FillMaskGenericFilter Self; + typedef ImageToImageGenericFilter Superclass; + typedef itk::SmartPointer Pointer; + typedef itk::SmartPointer ConstPointer; + + //-------------------------------------------------------------------- + itkNewMacro(Self); + itkTypeMacro(FillMaskGenericFilter, LightObject); - //---------------------------------------- - // Data members - //---------------------------------------- - args_info_clitkFillMask m_ArgsInfo; - bool m_Verbose; - std::string m_InputFileName; + //-------------------------------------------------------------------- + void SetArgsInfo(const ArgsInfoType & a); - }; + //-------------------------------------------------------------------- + // Main function called each time the filter is updated + template + void UpdateWithInputImageType(); + protected: + template void InitializeImageType(); + ArgsInfoType mArgsInfo; + }; // end class + //-------------------------------------------------------------------- } // end namespace clitk @@ -101,4 +65,4 @@ namespace clitk #include "clitkFillMaskGenericFilter.txx" #endif -#endif // #define clitkFillMaskGenericFilter_h +#endif // #define CLITKFILLMASKGENERICFILTER_H diff --git a/segmentation/clitkFillMaskGenericFilter.txx b/segmentation/clitkFillMaskGenericFilter.txx index d723e10..3450436 100644 --- a/segmentation/clitkFillMaskGenericFilter.txx +++ b/segmentation/clitkFillMaskGenericFilter.txx @@ -1,201 +1,87 @@ -#ifndef clitkFillMaskGenericFilter_txx -#define clitkFillMaskGenericFilter_txx +/*========================================================================= + Program: vv http://www.creatis.insa-lyon.fr/rio/vv -/* ================================================= - * @file clitkFillMaskGenericFilter.txx - * @author - * @date - * - * @brief - * - ===================================================*/ + 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. -namespace clitk -{ + It is distributed under dual licence - //------------------------------------------------------------------- - // Update with the pixeltype - //------------------------------------------------------------------- - template - void - FillMaskGenericFilter::UpdateWithPixelType() - { - // Dim & Pix - const unsigned int Dimension=3; - typedef int InternalPixelType; + - BSD See included LICENSE.txt file + - CeCILL-B http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html + ======================================================================-====*/ - // ImageTypes - typedef itk::Image InputImageType; - typedef itk::Image InternalImageType; - typedef itk::Image OutputImageType; - - // Read the input - typedef itk::ImageFileReader InputReaderType; - typename InputReaderType::Pointer reader = InputReaderType::New(); - reader->SetFileName( m_InputFileName); - reader->Update(); - typename InputImageType::Pointer input= reader->GetOutput(); - - // Read the directions over which to fill holes - std::vector direction; - if (m_ArgsInfo.dir_given) - for ( unsigned int i=0;i InputCastImageFilterType; - typename InputCastImageFilterType::Pointer inputCaster= InputCastImageFilterType::New(); - inputCaster->SetInput(input); - inputCaster->Update(); +#include "clitkImageCommon.h" + +//-------------------------------------------------------------------- +template +clitk::FillMaskGenericFilter::FillMaskGenericFilter(): + ImageToImageGenericFilter("FillMask") +{ + this->SetFilterBase(NULL); + InitializeImageType<3>(); +} +//-------------------------------------------------------------------- - //---------------------------------------- - // Loop over directions - //---------------------------------------- - typename InternalImageType::Pointer output=inputCaster->GetOutput(); - for (unsigned int i=0; i ImageSliceType; - typedef itk::Image MaskSliceType; - typename InternalImageType::RegionType region3D= input->GetLargestPossibleRegion(); - typename InternalImageType::RegionType::SizeType size3D= region3D.GetSize(); - typename InternalImageType::RegionType::SizeType size2D=size3D; - size2D[direction[i]]=0; - typename InternalImageType::IndexType start2D; - start2D.Fill(0); - typename InternalImageType::RegionType desiredRegion; - desiredRegion.SetSize( size2D ); - desiredRegion.SetIndex( start2D ); - - // Extract and Join - typedef itk::ExtractImageFilter ExtractImageFilterType; - typedef itk::JoinSeriesImageFilter JoinSeriesFilterType; - typename JoinSeriesFilterType::Pointer joinFilter=JoinSeriesFilterType::New(); - joinFilter->SetSpacing(input->GetSpacing()[direction[i]]); - - //---------------------------------------- - // Run over the sliceIndexs - // ---------------------------------------- - for(unsigned int sliceIndex=0; sliceIndex SetInput(output); - start2D[direction[i]]=sliceIndex; - desiredRegion.SetIndex( start2D ); - extractFilter->SetExtractionRegion( desiredRegion ); - extractFilter->Update( ); - typename ImageSliceType::Pointer slice= extractFilter->GetOutput(); +//-------------------------------------------------------------------- +template +template +void clitk::FillMaskGenericFilter::InitializeImageType() +{ + ADD_IMAGE_TYPE(Dim, uchar); + // ADD_IMAGE_TYPE(Dim, int); + // ADD_IMAGE_TYPE(Dim, float); +} +//-------------------------------------------------------------------- + - // Binarize the image (Before: OBJECT!=0, rest=0, After: object=1, rest=0 ) - typedef itk::BinaryThresholdImageFilter BinarizeFilterType; - typename BinarizeFilterType::Pointer binarizeFilter=BinarizeFilterType::New(); - binarizeFilter->SetInput(slice); - binarizeFilter->SetUpperThreshold(0); - binarizeFilter->SetOutsideValue(0); - binarizeFilter->SetInsideValue(1); - // writeImage(binarizeFilter->GetOutput(),"/home/jef/tmp/input.mhd"); +//-------------------------------------------------------------------- +template +void clitk::FillMaskGenericFilter::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); +} +//-------------------------------------------------------------------- - // Perform connected labelling on the slice (body+air=0 ) - typedef itk::ConnectedComponentImageFilter ConnectFilterType; - typename ConnectFilterType::Pointer connectFilter=ConnectFilterType::New(); - connectFilter->SetInput(binarizeFilter->GetOutput()); - connectFilter->SetBackgroundValue(0); - connectFilter->SetFullyConnected(false); - //connectFilter->Update(); - //writeImage(connectFilter->GetOutput(),"/home/jef/tmp/connect.mhd"); - - // Sort the labels - typedef itk::RelabelComponentImageFilter RelabelFilterType; - typename RelabelFilterType::Pointer relabelFilter=RelabelFilterType::New(); - relabelFilter->SetInput(connectFilter->GetOutput()); - //relabelFilter->Update(); - //writeImage(relabelFilter->GetOutput(),"/home/jef/tmp/label.mhd"); - - // Keep the first - typedef itk::ThresholdImageFilter ThresholdFilterType; - typename ThresholdFilterType::Pointer thresholdFilter=ThresholdFilterType::New(); - thresholdFilter->SetInput(relabelFilter->GetOutput()); - thresholdFilter->SetUpper(1); - thresholdFilter->SetOutsideValue(0); - // thresholdFilter->Update(); - // writeImage(thresholdFilter->GetOutput(),"/home/jef/tmp/bin.mhd"); - // Invert the labels (lung 1, rest 0) - typename BinarizeFilterType::Pointer switchFilter=BinarizeFilterType::New(); - switchFilter->SetInput(thresholdFilter->GetOutput()); - switchFilter->SetUpperThreshold(0); - switchFilter->SetOutsideValue(0); - switchFilter->SetInsideValue(1); - switchFilter->Update(); - //writeImage(switchFilter->GetOutput(),"/home/jef/tmp/inv_bin.mhd"); - - //Join - joinFilter->SetInput( sliceIndex, switchFilter->GetOutput()); - } - - // Join to a 3D image - if (m_Verbose) std::cout<<"Joining the slices..."<Update(); - - // Permute the axes to reset to orientation - typedef itk::PermuteAxesImageFilter PermuteFilterType; - typename PermuteFilterType::Pointer permuteFilter=PermuteFilterType::New(); - permuteFilter->SetInput(joinFilter->GetOutput()); - typename PermuteFilterType::PermuteOrderArrayType order; - order[direction[i]]=2; - if( direction[i]==2) - { - order[0]=0; - order[1]=1; - } - else if ( direction[i]==1) - { - order[0]=0; - order[2]=1; - } - else if (direction[i]==0) - { - order[1]=0; - order[2]=1; - } - permuteFilter->SetOrder(order); - permuteFilter->Update(); - output =permuteFilter->GetOutput(); - - // Set the image direction to the input one - output->SetDirection(input->GetDirection()); - output->SetOrigin(input->GetOrigin()); - } +//-------------------------------------------------------------------- +// Update with the number of dimensions and the pixeltype +//-------------------------------------------------------------------- +template +template +void clitk::FillMaskGenericFilter::UpdateWithInputImageType() +{ + // Reading input + typename ImageType::Pointer input = this->template GetInput(0); + // Create filter + typedef clitk::FillMaskFilter FilterType; + typename FilterType::Pointer filter = FilterType::New(); - // Cast - typedef itk::CastImageFilter OutputCastImageFilterType; - typename OutputCastImageFilterType::Pointer outputCaster =OutputCastImageFilterType::New(); - outputCaster->SetInput(output); - - // Output - typedef itk::ImageFileWriter WriterType; - typename WriterType::Pointer writer = WriterType::New(); - writer->SetFileName(m_ArgsInfo.output_arg); - writer->SetInput(outputCaster->GetOutput()); - writer->Update(); - } + // Set Options + filter->SetInput(input); + filter->SetOptionsFromArgsInfo(mArgsInfo); -}//end clitk + // Go ! + filter->Update(); + + // Write/Save results + typename ImageType::Pointer output = filter->GetOutput(); + this->template SetNextOutput(output); +} +//-------------------------------------------------------------------- -#endif //#define clitkFillMaskGenericFilter_txx +#endif //#define CLITKFILLMASKGENERICFILTER_TXX