]> Creatis software - clitk.git/blob - tools/clitkImageExtractLine.cxx
temporal median of a sequence of images, to be used for the mid P project
[clitk.git] / tools / clitkImageExtractLine.cxx
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://oncora1.lyon.fnclcc.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 CLITKIMAGEEXTRACTLINE_CXX
19 #define CLITKIMAGEEXTRACTLINE_CXX
20 /**
21    -------------------------------------------------
22    * @file   clitkImageExtractLine.cxx
23    * @author David Sarrut <David.Sarrut@creatis.insa-lyon.fr>
24    * @date   23 Feb 2008 08:37:53
25    -------------------------------------------------*/
26
27 // clitk include
28 #include "clitkImageExtractLine_ggo.h"
29 #include "clitkIO.h"
30 #include "clitkImageCommon.h"
31 #include "clitkCommon.h"
32 #include <itkLineConstIterator.h>
33
34 //--------------------------------------------------------------------
35 int main(int argc, char * argv[])
36 {
37
38   // Init command line
39   GGO(clitkImageExtractLine, args_info);
40   CLITK_INIT;
41
42   // Declare main types
43   typedef float PixelType;
44   const unsigned int Dimension=3;
45   typedef itk::Image<PixelType, Dimension> ImageType;
46
47   // Check options
48   if (args_info.firstIndex_given != Dimension) {
49     std::cerr << "Please give " << Dimension << "values to --firstIndex option" << std::endl;
50     exit(0);
51   }
52   if (args_info.lastIndex_given != Dimension) {
53     std::cerr << "Please give " << Dimension << "values to --lastIndex option" << std::endl;
54     exit(0);
55   }
56
57   // Read image
58   ImageType::Pointer input = clitk::readImage<ImageType>(args_info.input_arg, args_info.verbose_flag);
59
60   // Get first and last index
61   typedef ImageType::IndexType IndexType;
62   IndexType firstIndex;
63   IndexType lastIndex;
64   ImageType::SpacingType spacing = input->GetSpacing();
65   double length = 0.0;
66   for(unsigned int i=0; i<Dimension; i++) {
67     firstIndex[i] = args_info.firstIndex_arg[i];
68     lastIndex[i] = args_info.lastIndex_arg[i];
69     if (args_info.mm_flag) {
70       firstIndex[i] /= spacing[i];
71       lastIndex[i] /= spacing[i];
72     }
73     length += pow(lastIndex[i]*spacing[i]-firstIndex[i]*spacing[i],2);
74   }
75   length = sqrt(length);
76
77   // Loop
78   std::vector<double> depth;
79   std::vector<double> values;
80   itk::LineConstIterator<ImageType> iter(input, firstIndex, lastIndex);
81   iter.GoToBegin();
82   while (!iter.IsAtEnd()) {
83     values.push_back(iter.Get());
84     ++iter;
85   }
86   double step = length/values.size();
87
88   // If isocenter is used
89   double isoDistance = 0.0;
90   if (args_info.isocenter_given) { // isoCenter is in mm
91     IndexType isoCenter;
92     for(unsigned int i=0; i<Dimension; i++) {
93       isoCenter[i] = args_info.isocenter_arg[i];
94       isoDistance += pow(isoCenter[i] - firstIndex[i]*spacing[i],2);
95     }
96     DD(isoCenter);
97     isoDistance = sqrt(isoDistance);
98     DD(isoDistance);
99   }
100
101   // Write result
102   std::ofstream os(args_info.output_arg);
103   os << "# clitkImageExtractLine from " << args_info.input_arg << std::endl
104      << "# \t firstIndex = " << firstIndex << std::endl
105      << "# \t lastIndex  = " << lastIndex << std::endl
106      << "# \t nb values  = " << values.size() << std::endl
107      << "# \t length   = " << length << std::endl
108      << "# \t step       = " << step << std::endl;
109   if (args_info.depth_flag) {
110     double lg = -isoDistance;
111     for(unsigned int i=0; i<values.size(); i++) {
112       os << lg << " " << values[i] << std::endl;
113       lg += step;
114     }
115     os.close();
116   } else {
117     for(unsigned int i=0; i<values.size(); i++) {
118       os << values[i] << std::endl;
119     }
120     os.close();
121   }
122
123   // this is the end my friend
124   return 0;
125 } // end main
126
127 #endif //define CLITKIMAGEEXTRACTLINE_CXX