From 867fad1c5395a1931440882c84f7cab67a81d7fe Mon Sep 17 00:00:00 2001 From: schaerer Date: Tue, 26 Oct 2010 13:43:54 +0000 Subject: [PATCH] temporal median of a sequence of images, to be used for the mid P project --- tools/CMakeLists.txt | 5 + tools/clitkMedianTemporalDimension.cxx | 53 +++++ tools/clitkMedianTemporalDimension.ggo | 12 + ...tkMedianTemporalDimensionGenericFilter.cxx | 41 ++++ ...litkMedianTemporalDimensionGenericFilter.h | 114 +++++++++ ...tkMedianTemporalDimensionGenericFilter.txx | 221 ++++++++++++++++++ 6 files changed, 446 insertions(+) create mode 100644 tools/clitkMedianTemporalDimension.cxx create mode 100644 tools/clitkMedianTemporalDimension.ggo create mode 100644 tools/clitkMedianTemporalDimensionGenericFilter.cxx create mode 100644 tools/clitkMedianTemporalDimensionGenericFilter.h create mode 100644 tools/clitkMedianTemporalDimensionGenericFilter.txx diff --git a/tools/CMakeLists.txt b/tools/CMakeLists.txt index ce1d706..02a0511 100644 --- a/tools/CMakeLists.txt +++ b/tools/CMakeLists.txt @@ -73,6 +73,11 @@ IF (CLITK_BUILD_TOOLS) ADD_EXECUTABLE(clitkAverageTemporalDimension clitkAverageTemporalDimension.cxx ${clitkAverageTemporalDimension_GGO_C}) TARGET_LINK_LIBRARIES(clitkAverageTemporalDimension clitkCommon ITKIO ) + WRAP_GGO(clitkMedianTemporalDimension_GGO_C clitkMedianTemporalDimension.ggo) + ADD_EXECUTABLE(clitkMedianTemporalDimension clitkMedianTemporalDimension.cxx + ${clitkMedianTemporalDimension_GGO_C}) + TARGET_LINK_LIBRARIES(clitkMedianTemporalDimension clitkCommon ITKIO ) + WRAP_GGO(clitkWarpImage_GGO_C clitkWarpImage.ggo) ADD_EXECUTABLE(clitkWarpImage clitkWarpImage.cxx ${clitkWarpImage_GGO_C} clitkWarpImageGenericFilter.cxx) TARGET_LINK_LIBRARIES(clitkWarpImage ITKBasicFilters clitkCommon ITKIO) diff --git a/tools/clitkMedianTemporalDimension.cxx b/tools/clitkMedianTemporalDimension.cxx new file mode 100644 index 0000000..deba4ab --- /dev/null +++ b/tools/clitkMedianTemporalDimension.cxx @@ -0,0 +1,53 @@ +/*========================================================================= + 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 +======================================================================-====*/ + +/* ================================================= + * @file clitkMedianTemporalDimension.cxx + * @author + * @date + * + * @brief + * + ===================================================*/ + + +// clitk +#include "clitkMedianTemporalDimension_ggo.h" +#include "clitkIO.h" +#include "clitkCommon.h" +#include "clitkMedianTemporalDimensionGenericFilter.h" + +//-------------------------------------------------------------------- +int main(int argc, char * argv[]) +{ + + // Init command line + GGO(clitkMedianTemporalDimension, args_info); + CLITK_INIT; + + // Filter + typedef clitk::MedianTemporalDimensionGenericFilter FilterType; + FilterType::Pointer genericFilter = FilterType::New(); + + genericFilter->SetArgsInfo(args_info); + genericFilter->Update(); + + return EXIT_SUCCESS; +}// end main + +//-------------------------------------------------------------------- diff --git a/tools/clitkMedianTemporalDimension.ggo b/tools/clitkMedianTemporalDimension.ggo new file mode 100644 index 0000000..2e6e23e --- /dev/null +++ b/tools/clitkMedianTemporalDimension.ggo @@ -0,0 +1,12 @@ +#File clitkMedianTemporalDimension.ggo +package "clitkMedianTemporalDimension" +version "1.0" +purpose "Average the last dimension to an (n-1)D image. Input is either nD or multiple (n-1)D image or DVF" + +option "config" - "Config file" string no +option "verbose" v "Verbose" flag off + +option "input" i "Input image filename" string yes multiple +option "output" o "Output image filename" string yes + + diff --git a/tools/clitkMedianTemporalDimensionGenericFilter.cxx b/tools/clitkMedianTemporalDimensionGenericFilter.cxx new file mode 100644 index 0000000..67ec36e --- /dev/null +++ b/tools/clitkMedianTemporalDimensionGenericFilter.cxx @@ -0,0 +1,41 @@ +/*========================================================================= + 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 clitkMedianTemporalDimensionGenericFilter_cxx +#define clitkMedianTemporalDimensionGenericFilter_cxx + +/* ================================================= + * @file clitkMedianTemporalDimensionGenericFilter.cxx + * @author + * @date + * + * @brief + * + ===================================================*/ + +#include "clitkMedianTemporalDimensionGenericFilter.h" + + +namespace clitk +{ + + + + +} //end clitk + +#endif //#define clitkMedianTemporalDimensionGenericFilter_cxx diff --git a/tools/clitkMedianTemporalDimensionGenericFilter.h b/tools/clitkMedianTemporalDimensionGenericFilter.h new file mode 100644 index 0000000..34d023d --- /dev/null +++ b/tools/clitkMedianTemporalDimensionGenericFilter.h @@ -0,0 +1,114 @@ +/*========================================================================= + 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 clitkMedianTemporalDimensionGenericFilter_h +#define clitkMedianTemporalDimensionGenericFilter_h + +/* ================================================= + * @file clitkMedianTemporalDimensionGenericFilter.h + * @author + * @date + * + * @brief + * + ===================================================*/ + + +// clitk include +#include "clitkIO.h" +#include "clitkImageCommon.h" +#include "clitkMedianTemporalDimension_ggo.h" + +//itk include +#include "itkLightObject.h" + +namespace clitk +{ + + template + class ITK_EXPORT MedianTemporalDimensionGenericFilter : public itk::LightObject + { + public: + //---------------------------------------- + // ITK + //---------------------------------------- + typedef MedianTemporalDimensionGenericFilter 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( MedianTemporalDimensionGenericFilter, LightObject ); + + + //---------------------------------------- + // Typedefs + //---------------------------------------- + + + //---------------------------------------- + // Set & Get + //---------------------------------------- + void SetArgsInfo(const args_info_type & a) + { + m_ArgsInfo=a; + m_Verbose=m_ArgsInfo.verbose_flag; + m_InputFileName=m_ArgsInfo.input_arg[0]; + } + + + //---------------------------------------- + // Update + //---------------------------------------- + void Update(); + + protected: + + //---------------------------------------- + // Constructor & Destructor + //---------------------------------------- + MedianTemporalDimensionGenericFilter(); + ~MedianTemporalDimensionGenericFilter() {}; + + + //---------------------------------------- + // Templated members + //---------------------------------------- + template void UpdateWithDim(const std::string PixelType, const int Components); + template void UpdateWithDimAndPixelType(); + + + //---------------------------------------- + // Data members + //---------------------------------------- + args_info_type m_ArgsInfo; + bool m_Verbose; + std::string m_InputFileName; + + }; + + +} // end namespace clitk + +#ifndef ITK_MANUAL_INSTANTIATION +#include "clitkMedianTemporalDimensionGenericFilter.txx" +#endif + +#endif // #define clitkMedianTemporalDimensionGenericFilter_h diff --git a/tools/clitkMedianTemporalDimensionGenericFilter.txx b/tools/clitkMedianTemporalDimensionGenericFilter.txx new file mode 100644 index 0000000..15d6ec2 --- /dev/null +++ b/tools/clitkMedianTemporalDimensionGenericFilter.txx @@ -0,0 +1,221 @@ +/*========================================================================= + 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 clitkMedianTemporalDimensionGenericFilter_txx +#define clitkMedianTemporalDimensionGenericFilter_txx + +/* ================================================= + * @file clitkMedianTemporalDimensionGenericFilter.txx + * @author + * @date + * + * @brief + * + ===================================================*/ + +#include + +namespace clitk +{ + + //----------------------------------------------------------- + // Constructor + //----------------------------------------------------------- + template + MedianTemporalDimensionGenericFilter::MedianTemporalDimensionGenericFilter() + { + m_Verbose=false; + m_InputFileName=""; + } + + + //----------------------------------------------------------- + // Update + //----------------------------------------------------------- + template + void MedianTemporalDimensionGenericFilter::Update() + { + // Read the Dimension and PixelType + int Dimension, Components; + std::string PixelType; + ReadImageDimensionAndPixelType(m_InputFileName, Dimension, PixelType, Components); + + + // Call UpdateWithDim + if (m_ArgsInfo.input_given>1) Dimension+=1; + 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 4D!!!"< + template + void + MedianTemporalDimensionGenericFilter::UpdateWithDim(const std::string PixelType, const int Components) + { + if (m_Verbose) std::cout << "Image was detected to be "<(); + } + else if (PixelType == "unsigned_char") { + if (m_Verbose) std::cout << "Launching filter in "<< Dimension <<"D and unsigned_char..." << std::endl; + UpdateWithDimAndPixelType(); + } + else if (PixelType == "float") { + if (m_Verbose) std::cout << "Launching filter in "<< Dimension <<"D and float..." << std::endl; + UpdateWithDimAndPixelType(); + } + else if (PixelType == "double") { + if (m_Verbose) std::cout << "Launching filter in "<< Dimension <<"D and double..." << std::endl; + UpdateWithDimAndPixelType(); + } + else std::cerr<<"Number of components is "< + template + void + MedianTemporalDimensionGenericFilter::UpdateWithDimAndPixelType() + { + + // ImageTypes + typedef itk::Image InputImageType; + typedef itk::Image OutputImageType; + typename InputImageType::Pointer input; + + if (m_ArgsInfo.input_given ==1 ) { + // Read the input + typedef itk::ImageFileReader InputReaderType; + typename InputReaderType::Pointer reader = InputReaderType::New(); + reader->SetFileName( m_InputFileName); + reader->Update(); + input= reader->GetOutput(); + } + + else { + // Read and join multiple inputs + if (m_Verbose) std::cout< filenames; + for(unsigned int i=0; i ImageReaderType; + typename ImageReaderType::Pointer reader= ImageReaderType::New(); + reader->SetFileNames(filenames); + reader->Update(); + input =reader->GetOutput(); + } + + + // Output properties + typename OutputImageType::RegionType region; + typename OutputImageType::RegionType::SizeType size; + typename OutputImageType::IndexType index; + typename OutputImageType::SpacingType spacing; + typename OutputImageType::PointType origin; + typename InputImageType::RegionType region4D=input->GetLargestPossibleRegion(); + typename InputImageType::RegionType::SizeType size4D=region4D.GetSize(); + typename InputImageType::IndexType index4D=region4D.GetIndex(); + typename InputImageType::SpacingType spacing4D=input->GetSpacing(); + typename InputImageType::PointType origin4D=input->GetOrigin(); + + for (unsigned int i=0; i< Dimension-1; i++) { + size[i]=size4D[i]; + index[i]=index4D[i]; + spacing[i]=spacing4D[i]; + origin[i]=origin4D[i]; + } + region.SetSize(size); + region.SetIndex(index); + typename OutputImageType::Pointer output= OutputImageType::New(); + output->SetRegions(region); + output->SetSpacing(spacing); + output->SetOrigin(origin); + output->Allocate(); + + + // Region iterators + typedef itk::ImageRegionIterator IteratorType; + std::vector iterators(size4D[Dimension-1]); + for (unsigned int i=0; i< size4D[Dimension-1]; i++) { + typename InputImageType::RegionType regionIt=region4D; + typename InputImageType::RegionType::SizeType sizeIt=regionIt.GetSize(); + sizeIt[Dimension-1]=1; + regionIt.SetSize(sizeIt); + typename InputImageType::IndexType indexIt=regionIt.GetIndex(); + indexIt[Dimension-1]=i; + regionIt.SetIndex(indexIt); + iterators[i]=IteratorType(input, regionIt); + } + + typedef itk::ImageRegionIterator OutputIteratorType; + OutputIteratorType avIt(output, output->GetLargestPossibleRegion()); + + // Take the median + double value; + while (!(iterators[0]).IsAtEnd()) { + value=0.; + std::vector temp; + for (unsigned int i=0; i WriterType; + typename WriterType::Pointer writer = WriterType::New(); + writer->SetFileName(m_ArgsInfo.output_arg); + writer->SetInput(output); + writer->Update(); + +} + + +}//end clitk + +#endif //#define clitkMedianTemporalDimensionGenericFilter_txx -- 2.45.2