From 029daf53fe49fdf0866f9614c22c7a448d05d3f1 Mon Sep 17 00:00:00 2001 From: tbaudier Date: Tue, 27 Sep 2016 12:03:41 +0200 Subject: [PATCH] Add new tool: conversion from 4D Image to Vector Image The last dimension of the 4D image is now the vector pixel of the output image --- tools/CMakeLists.txt | 7 +- tools/clitk4DImageToNVectorImage.cxx | 51 +++++++ tools/clitk4DImageToNVectorImage.ggo | 11 ++ ...litk4DImageToNVectorImageGenericFilter.cxx | 71 ++++++++++ .../clitk4DImageToNVectorImageGenericFilter.h | 115 +++++++++++++++ ...litk4DImageToNVectorImageGenericFilter.txx | 134 ++++++++++++++++++ 6 files changed, 388 insertions(+), 1 deletion(-) create mode 100644 tools/clitk4DImageToNVectorImage.cxx create mode 100644 tools/clitk4DImageToNVectorImage.ggo create mode 100644 tools/clitk4DImageToNVectorImageGenericFilter.cxx create mode 100644 tools/clitk4DImageToNVectorImageGenericFilter.h create mode 100644 tools/clitk4DImageToNVectorImageGenericFilter.txx diff --git a/tools/CMakeLists.txt b/tools/CMakeLists.txt index 7333fab..422d8f9 100644 --- a/tools/CMakeLists.txt +++ b/tools/CMakeLists.txt @@ -249,11 +249,16 @@ if(CLITK_BUILD_TOOLS) target_link_libraries(clitkVectorImageToImage clitkCommon) set(TOOLS_INSTALL ${TOOLS_INSTALL} clitkVectorImageToImage) - WRAP_GGO(clitkNVectorImageTo4DImage_GGO_C clitkNVectorImageTo4DImage.ggo) + 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) + WRAP_GGO(clitk4DImageToNVectorImage_GGO_C clitk4DImageToNVectorImage.ggo) + add_executable(clitk4DImageToNVectorImage clitk4DImageToNVectorImage.cxx clitk4DImageToNVectorImageGenericFilter.cxx ${clitk4DImageToNVectorImage_GGO_C}) + target_link_libraries(clitk4DImageToNVectorImage clitkCommon) + set(TOOLS_INSTALL ${TOOLS_INSTALL} clitk4DImageToNVectorImage) + add_executable(clitkMIP clitkMIP.cxx clitkMIPGenericFilter.cxx) target_link_libraries(clitkMIP clitkMIPLib clitkCommon) set(TOOLS_INSTALL ${TOOLS_INSTALL} clitkMIP) diff --git a/tools/clitk4DImageToNVectorImage.cxx b/tools/clitk4DImageToNVectorImage.cxx new file mode 100644 index 0000000..62bd49d --- /dev/null +++ b/tools/clitk4DImageToNVectorImage.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 clitk4DImageToNVectorImage.cxx + * @author + * @date + * + * @brief + * + ===================================================*/ + + +// clitk +#include "clitk4DImageToNVectorImage_ggo.h" +#include "clitkIO.h" +#include "clitk4DImageToNVectorImageGenericFilter.h" + + +//-------------------------------------------------------------------- +int main(int argc, char * argv[]) { + + // Init command line + GGO(clitk4DImageToNVectorImage, args_info); + CLITK_INIT; + + // Filter + clitk::FourDImageToNVectorImageGenericFilter::Pointer genericFilter=clitk::FourDImageToNVectorImageGenericFilter::New(); + + genericFilter->SetArgsInfo(args_info); + genericFilter->Update(); + + return EXIT_SUCCESS; +}// end main + +//-------------------------------------------------------------------- diff --git a/tools/clitk4DImageToNVectorImage.ggo b/tools/clitk4DImageToNVectorImage.ggo new file mode 100644 index 0000000..7f0d9dc --- /dev/null +++ b/tools/clitk4DImageToNVectorImage.ggo @@ -0,0 +1,11 @@ +#File clitk4DImageToNVectorImage.ggo +package "clitk4DImageToNVectorImage" +version "1.0" +purpose "Convert the 4th dimension of a 4D Image into a VectorPixel 3D 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 + diff --git a/tools/clitk4DImageToNVectorImageGenericFilter.cxx b/tools/clitk4DImageToNVectorImageGenericFilter.cxx new file mode 100644 index 0000000..d70af20 --- /dev/null +++ b/tools/clitk4DImageToNVectorImageGenericFilter.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 clitk4DImageToNVectorImageGenericFilter_cxx +#define clitk4DImageToNVectorImageGenericFilter_cxx + +/* ================================================= + * @file clitk4DImageToNVectorImageGenericFilter.cxx + * @author + * @date + * + * @brief + * + ===================================================*/ + +#include "clitk4DImageToNVectorImageGenericFilter.h" + + +namespace clitk +{ + + + //----------------------------------------------------------- + // Constructor + //----------------------------------------------------------- + FourDImageToNVectorImageGenericFilter::FourDImageToNVectorImageGenericFilter() + { + m_Verbose=false; + m_InputFileName=""; + } + + + //----------------------------------------------------------- + // Update + //----------------------------------------------------------- + void FourDImageToNVectorImageGenericFilter::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( 4DImageToNVectorImageGenericFilter, LightObject ); + + + //---------------------------------------- + // Typedefs + //---------------------------------------- + + + //---------------------------------------- + // Set & Get + //---------------------------------------- + void SetArgsInfo(const args_info_clitk4DImageToNVectorImage & a) + { + m_ArgsInfo=a; + m_Verbose=m_ArgsInfo.verbose_flag; + m_InputFileName=m_ArgsInfo.input_arg; + } + + + //---------------------------------------- + // Update + //---------------------------------------- + void Update(); + + protected: + + //---------------------------------------- + // Constructor & Destructor + //---------------------------------------- + FourDImageToNVectorImageGenericFilter(); + ~FourDImageToNVectorImageGenericFilter() {}; + + + //---------------------------------------- + // Templated members + //---------------------------------------- + template void UpdateWithDim(std::string PixelType, unsigned int Components); + template void UpdateWithDimAndPixelType(); + + + //---------------------------------------- + // Data members + //---------------------------------------- + args_info_clitk4DImageToNVectorImage m_ArgsInfo; + bool m_Verbose; + std::string m_InputFileName; + + }; + + +} // end namespace clitk + +#ifndef ITK_MANUAL_INSTANTIATION +#include "clitk4DImageToNVectorImageGenericFilter.txx" +#endif + +#endif // #define clitk4DImageToNVectorImageGenericFilter_h diff --git a/tools/clitk4DImageToNVectorImageGenericFilter.txx b/tools/clitk4DImageToNVectorImageGenericFilter.txx new file mode 100644 index 0000000..554cec7 --- /dev/null +++ b/tools/clitk4DImageToNVectorImageGenericFilter.txx @@ -0,0 +1,134 @@ +/*========================================================================= + 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 clitk4DImageToNVectorImageGenericFilter_txx +#define clitk4DImageToNVectorImageGenericFilter_txx + +/* ================================================= + * @file clitk4DImageToNVectorImageGenericFilter.txx + * @author + * @date + * + * @brief + * + ===================================================*/ + +#include "itkComposeImageFilter.h" +#include "itkExtractImageFilter.h" + +namespace clitk +{ + + //------------------------------------------------------------------- + // Update with the number of dimensions + //------------------------------------------------------------------- + template + void + FourDImageToNVectorImageGenericFilter::UpdateWithDim(std::string PixelType, unsigned int Components) + { + if (m_Verbose) std::cout << "Image was detected to be "<(); + } else if (PixelType == "unsigned short") { + UpdateWithDimAndPixelType(); + } else if (PixelType == "unsigned_short") { + UpdateWithDimAndPixelType(); + } else if (PixelType == "char") { + UpdateWithDimAndPixelType(); + } else if (PixelType == "unsigned_char") { + UpdateWithDimAndPixelType(); + } else if (PixelType == "int") { + UpdateWithDimAndPixelType(); + } else if (PixelType == "unsigned_int") { + UpdateWithDimAndPixelType(); + } else if (PixelType == "double") { + UpdateWithDimAndPixelType(); + } else if (PixelType == "float") { + UpdateWithDimAndPixelType(); + } else { + std::cerr << "Error, pixel type : \"" << PixelType << "\" unknown !" << std::endl; + } + } + + + //------------------------------------------------------------------- + // Update with the number of dimensions and the pixeltype + //------------------------------------------------------------------- + template + void + FourDImageToNVectorImageGenericFilter::UpdateWithDimAndPixelType() + { + // ImageTypes + typedef itk::Image InputImageType; + typedef itk::Image MedianImageType; + typedef itk::VectorImage 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(); + std::string fileName=m_ArgsInfo.output_arg; + + //Filter + typedef itk::ComposeImageFilter ImageToVectorImageFilterType; + typename ImageToVectorImageFilterType::Pointer imageToVectorImageFilter = ImageToVectorImageFilterType::New(); + typedef itk::ExtractImageFilter ExtractFilterType; + typename ExtractFilterType::Pointer extractFilter = ExtractFilterType::New(); + extractFilter->SetDirectionCollapseToSubmatrix(); + typedef itk::ImageFileWriter WriterType; + typename WriterType::Pointer writer = WriterType::New(); + + extractFilter->SetInput(input); + typename InputImageType::SizeType size; + for (unsigned int nbDimension=0; nbDimensionGetLargestPossibleRegion().GetSize(nbDimension); + } + size[Dimension-1] = 0; + typename MedianImageType::Pointer tempImage = MedianImageType::New(); + + //Extract All "time" slices + for (unsigned int nbSlice=0; nbSliceGetLargestPossibleRegion().GetSize(Dimension-1); ++nbSlice) + { + typename InputImageType::RegionType desiredRegion; + typename InputImageType::IndexType index; + index.Fill(0); + index[Dimension-1]=nbSlice; + desiredRegion.SetSize(size); + extractFilter->SetInput(input); + desiredRegion.SetIndex(index); + extractFilter->SetExtractionRegion(desiredRegion); + extractFilter->Update(); + tempImage = extractFilter->GetOutput(); + tempImage->DisconnectPipeline(); + extractFilter->Update(); + imageToVectorImageFilter->SetInput(nbSlice, tempImage); + } + + imageToVectorImageFilter->Update(); + + // Output + writer->SetInput(imageToVectorImageFilter->GetOutput()); + writer->SetFileName(fileName); + writer->Update(); + } +}//end clitk + +#endif //#define clitk4DImageToNVectorImageGenericFilter_txx -- 2.47.1