From 523a9f961d83eaed5ec3365a07b9f6be35b538f0 Mon Sep 17 00:00:00 2001 From: dsarrut Date: Fri, 22 Oct 2010 07:33:59 +0000 Subject: [PATCH 1/1] new CropImage with AutoCrop and CropLike --- itk/clitkAutoCropFilter.h | 8 +- itk/clitkAutoCropFilter.txx | 4 +- itk/clitkCropLikeImageFilter.h | 95 ++++++++++ itk/clitkCropLikeImageFilter.txx | 179 ++++++++++++++++++ tools/CMakeLists.txt | 12 +- tools/clitkCropImage.cxx | 35 ++-- tools/clitkCropImage.ggo | 24 ++- tools/clitkCropImageGenericFilter.h | 107 ++++------- tools/clitkCropImageGenericFilter.txx | 254 +++++++++++++------------- 9 files changed, 482 insertions(+), 236 deletions(-) create mode 100644 itk/clitkCropLikeImageFilter.h create mode 100644 itk/clitkCropLikeImageFilter.txx diff --git a/itk/clitkAutoCropFilter.h b/itk/clitkAutoCropFilter.h index d060a97..f4f35bc 100644 --- a/itk/clitkAutoCropFilter.h +++ b/itk/clitkAutoCropFilter.h @@ -35,10 +35,10 @@ namespace clitk { public: /** Standard class typedefs. */ - typedef AutoCropFilter Self; - typedef itk::ImageToImageFilter Superclass; - typedef itk::SmartPointer Pointer; - typedef itk::SmartPointer ConstPointer; + typedef AutoCropFilter Self; + typedef itk::ImageToImageFilter Superclass; + typedef itk::SmartPointer Pointer; + typedef itk::SmartPointer ConstPointer; /** Method for creation through the object factory. */ itkNewMacro(Self); diff --git a/itk/clitkAutoCropFilter.txx b/itk/clitkAutoCropFilter.txx index 743fe13..145cc36 100644 --- a/itk/clitkAutoCropFilter.txx +++ b/itk/clitkAutoCropFilter.txx @@ -132,8 +132,8 @@ namespace clitk { ImageConstPointer input = dynamic_cast(itk::ProcessObject::GetInput(0)); // Extract the region with RegionOfInterestImageFilter or ExtractImageFilter ? - // The first is when reducing the nb of dimension (index always zero) - // The second keep index + // The second is when reducing the nb of dimension (index always zero) + // The first keep index typedef itk::RegionOfInterestImageFilter CropFilterType; //typedef itk::ExtractImageFilter CropFilterType; diff --git a/itk/clitkCropLikeImageFilter.h b/itk/clitkCropLikeImageFilter.h new file mode 100644 index 0000000..4ceadc1 --- /dev/null +++ b/itk/clitkCropLikeImageFilter.h @@ -0,0 +1,95 @@ +/*========================================================================= + 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 CLITKCROPLIKEIMAGEFILTER_H +#define CLITKCROPLIKEIMAGEFILTER_H + +#include + +namespace clitk { + + //-------------------------------------------------------------------- + /* + Perform various cropping operation on a image + */ + //-------------------------------------------------------------------- + + template + class ITK_EXPORT CropLikeImageFilter: + public itk::ImageToImageFilter { + + public: + /** Standard class typedefs. */ + typedef CropLikeImageFilter Self; + typedef itk::ImageToImageFilter 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(CropLikeImageFilter, ImageToImageFilter); + + /** Some convenient typedefs. */ + typedef typename ImageType::ConstPointer ImageConstPointer; + typedef typename ImageType::Pointer ImagePointer; + typedef typename ImageType::PixelType PixelType; + typedef typename ImageType::RegionType RegionType; + typedef typename ImageType::PointType PointType; + + /** Input image to crop */ + void SetInput(const ImageType * image); + + /** Image filename for Crop Like */ + void SetCropLikeFilename(std::string f); + void SetCropLikeImage(const itk::ImageBase * like); + void SetCropLikeImage(const itk::ImageBase * like, int axe); + + /** ImageDimension constants */ + itkStaticConstMacro(ImageDimension, unsigned int, ImageType::ImageDimension); + + protected: + CropLikeImageFilter(); + virtual ~CropLikeImageFilter() {} + + virtual void GenerateOutputInformation(); + virtual void GenerateData(); + + RegionType m_Region; + std::string m_LikeFilename; + bool m_LikeFilenameIsGiven; + PointType m_Origin; + const itk::ImageBase * m_LikeImage; + std::vector m_CropAlongThisDimension; + + private: + CropLikeImageFilter(const Self&); //purposely not implemented + void operator=(const Self&); //purposely not implemented + + }; // end class + //-------------------------------------------------------------------- + +} // end namespace clitk +//-------------------------------------------------------------------- + +#ifndef ITK_MANUAL_INSTANTIATION +#include "clitkCropLikeImageFilter.txx" +#endif + +#endif // # CROPLIKEIMAGEFILTER diff --git a/itk/clitkCropLikeImageFilter.txx b/itk/clitkCropLikeImageFilter.txx new file mode 100644 index 0000000..90190c6 --- /dev/null +++ b/itk/clitkCropLikeImageFilter.txx @@ -0,0 +1,179 @@ +/*========================================================================= + 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 CLITKCROPLIKEIMAGEFILTER_TXX +#define CLITKCROPLIKEIMAGEFILTER_TXX + +// clitk +#include "clitkCommon.h" + +// itk +#include "itkRegionOfInterestImageFilter.h" + + +//-------------------------------------------------------------------- +template +clitk::CropLikeImageFilter:: +CropLikeImageFilter():itk::ImageToImageFilter() { + this->SetNumberOfRequiredInputs(1); + m_LikeImage = NULL; + m_LikeFilenameIsGiven = false; + m_CropAlongThisDimension.resize(ImageType::ImageDimension); + for(uint i=0; i +void +clitk::CropLikeImageFilter:: +SetCropLikeFilename(std::string f) +{ + m_LikeFilename = f; + m_LikeFilenameIsGiven = true; +} +//-------------------------------------------------------------------- + + +//-------------------------------------------------------------------- +template +void +clitk::CropLikeImageFilter:: +SetCropLikeImage(const itk::ImageBase * like) +{ + m_LikeImage = like; +} +//-------------------------------------------------------------------- + + +//-------------------------------------------------------------------- +template +void +clitk::CropLikeImageFilter:: +SetCropLikeImage(const itk::ImageBase * like, int axe) +{ + m_LikeImage = like; + for(uint i=0; i +void +clitk::CropLikeImageFilter:: +SetInput(const ImageType * image) { + // Process object is not const-correct so the const casting is required. + this->SetNthInput(0, const_cast( image )); +} +//-------------------------------------------------------------------- + + +//-------------------------------------------------------------------- +template +void +clitk::CropLikeImageFilter:: +GenerateOutputInformation() { + // Get input pointers + ImageConstPointer input = dynamic_cast(itk::ProcessObject::GetInput(0)); + + // Get output pointer + ImagePointer output = this->GetOutput(0); + + // Get input info + typename ImageType::SizeType likeSize; + typename ImageType::IndexType likeStart; + typename ImageType::PointType likeOrigin; + typename ImageType::SpacingType likeSpacing; + if (m_LikeImage) { + likeSize = m_LikeImage->GetLargestPossibleRegion().GetSize(); + likeStart = m_LikeImage->GetLargestPossibleRegion().GetIndex(); + likeOrigin = m_LikeImage->GetOrigin(); + likeSpacing = m_LikeImage->GetSpacing(); + } + else { + if (m_LikeFilenameIsGiven) { + itk::ImageIOBase::Pointer header = readImageHeader(m_LikeFilename); + for(unsigned int i=0; iGetIORegion().GetSize()[i]; //GetDimensions(i); + likeStart[i] = header->GetIORegion().GetIndex()[i]; + likeOrigin[i] = header->GetOrigin(i); + likeSpacing[i] = header->GetSpacing(i); + } + } + else { + clitkExceptionMacro("You should provide SetCropLikeFilename or SetCropLike to CropLikeImageFilter"); + } + } + + // Compute region + typename ImageType::SizeType size; + typename ImageType::IndexType start; + for(unsigned int i=0; iGetLargestPossibleRegion().GetSize()[i]; + ol = input->GetOrigin()[i]; + } + double oi = input->GetOrigin()[i]; + start[i] = lrint((ol-oi)/input->GetSpacing()[i]); + m_Origin[i] = likeOrigin[i]; + if (likeSpacing[i] != input->GetSpacing()[i]) { + clitkExceptionMacro("Images must have the same spacing, but input's spacing(" << i + <<") is " << input->GetSpacing()[i] << " while like's spacing(" << i + << ") is " << likeSpacing[i] << "."); + } + } + + m_Region.SetSize(size); + m_Region.SetIndex(start); + output->SetRegions(m_Region); + output->SetSpacing(input->GetSpacing()); +} +//-------------------------------------------------------------------- + +//-------------------------------------------------------------------- +template +void +clitk::CropLikeImageFilter:: +GenerateData() { + // Get input pointers + ImageConstPointer input = dynamic_cast(itk::ProcessObject::GetInput(0)); + + typedef itk::RegionOfInterestImageFilter CropFilterType; + typename CropFilterType::Pointer cropFilter = CropFilterType::New(); + cropFilter->SetInput(input); + cropFilter->SetReleaseDataFlag(this->GetReleaseDataFlag()); + cropFilter->SetRegionOfInterest(m_Region); + cropFilter->Update(); + + // Get (graft) output (SetNthOutput does not fit here because of Origin). + this->GraftOutput(cropFilter->GetOutput()); +} +//-------------------------------------------------------------------- + + +#endif //#define CLITKAUTOCROPFILTER diff --git a/tools/CMakeLists.txt b/tools/CMakeLists.txt index 10c88e2..8b8e785 100644 --- a/tools/CMakeLists.txt +++ b/tools/CMakeLists.txt @@ -3,7 +3,7 @@ INCLUDE(${PROJECT_SOURCE_DIR}/cmake/common.cmake) #========================================================= #Add your ggo files to this list if they are used by vv FOREACH(clitkTool clitkAffineTransform clitkBinarizeImage clitkImageArithm - clitkMedianImageFilter clitkImageConvert clitkImageResample) + clitkMedianImageFilter clitkImageConvert clitkImageResample clitkCropImage) WRAP_GGO(${clitkTool}_GGO_C ${clitkTool}.ggo) SET(GGO_C_FILES ${GGO_C_FILES} ${${clitkTool}_GGO_C}) ENDFOREACH(clitkTool) @@ -112,9 +112,9 @@ IF (CLITK_BUILD_TOOLS) ADD_EXECUTABLE(clitkMinMaxMask clitkMinMaxMask.cxx ${clitkMinMaxMask_GGO_C}) TARGET_LINK_LIBRARIES(clitkMinMaxMask clitkCommon ITKIO ) - WRAP_GGO(clitkAutoCrop_GGO_C clitkAutoCrop.ggo) - ADD_EXECUTABLE(clitkAutoCrop clitkAutoCrop.cxx ${clitkAutoCrop_GGO_C}) - TARGET_LINK_LIBRARIES(clitkAutoCrop clitkCommon ITKIO ) + # WRAP_GGO(clitkAutoCrop_GGO_C clitkAutoCrop.ggo) + # ADD_EXECUTABLE(clitkAutoCrop clitkAutoCrop.cxx ${clitkAutoCrop_GGO_C}) + # TARGET_LINK_LIBRARIES(clitkAutoCrop clitkCommon ITKIO ) WRAP_GGO(clitkDicomRTStruct2BinaryImage_GGO_C clitkDicomRTStruct2BinaryImage.ggo) ADD_EXECUTABLE(clitkDicomRTStruct2BinaryImage clitkDicomRT_ROI_ConvertToImageFilter.cxx clitkDicomRTStruct2BinaryImage.cxx ${clitkDicomRTStruct2BinaryImage_GGO_C}) @@ -144,8 +144,8 @@ IF (CLITK_BUILD_TOOLS) ADD_EXECUTABLE(clitkBackProjectImage clitkBackProjectImage.cxx clitkBackProjectImageGenericFilter.cxx ${clitkBackProjectImage_GGO_C}) TARGET_LINK_LIBRARIES(clitkBackProjectImage ITKIO clitkCommon ) - WRAP_GGO(clitkCropImage_GGO_C clitkCropImage.ggo) - ADD_EXECUTABLE(clitkCropImage clitkCropImage.cxx clitkCropImageGenericFilter.cxx ${clitkCropImage_GGO_C}) +# WRAP_GGO(clitkCropImage_GGO_C clitkCropImage.ggo) + ADD_EXECUTABLE(clitkCropImage clitkCropImage.cxx ${clitkCropImage_GGO_C}) TARGET_LINK_LIBRARIES(clitkCropImage clitkCommon ITKIO) # WRAP_GGO(clitkExtractSlice_GGO_C clitkExtractSlice.ggo) diff --git a/tools/clitkCropImage.cxx b/tools/clitkCropImage.cxx index daa81ec..b353e07 100755 --- a/tools/clitkCropImage.cxx +++ b/tools/clitkCropImage.cxx @@ -1,7 +1,8 @@ + /*========================================================================= Program: vv http://www.creatis.insa-lyon.fr/rio/vv - Authors belong to: + 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 @@ -16,36 +17,30 @@ - CeCILL-B http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html ======================================================================-====*/ -/* ================================================= - * @file clitkCropImage.cxx - * @author - * @date - * - * @brief - * - ===================================================*/ - - // clitk #include "clitkCropImage_ggo.h" -#include "clitkIO.h" #include "clitkCropImageGenericFilter.h" - //-------------------------------------------------------------------- -int main(int argc, char * argv[]) { +int main(int argc, char * argv[]) +{ // Init command line GGO(clitkCropImage, args_info); CLITK_INIT; // Filter - clitk::CropImageGenericFilter::Pointer genericFilter=clitk::CropImageGenericFilter::New(); - - genericFilter->SetArgsInfo(args_info); - genericFilter->Update(); + typedef clitk::CropImageGenericFilter FilterType; + FilterType::Pointer filter = FilterType::New(); - return EXIT_SUCCESS; -}// end main + filter->SetArgsInfo(args_info); + + try { + filter->Update(); + } catch(std::runtime_error e) { + std::cout << e.what() << std::endl; + } + return EXIT_SUCCESS; +} // This is the end, my friend //-------------------------------------------------------------------- diff --git a/tools/clitkCropImage.ggo b/tools/clitkCropImage.ggo index ce5eab1..01a19b3 100755 --- a/tools/clitkCropImage.ggo +++ b/tools/clitkCropImage.ggo @@ -1,14 +1,30 @@ #File clitkCropImage.ggo package "clitkCropImage" version "1.0" -purpose "Reduce the image size by removing regions at the lower and upper end." +purpose "Crop an image according to a given extends or AutoCrop with a background value or like another image" -option "config" - "Config file" string no -option "verbose" v "Verbose" flag off +option "config" - "Config file" string no +option "imagetypes" - "Display allowed image types" flag off +option "verbose" v "Verbose" flag off +option "verboseOption" - "Display options values" flag off +option "verboseWarningOff" - "Do not display warning" flag off + +section "I/O" option "input" i "Input image filename" string yes option "output" o "Output image filename" string yes -option "boundingBox" b "Bounding box of the crop region" int no multiple + +section "Used determined crop" +option "boundingBox" b "Bounding box of the crop region" int no multiple option "lower" l "Size of the lower crop region" int no multiple option "upper" u "Size of the upper crop region" int no multiple option "origin" - "Set new origin to zero" flag off + +section "AutoCrop with BG value" +option "BG" - "Background value in input." double default="0" no + +section "Crop like another image" +option "like" - "Crop like this image (must have the same spacing" string no + + + diff --git a/tools/clitkCropImageGenericFilter.h b/tools/clitkCropImageGenericFilter.h index 5347b0b..2a16b9d 100755 --- a/tools/clitkCropImageGenericFilter.h +++ b/tools/clitkCropImageGenericFilter.h @@ -15,102 +15,59 @@ - BSD See included LICENSE.txt file - CeCILL-B http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html ======================================================================-====*/ -#ifndef clitkCropImageGenericFilter_h -#define clitkCropImageGenericFilter_h -/* ================================================= - * @file clitkCropImageGenericFilter.h - * @author - * @date - * - * @brief - * - ===================================================*/ +#ifndef CLITKCROPIMAGEGENERICFILTER_H +#define CLITKCROPIMAGEGENERICFILTER_H - -// clitk include +// clitk #include "clitkIO.h" -#include "clitkCommon.h" -#include "clitkImageCommon.h" -#include "clitkCropImage_ggo.h" +#include "clitkImageToImageGenericFilter.h" +#include "clitkCropLikeImageFilter.h" +#include "clitkAutoCropFilter.h" -//itk include -#include "itkLightObject.h" -#include "itkCropImageFilter.h" +// itk +#include +//-------------------------------------------------------------------- namespace clitk { - - class ITK_EXPORT CropImageGenericFilter : public itk::LightObject + template + class ITK_EXPORT CropImageGenericFilter: + public ImageToImageGenericFilter > { public: - //---------------------------------------- - // ITK - //---------------------------------------- - typedef CropImageGenericFilter Self; - typedef itk::LightObject Superclass; - typedef itk::SmartPointer Pointer; - typedef itk::SmartPointer ConstPointer; + //-------------------------------------------------------------------- + CropImageGenericFilter(); + + //-------------------------------------------------------------------- + typedef CropImageGenericFilter Self; + typedef ImageToImageGenericFilter > 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( CropImageGenericFilter, LightObject ); + //-------------------------------------------------------------------- + void SetArgsInfo(const ArgsInfoType & a); - //---------------------------------------- - // Typedefs - //---------------------------------------- - - - //---------------------------------------- - // Set & Get - //---------------------------------------- - void SetArgsInfo(const args_info_clitkCropImage & a) - { - m_ArgsInfo=a; - m_Verbose=m_ArgsInfo.verbose_flag; - m_InputFileName=m_ArgsInfo.input_arg; - } - - - //---------------------------------------- - // Update - //---------------------------------------- - void Update(); + //-------------------------------------------------------------------- + // Main function called each time the filter is updated + template + void UpdateWithInputImageType(); protected: + template void InitializeImageType(); + ArgsInfoType mArgsInfo; - //---------------------------------------- - // Constructor & Destructor - //---------------------------------------- - CropImageGenericFilter(); - ~CropImageGenericFilter() {}; - - - //---------------------------------------- - // Templated members - //---------------------------------------- - template void UpdateWithDim(std::string PixelType, unsigned int Components); - template void UpdateWithDimAndPixelType(); - - - //---------------------------------------- - // Data members - //---------------------------------------- - args_info_clitkCropImage m_ArgsInfo; - bool m_Verbose; - std::string m_InputFileName; - - }; - - + };// end class + //-------------------------------------------------------------------- } // end namespace clitk #ifndef ITK_MANUAL_INSTANTIATION #include "clitkCropImageGenericFilter.txx" #endif -#endif // #define clitkCropImageGenericFilter_h +#endif // #define CLITKCROPIMAGEGENERICFILTER_H diff --git a/tools/clitkCropImageGenericFilter.txx b/tools/clitkCropImageGenericFilter.txx index b35e557..89aeabc 100755 --- a/tools/clitkCropImageGenericFilter.txx +++ b/tools/clitkCropImageGenericFilter.txx @@ -14,139 +14,143 @@ - BSD See included LICENSE.txt file - CeCILL-B http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html -======================================================================-====*/ -#ifndef clitkCropImageGenericFilter_txx -#define clitkCropImageGenericFilter_txx + ======================================================================-====*/ -/* ================================================= - * @file clitkCropImageGenericFilter.txx - * @author - * @date - * - * @brief - * - ===================================================*/ +#include "clitkImageCommon.h" - -namespace clitk +//-------------------------------------------------------------------- +template +clitk::CropImageGenericFilter::CropImageGenericFilter(): + ImageToImageGenericFilter("CropImage") { - - //------------------------------------------------------------------- - // Update with the number of dimensions - //------------------------------------------------------------------- - template - void - CropImageGenericFilter::UpdateWithDim(std::string PixelType, unsigned int Components) - { - if (m_Verbose) std::cout << "Image was detected to be "<(); - } - // 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(); - } - - // 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(); - } + // Default values + cmdline_parser_clitkCropImage_init(&mArgsInfo); + //InitializeImageType<2>(); + InitializeImageType<3>(); + InitializeImageType<4>(); +} +//-------------------------------------------------------------------- + + +//-------------------------------------------------------------------- +template +template +void clitk::CropImageGenericFilter::InitializeImageType() +{ + // ADD_DEFAULT_IMAGE_TYPES(Dim); + ADD_IMAGE_TYPE(Dim, uchar); + ADD_IMAGE_TYPE(Dim, short); + // ADD_IMAGE_TYPE(Dim, uint); + // ADD_IMAGE_TYPE(Dim, ulong); + // ADD_IMAGE_TYPE(Dim, int); + // ADD_IMAGE_TYPE(Dim, float); +} +//-------------------------------------------------------------------- + + +//-------------------------------------------------------------------- +template +void clitk::CropImageGenericFilter::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); +} +//-------------------------------------------------------------------- + + +//-------------------------------------------------------------------- +// Update with the number of dimensions and the pixeltype +//-------------------------------------------------------------------- +template +template +void clitk::CropImageGenericFilter::UpdateWithInputImageType() +{ + // Reading input + typename ImageType::Pointer input = this->template GetInput(0); + + // Check options + if (mArgsInfo.BG_given && mArgsInfo.like_given) + clitkExceptionMacro("Do not use --BG and --like at the same time"); + + // Prepare output + typename ImageType::Pointer output; + + // ------------------------------------------------ + if (mArgsInfo.BG_given) { // AutoCrop filter + if (mArgsInfo.boundingBox_given) + clitkExceptionMacro("Do not use --BG and --boundingBox at the same time"); + if (mArgsInfo.lower_given) + clitkExceptionMacro("Do not use --BG and --lower at the same time"); + if (mArgsInfo.upper_given) + clitkExceptionMacro("Do not use --BG and --upper at the same time"); + typedef clitk::AutoCropFilter FilterType; + typename FilterType::Pointer filter = FilterType::New(); + filter->SetInput(input); + filter->SetBackgroundValue(mArgsInfo.BG_arg); + filter->Update(); + output = filter->GetOutput(); + } + else { + // ------------------------------------------------ + if (mArgsInfo.like_given) { // CropLike filter + if (mArgsInfo.boundingBox_given) + clitkExceptionMacro("Do not use --like and --boundingBox at the same time"); + if (mArgsInfo.lower_given) + clitkExceptionMacro("Do not use --like and --lower at the same time"); + if (mArgsInfo.upper_given) + clitkExceptionMacro("Do not use --like and --upper at the same time"); + typedef clitk::CropLikeImageFilter FilterType; + typename FilterType::Pointer filter = FilterType::New(); + filter->SetInput(input); + filter->SetCropLikeFilename(mArgsInfo.like_arg); + filter->Update(); + output = filter->GetOutput(); + } + else { + // ------------------------------------------------ + typename ImageType::SizeType lSize; + typename ImageType::SizeType uSize; + if (mArgsInfo.boundingBox_given) { + for(unsigned int i=0; iGetLargestPossibleRegion().GetSize()[i]-mArgsInfo.boundingBox_arg[2*i+1]-1; + } } - - else if (Components==3) - { - if (m_Verbose) std::cout << "Launching filter in "<< Dimension <<"D and 3D float (DVF)" << std::endl; - UpdateWithDimAndPixelType >(); + else { + if (mArgsInfo.lower_given) { + for(unsigned int i=0; i(mArgsInfo.lower_arg[i]); + } + else lSize.Fill(0); + if (mArgsInfo.upper_given) { + for(unsigned int i=0; i(mArgsInfo.upper_arg[i]); + } + else uSize.Fill(0); } - - else std::cerr<<"Number of components is "< CropImageFilterType; + typename CropImageFilterType::Pointer filter=CropImageFilterType::New(); + filter->SetInput(input); + filter->SetLowerBoundaryCropSize(lSize); + filter->SetUpperBoundaryCropSize(uSize); + filter->Update(); + output = filter->GetOutput(); + } } - - //------------------------------------------------------------------- - // Update with the number of dimensions and the pixeltype - //------------------------------------------------------------------- - template - void - CropImageGenericFilter::UpdateWithDimAndPixelType() - { - - // ImageTypes - typedef itk::Image InputImageType; - 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(); - - // Filter - typedef itk::CropImageFilter CropImageFilterType; - typename CropImageFilterType::Pointer cropFilter=CropImageFilterType::New(); - cropFilter->SetInput(input); - typename InputImageType::SizeType lSize, uSize; - - if (m_ArgsInfo.boundingBox_given) - { - for(unsigned int i=0;iGetLargestPossibleRegion().GetSize()[i]-m_ArgsInfo.boundingBox_arg[2*i+1]-1; - } - } - else - { - - if (m_ArgsInfo.lower_given) - for(unsigned int i=0;i(m_ArgsInfo.lower_arg[i]); - else lSize.Fill(0); - - if (m_ArgsInfo.upper_given) - for(unsigned int i=0;i(m_ArgsInfo.upper_arg[i]); - else uSize.Fill(0); - } - - cropFilter->SetLowerBoundaryCropSize(lSize); - cropFilter->SetUpperBoundaryCropSize(uSize); - cropFilter->Update(); - typename OutputImageType::Pointer output= cropFilter->GetOutput(); - - // Origin? - typename OutputImageType::PointType origin; + // Force origin if needed + if (mArgsInfo.origin_flag) { + typename ImageType::PointType origin; origin.Fill(itk::NumericTraits::Zero); - if (m_ArgsInfo.origin_flag) - { - output->SetOrigin(origin); - output->Update(); - if (m_Verbose) std::cout<<"Setting origin to "<< origin<<"..."< WriterType; - typename WriterType::Pointer writer = WriterType::New(); - writer->SetFileName(m_ArgsInfo.output_arg); - writer->SetInput(output); - writer->Update(); + output->SetOrigin(origin); } + // Write/Save results + this->template SetNextOutput(output); +} +//-------------------------------------------------------------------- + -}//end clitk - -#endif //#define clitkCropImageGenericFilter_txx -- 2.45.2