From d067c7f46d2cf1041d17ce2da8cfa4a9e8b939c2 Mon Sep 17 00:00:00 2001 From: tbaudier Date: Tue, 13 Sep 2016 17:06:12 +0200 Subject: [PATCH] Add tool: clitkNVectorImageTo4DImage --- tools/CMakeLists.txt | 5 + tools/clitkNVectorImageTo4DImage.cxx | 51 ++++++++ tools/clitkNVectorImageTo4DImage.ggo | 12 ++ tools/clitkNVectorImageTo4DImageFilter.h | 108 ++++++++++++++++ tools/clitkNVectorImageTo4DImageFilter.txx | 72 +++++++++++ ...litkNVectorImageTo4DImageGenericFilter.cxx | 71 ++++++++++ .../clitkNVectorImageTo4DImageGenericFilter.h | 116 +++++++++++++++++ ...litkNVectorImageTo4DImageGenericFilter.txx | 122 ++++++++++++++++++ 8 files changed, 557 insertions(+) create mode 100644 tools/clitkNVectorImageTo4DImage.cxx create mode 100644 tools/clitkNVectorImageTo4DImage.ggo create mode 100644 tools/clitkNVectorImageTo4DImageFilter.h create mode 100644 tools/clitkNVectorImageTo4DImageFilter.txx create mode 100644 tools/clitkNVectorImageTo4DImageGenericFilter.cxx create mode 100644 tools/clitkNVectorImageTo4DImageGenericFilter.h create mode 100644 tools/clitkNVectorImageTo4DImageGenericFilter.txx diff --git a/tools/CMakeLists.txt b/tools/CMakeLists.txt index e0981f3..7333fab 100644 --- a/tools/CMakeLists.txt +++ b/tools/CMakeLists.txt @@ -248,6 +248,11 @@ if(CLITK_BUILD_TOOLS) add_executable(clitkVectorImageToImage clitkVectorImageToImage.cxx clitkVectorImageToImageGenericFilter.cxx ${clitkVectorImageToImage_GGO_C}) target_link_libraries(clitkVectorImageToImage clitkCommon) set(TOOLS_INSTALL ${TOOLS_INSTALL} clitkVectorImageToImage) + + WRAP_GGO(clitkNVectorImageTo4DImage_GGO_C clitkNVectorImageTo4DImage.ggo) + add_executable(clitkNVectorImageTo4DImage clitkNVectorImageTo4DImage.cxx clitkNVectorImageTo4DImageGenericFilter.cxx ${clitkNVectorImageTo4DImage_GGO_C}) + target_link_libraries(clitkNVectorImageTo4DImage clitkCommon) + set(TOOLS_INSTALL ${TOOLS_INSTALL} clitkNVectorImageTo4DImage) add_executable(clitkMIP clitkMIP.cxx clitkMIPGenericFilter.cxx) target_link_libraries(clitkMIP clitkMIPLib clitkCommon) diff --git a/tools/clitkNVectorImageTo4DImage.cxx b/tools/clitkNVectorImageTo4DImage.cxx new file mode 100644 index 0000000..05bf7ae --- /dev/null +++ b/tools/clitkNVectorImageTo4DImage.cxx @@ -0,0 +1,51 @@ +/*========================================================================= + 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://www.centreleonberard.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 +===========================================================================**/ + +/* ================================================= + * @file clitkNVectorImageTo4DImage.cxx + * @author + * @date + * + * @brief + * + ===================================================*/ + + +// clitk +#include "clitkNVectorImageTo4DImage_ggo.h" +#include "clitkIO.h" +#include "clitkNVectorImageTo4DImageGenericFilter.h" + + +//-------------------------------------------------------------------- +int main(int argc, char * argv[]) { + + // Init command line + GGO(clitkNVectorImageTo4DImage, args_info); + CLITK_INIT; + + // Filter + clitk::NVectorImageTo4DImageGenericFilter::Pointer genericFilter=clitk::NVectorImageTo4DImageGenericFilter::New(); + + genericFilter->SetArgsInfo(args_info); + genericFilter->Update(); + + return EXIT_SUCCESS; +}// end main + +//-------------------------------------------------------------------- diff --git a/tools/clitkNVectorImageTo4DImage.ggo b/tools/clitkNVectorImageTo4DImage.ggo new file mode 100644 index 0000000..d72a700 --- /dev/null +++ b/tools/clitkNVectorImageTo4DImage.ggo @@ -0,0 +1,12 @@ +#File clitkNVectorImageTo4DImage.ggo +package "clitkNVectorImageTo4DImage" +version "1.0" +purpose "Convert all pixel channels of the image into a 4D image" + +option "config" - "Config file" string no +option "verbose" v "Verbose" flag off + +option "input" i "Input image filename" string yes +option "output" o "Output image filename" string yes +option "componentIndex" c "Component index to extract" int no default="0" + diff --git a/tools/clitkNVectorImageTo4DImageFilter.h b/tools/clitkNVectorImageTo4DImageFilter.h new file mode 100644 index 0000000..f76eb9c --- /dev/null +++ b/tools/clitkNVectorImageTo4DImageFilter.h @@ -0,0 +1,108 @@ +/*========================================================================= + 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://www.centreleonberard.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 clitkNVectorImageTo4DImageFilter_h +#define clitkNVectorImageTo4DImageFilter_h + +/* ================================================= + * @file clitkNVectorImageTo4DImageFilter.h + * @author + * @date + * + * @brief + * + ===================================================*/ + + +// clitk include +#include "clitkIO.h" +#include "clitkCommon.h" + +//itk include +#include "itkImageToImageFilter.h" + +namespace clitk +{ + + template + class ITK_EXPORT NVectorImageTo4DImageFilter : + public itk::ImageToImageFilter + { + public: + //---------------------------------------- + // ITK + //---------------------------------------- + typedef NVectorImageTo4DImageFilter 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( NVectorImageTo4DImageFilter, ImageToImageFilter ); + + /** Dimension of the domain space. */ + itkStaticConstMacro(InputImageDimension, unsigned int, Superclass::InputImageDimension); + itkStaticConstMacro(OutputImageDimension, unsigned int, Superclass::OutputImageDimension); + + //---------------------------------------- + // Typedefs + //---------------------------------------- + typedef typename OutputImageType::RegionType OutputImageRegionType; + + //---------------------------------------- + // Set & Get + //---------------------------------------- + itkBooleanMacro(Verbose); + itkSetMacro( Verbose, bool); + itkGetConstReferenceMacro( Verbose, bool); + itkSetMacro(ComponentIndex, unsigned int); + itkGetConstMacro(ComponentIndex, unsigned int); + + protected: + + //---------------------------------------- + // Constructor & Destructor + //---------------------------------------- + NVectorImageTo4DImageFilter(); + ~NVectorImageTo4DImageFilter() {}; + + //---------------------------------------- + // Update + //---------------------------------------- + void ThreadedGenerateData(const OutputImageRegionType & outputRegionForThread, itk::ThreadIdType threadId ); + + //---------------------------------------- + // Data members + //---------------------------------------- + bool m_Verbose; + unsigned int m_ComponentIndex; + + }; + + +} // end namespace clitk + +#ifndef ITK_MANUAL_INSTANTIATION +#include "clitkNVectorImageTo4DImageFilter.txx" +#endif + +#endif // #define clitkNVectorImageTo4DImageFilter_h + + diff --git a/tools/clitkNVectorImageTo4DImageFilter.txx b/tools/clitkNVectorImageTo4DImageFilter.txx new file mode 100644 index 0000000..f5570cd --- /dev/null +++ b/tools/clitkNVectorImageTo4DImageFilter.txx @@ -0,0 +1,72 @@ +/*========================================================================= + 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://www.centreleonberard.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 clitkNVectorImageTo4DImageFilter_txx +#define clitkNVectorImageTo4DImageFilter_txx + +/* ================================================= + * @file clitkNVectorImageTo4DImageFilter.txx + * @author + * @date + * + * @brief + * + ===================================================*/ + + +namespace clitk +{ + + //------------------------------------------------------------------- + // Constructor + //------------------------------------------------------------------- + template + NVectorImageTo4DImageFilter::NVectorImageTo4DImageFilter() + { + m_Verbose=false; + m_ComponentIndex=0; + } + + + //------------------------------------------------------------------- + // Generate Data + //------------------------------------------------------------------- + template + void NVectorImageTo4DImageFilter::ThreadedGenerateData(const OutputImageRegionType & outputRegionForThread, itk::ThreadIdType threadId) + { + // Iterators + typename OutputImageType::Pointer output=this->GetOutput(); + typename InputImageType::ConstPointer input=this->GetInput(); + + typedef itk::ImageRegionConstIterator InputIteratorType; + InputIteratorType inputIt (input, outputRegionForThread); + + typedef itk::ImageRegionIterator OutputIteratorType; + OutputIteratorType outputIt (output, outputRegionForThread); + + while(! inputIt.IsAtEnd() ) + { + //outputIt.Set(inputIt.Get()[m_ComponentIndex]); + ++outputIt; + ++inputIt; + } + } + + +}//end clitk + +#endif //#define clitkNVectorImageTo4DImageFilter_txx diff --git a/tools/clitkNVectorImageTo4DImageGenericFilter.cxx b/tools/clitkNVectorImageTo4DImageGenericFilter.cxx new file mode 100644 index 0000000..9203c4c --- /dev/null +++ b/tools/clitkNVectorImageTo4DImageGenericFilter.cxx @@ -0,0 +1,71 @@ +/*========================================================================= + 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://www.centreleonberard.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 clitkNVectorImageTo4DImageGenericFilter_cxx +#define clitkNVectorImageTo4DImageGenericFilter_cxx + +/* ================================================= + * @file clitkNVectorImageTo4DImageGenericFilter.cxx + * @author + * @date + * + * @brief + * + ===================================================*/ + +#include "clitkNVectorImageTo4DImageGenericFilter.h" + + +namespace clitk +{ + + + //----------------------------------------------------------- + // Constructor + //----------------------------------------------------------- + NVectorImageTo4DImageGenericFilter::NVectorImageTo4DImageGenericFilter() + { + m_Verbose=false; + m_InputFileName=""; + } + + + //----------------------------------------------------------- + // Update + //----------------------------------------------------------- + void NVectorImageTo4DImageGenericFilter::Update() + { + // Read the Dimension and PixelType + int Dimension, Components; + std::string PixelType; + ReadImageDimensionAndPixelType(m_InputFileName, Dimension, PixelType, Components); + + // Call UpdateWithDim + if(Dimension==2) UpdateWithDim<2>(PixelType, Components); + else if(Dimension==3) UpdateWithDim<3>(PixelType, Components); + else if (Dimension==4)UpdateWithDim<4>(PixelType, Components); + else + { + std::cout<<"Error, Only for 2, 3 or 4 Dimensions!!!"< Pointer; + typedef itk::SmartPointer ConstPointer; + + // Method for creation through the object factory + itkNewMacro(Self); + + // Run-time type information (and related methods) + itkTypeMacro( NVectorImageTo4DImageGenericFilter, LightObject ); + + + //---------------------------------------- + // Typedefs + //---------------------------------------- + + + //---------------------------------------- + // Set & Get + //---------------------------------------- + void SetArgsInfo(const args_info_clitkNVectorImageTo4DImage & a) + { + m_ArgsInfo=a; + m_Verbose=m_ArgsInfo.verbose_flag; + m_InputFileName=m_ArgsInfo.input_arg; + } + + + //---------------------------------------- + // Update + //---------------------------------------- + void Update(); + + protected: + + //---------------------------------------- + // Constructor & Destructor + //---------------------------------------- + NVectorImageTo4DImageGenericFilter(); + ~NVectorImageTo4DImageGenericFilter() {}; + + + //---------------------------------------- + // Templated members + //---------------------------------------- + template void UpdateWithDim(std::string PixelType, unsigned int Components); + template void UpdateWithDimAndPixelType(); + + + //---------------------------------------- + // Data members + //---------------------------------------- + args_info_clitkNVectorImageTo4DImage m_ArgsInfo; + bool m_Verbose; + std::string m_InputFileName; + + }; + + +} // end namespace clitk + +#ifndef ITK_MANUAL_INSTANTIATION +#include "clitkNVectorImageTo4DImageGenericFilter.txx" +#endif + +#endif // #define clitkNVectorImageTo4DImageGenericFilter_h diff --git a/tools/clitkNVectorImageTo4DImageGenericFilter.txx b/tools/clitkNVectorImageTo4DImageGenericFilter.txx new file mode 100644 index 0000000..0c2d429 --- /dev/null +++ b/tools/clitkNVectorImageTo4DImageGenericFilter.txx @@ -0,0 +1,122 @@ +/*========================================================================= + 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://www.centreleonberard.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 clitkNVectorImageTo4DImageGenericFilter_txx +#define clitkNVectorImageTo4DImageGenericFilter_txx + +/* ================================================= + * @file clitkNVectorImageTo4DImageGenericFilter.txx + * @author + * @date + * + * @brief + * + ===================================================*/ + +#include "itkVectorImageToImageAdaptor.h" +#include + +namespace clitk +{ + + //------------------------------------------------------------------- + // Update with the number of dimensions + //------------------------------------------------------------------- + template + void + NVectorImageTo4DImageGenericFilter::UpdateWithDim(std::string PixelType, unsigned int Components) + { + if (m_Verbose) std::cout << "Image was detected to be "<(); + } + + + //------------------------------------------------------------------- + // Update with the number of dimensions and the pixeltype + //------------------------------------------------------------------- + template + void + NVectorImageTo4DImageGenericFilter::UpdateWithDimAndPixelType() + { + // ImageTypes + typedef itk::VectorImage 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::VectorImageToImageAdaptor ImageAdaptorType; + typedef itk::ImageFileWriter WriterType; + typename ImageAdaptorType::Pointer adaptor = ImageAdaptorType::New(); + typename OutputImageType::Pointer output = OutputImageType::New(); + typename WriterType::Pointer writer = WriterType::New(); + + adaptor->SetExtractComponentIndex(0); + adaptor->SetImage(input); + std::string fileName=m_ArgsInfo.output_arg; + + //Create the output + typename OutputImageType::IndexType index; + index.Fill(0); + typename OutputImageType::SizeType size; + for (unsigned int pixelDim=0; pixelDimGetNumberOfComponentsPerPixel(); ++pixelDim) + { + size[pixelDim]=input->GetLargestPossibleRegion().GetSize(pixelDim); + } + size[Dimension]=input->GetNumberOfComponentsPerPixel(); + typename OutputImageType::RegionType region; + region.SetSize(size); + region.SetIndex(index); + output->SetRegions(region); + output->Allocate(); + writer->SetInput(output); + + //Copy each channel + for (unsigned int pixelDim=0; pixelDimGetNumberOfComponentsPerPixel(); ++pixelDim) + { + adaptor->SetExtractComponentIndex(pixelDim); + + itk::ImageRegionIterator imageIterator(input,input->GetLargestPossibleRegion()); + + while(!imageIterator.IsAtEnd()) + { + typename OutputImageType::IndexType indexVector; + indexVector.Fill(0); + for (unsigned int indexDim=0; indexDimSetPixel(indexVector, adaptor->GetPixel(imageIterator.GetIndex())); + ++imageIterator; + } + } + // Output + writer->SetFileName(fileName); + writer->Update(); + } +}//end clitk + +#endif //#define clitkNVectorImageTo4DImageGenericFilter_txx -- 2.45.1