public:
/** Standard class typedefs. */
- typedef AutoCropFilter Self;
- typedef itk::ImageToImageFilter<ImageType, ImageType> Superclass;
- typedef itk::SmartPointer<Self> Pointer;
- typedef itk::SmartPointer<const Self> ConstPointer;
+ typedef AutoCropFilter Self;
+ typedef itk::ImageToImageFilter<ImageType, ImageType> Superclass;
+ typedef itk::SmartPointer<Self> Pointer;
+ typedef itk::SmartPointer<const Self> ConstPointer;
/** Method for creation through the object factory. */
itkNewMacro(Self);
ImageConstPointer input = dynamic_cast<const ImageType*>(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<ImageType, ImageType> CropFilterType;
//typedef itk::ExtractImageFilter<ImageType, ImageType> CropFilterType;
--- /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 CLITKCROPLIKEIMAGEFILTER_H
+#define CLITKCROPLIKEIMAGEFILTER_H
+
+#include <itkImageToImageFilter.h>
+
+namespace clitk {
+
+ //--------------------------------------------------------------------
+ /*
+ Perform various cropping operation on a image
+ */
+ //--------------------------------------------------------------------
+
+ template <class ImageType>
+ class ITK_EXPORT CropLikeImageFilter:
+ public itk::ImageToImageFilter<ImageType, ImageType> {
+
+ public:
+ /** Standard class typedefs. */
+ typedef CropLikeImageFilter Self;
+ typedef itk::ImageToImageFilter<ImageType, ImageType> Superclass;
+ 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(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<ImageType::ImageDimension> * like);
+ void SetCropLikeImage(const itk::ImageBase<ImageType::ImageDimension> * 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<ImageDimension> * m_LikeImage;
+ std::vector<bool> 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
--- /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 CLITKCROPLIKEIMAGEFILTER_TXX
+#define CLITKCROPLIKEIMAGEFILTER_TXX
+
+// clitk
+#include "clitkCommon.h"
+
+// itk
+#include "itkRegionOfInterestImageFilter.h"
+
+
+//--------------------------------------------------------------------
+template <class ImageType>
+clitk::CropLikeImageFilter<ImageType>::
+CropLikeImageFilter():itk::ImageToImageFilter<ImageType, ImageType>() {
+ this->SetNumberOfRequiredInputs(1);
+ m_LikeImage = NULL;
+ m_LikeFilenameIsGiven = false;
+ m_CropAlongThisDimension.resize(ImageType::ImageDimension);
+ for(uint i=0; i<ImageType::ImageDimension; i++)
+ m_CropAlongThisDimension[i] = true;
+}
+//--------------------------------------------------------------------
+
+
+//--------------------------------------------------------------------
+template <class ImageType>
+void
+clitk::CropLikeImageFilter<ImageType>::
+SetCropLikeFilename(std::string f)
+{
+ m_LikeFilename = f;
+ m_LikeFilenameIsGiven = true;
+}
+//--------------------------------------------------------------------
+
+
+//--------------------------------------------------------------------
+template <class ImageType>
+void
+clitk::CropLikeImageFilter<ImageType>::
+SetCropLikeImage(const itk::ImageBase<ImageType::ImageDimension> * like)
+{
+ m_LikeImage = like;
+}
+//--------------------------------------------------------------------
+
+
+//--------------------------------------------------------------------
+template <class ImageType>
+void
+clitk::CropLikeImageFilter<ImageType>::
+SetCropLikeImage(const itk::ImageBase<ImageType::ImageDimension> * like, int axe)
+{
+ m_LikeImage = like;
+ for(uint i=0; i<ImageType::ImageDimension; i++)
+ m_CropAlongThisDimension[i] = false;
+ m_CropAlongThisDimension[axe] = true;
+}
+//--------------------------------------------------------------------
+
+
+//--------------------------------------------------------------------
+template <class ImageType>
+void
+clitk::CropLikeImageFilter<ImageType>::
+SetInput(const ImageType * image) {
+ // Process object is not const-correct so the const casting is required.
+ this->SetNthInput(0, const_cast<ImageType *>( image ));
+}
+//--------------------------------------------------------------------
+
+
+//--------------------------------------------------------------------
+template <class ImageType>
+void
+clitk::CropLikeImageFilter<ImageType>::
+GenerateOutputInformation() {
+ // Get input pointers
+ ImageConstPointer input = dynamic_cast<const ImageType*>(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; i<ImageType::ImageDimension; i++) {
+ likeSize[i] = header->GetIORegion().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; i<ImageType::ImageDimension; i++) {
+ double ol;
+ if (m_CropAlongThisDimension[i]) {
+ size[i] = likeSize[i];
+ ol = likeOrigin[i];
+ }
+ else {
+ size[i] = input->GetLargestPossibleRegion().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 <class ImageType>
+void
+clitk::CropLikeImageFilter<ImageType>::
+GenerateData() {
+ // Get input pointers
+ ImageConstPointer input = dynamic_cast<const ImageType*>(itk::ProcessObject::GetInput(0));
+
+ typedef itk::RegionOfInterestImageFilter<ImageType, ImageType> 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
#=========================================================
#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)
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})
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)
+
/*=========================================================================
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
- 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<args_info_clitkCropImage> 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
//--------------------------------------------------------------------
#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
+
+
+
- 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 <itkCropImageFilter.h>
+//--------------------------------------------------------------------
namespace clitk
{
-
- class ITK_EXPORT CropImageGenericFilter : public itk::LightObject
+ template<class ArgsInfoType>
+ class ITK_EXPORT CropImageGenericFilter:
+ public ImageToImageGenericFilter<CropImageGenericFilter<ArgsInfoType> >
{
public:
- //----------------------------------------
- // ITK
- //----------------------------------------
- typedef CropImageGenericFilter Self;
- typedef itk::LightObject Superclass;
- typedef itk::SmartPointer<Self> Pointer;
- typedef itk::SmartPointer<const Self> ConstPointer;
+ //--------------------------------------------------------------------
+ CropImageGenericFilter();
+
+ //--------------------------------------------------------------------
+ typedef CropImageGenericFilter Self;
+ typedef ImageToImageGenericFilter<CropImageGenericFilter<ArgsInfoType> > Superclass;
+ 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( 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<class ImageType>
+ void UpdateWithInputImageType();
protected:
+ template<unsigned int Dim> void InitializeImageType();
+ ArgsInfoType mArgsInfo;
- //----------------------------------------
- // Constructor & Destructor
- //----------------------------------------
- CropImageGenericFilter();
- ~CropImageGenericFilter() {};
-
-
- //----------------------------------------
- // Templated members
- //----------------------------------------
- template <unsigned int Dimension> void UpdateWithDim(std::string PixelType, unsigned int Components);
- template <unsigned int Dimension, class PixelType> 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
- 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<class ArgsInfoType>
+clitk::CropImageGenericFilter<ArgsInfoType>::CropImageGenericFilter():
+ ImageToImageGenericFilter<Self>("CropImage")
{
-
- //-------------------------------------------------------------------
- // Update with the number of dimensions
- //-------------------------------------------------------------------
- template<unsigned int Dimension>
- void
- CropImageGenericFilter::UpdateWithDim(std::string PixelType, unsigned int Components)
- {
- if (m_Verbose) std::cout << "Image was detected to be "<<Dimension<<"D and "<<Components<<" component(s) of "<< PixelType<<"..."<<std::endl;
-
- if (Components==1)
- {
- 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>();
- }
-
- // 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>();
- }
+ // Default values
+ cmdline_parser_clitkCropImage_init(&mArgsInfo);
+ //InitializeImageType<2>();
+ InitializeImageType<3>();
+ InitializeImageType<4>();
+}
+//--------------------------------------------------------------------
+
+
+//--------------------------------------------------------------------
+template<class ArgsInfoType>
+template<unsigned int Dim>
+void clitk::CropImageGenericFilter<ArgsInfoType>::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<class ArgsInfoType>
+void clitk::CropImageGenericFilter<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);
+}
+//--------------------------------------------------------------------
+
+
+//--------------------------------------------------------------------
+// Update with the number of dimensions and the pixeltype
+//--------------------------------------------------------------------
+template<class ArgsInfoType>
+template<class ImageType>
+void clitk::CropImageGenericFilter<ArgsInfoType>::UpdateWithInputImageType()
+{
+ // Reading input
+ typename ImageType::Pointer input = this->template GetInput<ImageType>(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<ImageType> 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<ImageType> 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; i<ImageType::ImageDimension; i++) {
+ lSize[i] = mArgsInfo.boundingBox_arg[2*i];
+ uSize[i] = input->GetLargestPossibleRegion().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<Dimension, itk::Vector<float, 3> >();
+ else {
+ if (mArgsInfo.lower_given) {
+ for(unsigned int i=0; i<ImageType::ImageDimension; i++)
+ lSize[i]=static_cast<unsigned int >(mArgsInfo.lower_arg[i]);
+ }
+ else lSize.Fill(0);
+ if (mArgsInfo.upper_given) {
+ for(unsigned int i=0; i<ImageType::ImageDimension; i++)
+ uSize[i]=static_cast<unsigned int >(mArgsInfo.upper_arg[i]);
+ }
+ else uSize.Fill(0);
}
-
- else std::cerr<<"Number of components is "<<Components<<", not supported!"<<std::endl;
+ typedef itk::CropImageFilter<ImageType, ImageType> 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 <unsigned int Dimension, class PixelType>
- void
- CropImageGenericFilter::UpdateWithDimAndPixelType()
- {
-
- // ImageTypes
- typedef itk::Image<PixelType, Dimension> InputImageType;
- typedef itk::Image<PixelType, Dimension> OutputImageType;
-
- // Read the input
- typedef itk::ImageFileReader<InputImageType> InputReaderType;
- typename InputReaderType::Pointer reader = InputReaderType::New();
- reader->SetFileName( m_InputFileName);
- reader->Update( );
- typename InputImageType::Pointer input= reader->GetOutput();
-
- // Filter
- typedef itk::CropImageFilter<InputImageType, OutputImageType> 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;i<Dimension;i++)
- {
- lSize[i]=m_ArgsInfo.boundingBox_arg[2*i];
- uSize[i]=input->GetLargestPossibleRegion().GetSize()[i]-m_ArgsInfo.boundingBox_arg[2*i+1]-1;
- }
- }
- else
- {
-
- if (m_ArgsInfo.lower_given)
- for(unsigned int i=0;i<Dimension;i++)
- lSize[i]=static_cast<unsigned int >(m_ArgsInfo.lower_arg[i]);
- else lSize.Fill(0);
-
- if (m_ArgsInfo.upper_given)
- for(unsigned int i=0;i<Dimension;i++)
- uSize[i]=static_cast<unsigned int >(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<double>::Zero);
- if (m_ArgsInfo.origin_flag)
- {
- output->SetOrigin(origin);
- output->Update();
- if (m_Verbose) std::cout<<"Setting origin to "<< origin<<"..."<<std::endl;
- }
-
- // Output
- typedef itk::ImageFileWriter<OutputImageType> 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<ImageType>(output);
+}
+//--------------------------------------------------------------------
+
-}//end clitk
-
-#endif //#define clitkCropImageGenericFilter_txx