]> Creatis software - clitk.git/blob - tools/clitkJacobianImageGenericFilter.txx
close #59 #58 Change clitkAffineTransform --transform_grid
[clitk.git] / tools / clitkJacobianImageGenericFilter.txx
1 /*=========================================================================
2   Program:   vv                     http://www.creatis.insa-lyon.fr/rio/vv
3
4   Authors belong to:
5   - University of LYON              http://www.universite-lyon.fr/
6   - Léon Bérard cancer center       http://www.centreleonberard.fr
7   - CREATIS CNRS laboratory         http://www.creatis.insa-lyon.fr
8
9   This software is distributed WITHOUT ANY WARRANTY; without even
10   the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
11   PURPOSE.  See the copyright notices for more information.
12
13   It is distributed under dual licence
14
15   - BSD        See included LICENSE.txt file
16   - CeCILL-B   http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
17 ===========================================================================**/
18 #ifndef clitkJacobianImageGenericFilter_txx
19 #define clitkJacobianImageGenericFilter_txx
20
21 /* =================================================
22  * @file   clitkJacobianImageGenericFilter.txx
23  * @author
24  * @date
25  *
26  * @brief
27  *
28  ===================================================*/
29
30 #include "clitkImageCommon.h"
31 #include "itkDisplacementFieldJacobianDeterminantFilter.h"
32 #include "itkImage.h"
33 #include "itkImageFileReader.h"
34 #include "itkImageFileWriter.h"
35 #include "itkVector.h"
36 #include "itkNormalizeImageFilter.h"
37
38 namespace clitk
39 {
40
41 //-----------------------------------------------------------
42 // Constructor
43 //-----------------------------------------------------------
44 template<class args_info_type>
45 JacobianImageGenericFilter<args_info_type>::JacobianImageGenericFilter()
46 {
47   m_Verbose=false;
48   m_InputFileName="";
49 }
50
51
52 //-----------------------------------------------------------
53 // Update
54 //-----------------------------------------------------------
55 template<class args_info_type>
56 void JacobianImageGenericFilter<args_info_type>::Update()
57 {
58   // Read the Dimension and PixelType
59   int Dimension;
60   std::string PixelType;
61   ReadImageDimensionAndPixelType(m_InputFileName, Dimension, PixelType);
62
63
64   // Call UpdateWithDim
65   if(Dimension==2) UpdateWithDim<2>(PixelType);
66   else if(Dimension==3) UpdateWithDim<3>(PixelType);
67   // else if (Dimension==4)UpdateWithDim<4>(PixelType);
68   else {
69     std::cout<<"Error, Only for 2 or 3  Dimensions!!!"<<std::endl ;
70     return;
71   }
72 }
73
74 //-------------------------------------------------------------------
75 // Update with the number of dimensions
76 //-------------------------------------------------------------------
77 template<class args_info_type>
78 template<unsigned int Dimension>
79 void
80 JacobianImageGenericFilter<args_info_type>::UpdateWithDim(std::string PixelType)
81 {
82   if (m_Verbose) std::cout << "Image was detected to be "<<Dimension<<"D and "<< PixelType<<"..."<<std::endl;
83
84   //    if(PixelType == "short"){
85   //       if (m_Verbose) std::cout << "Launching filter in "<< Dimension <<"D and signed short..." << std::endl;
86   //       UpdateWithDimAndPixelType<Dimension, signed short>();
87   //     }
88   //    else if(PixelType == "unsigned_short"){
89   //       if (m_Verbose) std::cout  << "Launching filter in "<< Dimension <<"D and unsigned_short..." << std::endl;
90   //       UpdateWithDimAndPixelType<Dimension, unsigned short>();
91   //     }
92
93   //     else if (PixelType == "unsigned_char"){
94   //       if (m_Verbose) std::cout  << "Launching filter in "<< Dimension <<"D and unsigned_char..." << std::endl;
95   //       UpdateWithDimAndPixelType<Dimension, unsigned char>();
96   //     }
97
98   //     else if (PixelType == "char"){
99   //       if (m_Verbose) std::cout  << "Launching filter in "<< Dimension <<"D and signed_char..." << std::endl;
100   //       UpdateWithDimAndPixelType<Dimension, signed char>();
101   //     }
102   //  else {
103   if (m_Verbose) std::cout  << "Launching filter in "<< Dimension <<"D and float..." << std::endl;
104   UpdateWithDimAndPixelType<Dimension, itk::Vector<float, Dimension> >();
105   // }
106 }
107
108
109 //-------------------------------------------------------------------
110 // Update with the number of dimensions and the pixeltype
111 //-------------------------------------------------------------------
112 template<class args_info_type>
113 template <unsigned int Dimension, class  PixelType>
114 void
115 JacobianImageGenericFilter<args_info_type>::UpdateWithDimAndPixelType()
116 {
117   std::string vfield_file = m_ArgsInfo.input_arg;
118   
119   const unsigned int dim = Dimension;
120   typedef itk::Vector<double, dim> VectorType;
121   typedef itk::Image<VectorType, dim> VectorFieldType;
122   typedef itk::ImageFileReader<VectorFieldType> VectorFieldReaderType;
123   
124   typename VectorFieldReaderType::Pointer vfield_reader = VectorFieldReaderType::New();
125   vfield_reader->SetFileName(vfield_file.c_str());
126   
127   typedef double OutputPixelType;
128   typedef itk::Image<OutputPixelType, dim> ImageType;
129   typedef itk::DisplacementFieldJacobianDeterminantFilter<VectorFieldType, OutputPixelType, ImageType> JacobianFilterType;
130
131   typename VectorFieldType::Pointer vfield = vfield_reader->GetOutput();
132   typename JacobianFilterType::Pointer jac = JacobianFilterType::New();
133   jac->SetInput(vfield);
134   jac->Update();
135   
136   typedef itk::NormalizeImageFilter<ImageType, ImageType> NormalizeFilterType;
137   typename NormalizeFilterType::Pointer normalize = NormalizeFilterType::New();
138   normalize->SetInput(jac->GetOutput());
139   normalize->Update();
140
141   typedef itk::ImageFileWriter<ImageType> ImageFileWriterType;
142   typename ImageFileWriterType::Pointer image_writer = ImageFileWriterType::New();
143   
144   std::string image_file = m_ArgsInfo.output_arg;
145   image_writer->SetFileName(image_file.c_str());
146   image_writer->SetInput(jac->GetOutput());
147   image_writer->Update();
148   
149 }
150
151
152 }//end clitk
153
154 #endif //#define clitkJacobianImageGenericFilter_txx