Orfeo Toolbox  3.16
ImageAdaptor3.cxx
Go to the documentation of this file.
1 /*=========================================================================
2 
3  Program: ORFEO Toolbox
4  Language: C++
5  Date: $Date$
6  Version: $Revision$
7 
8 
9  Copyright (c) Centre National d'Etudes Spatiales. All rights reserved.
10  See OTBCopyright.txt for details.
11 
12 
13  This software is distributed WITHOUT ANY WARRANTY; without even
14  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15  PURPOSE. See the above copyright notices for more information.
16 
17 =========================================================================*/
18 
19 
20 // Software Guide : BeginLatex
21 //
22 // This example illustrates the use of \doxygen{itk}{ImageAdaptor}
23 // to obtain access to the components of a vector image.
24 // Specifically, it shows how to manage pixel accessors containing
25 // internal parameters. In this example we create an image of vectors by using
26 // a gradient filter. Then, we use an image adaptor to extract one of the
27 // components of the vector image. The vector type used by the gradient filter
28 // is the \doxygen{itk}{CovariantVector} class.
29 //
30 // We start by including the relevant headers.
31 //
32 // \index{itk::ImageAdaptor!Instantiation}
33 // \index{itk::ImageAdaptor!Header}
34 // \index{itk::PixelAccessor!with parameters}
35 //
36 // Software Guide : EndLatex
37 
38 #include "otbImage.h"
39 #include "itkImageAdaptor.h"
41 #include "otbImageFileReader.h"
42 #include "otbImageFileWriter.h"
44 
45 // Software Guide : BeginCodeSnippet
46 #include "itkCovariantVector.h"
48 // Software Guide : EndCodeSnippet
49 
50 // Software Guide : BeginLatex
51 //
52 // A pixel accessors class may have internal parameters that affect the
53 // operations performed on input pixel data. Image adaptors support
54 // parameters in their internal pixel accessor by using
55 // the assignment operator. Any pixel accessor which has internal
56 // parameters must therefore implement the assignment operator.
57 // The following defines a pixel accessor for extracting
58 // components from a vector pixel. The
59 // \code{m\_Index} member variable is used to select the vector component
60 // to be returned.
61 //
62 // Software Guide : EndLatex
63 
64 // Software Guide : BeginCodeSnippet
66 {
67 public:
69  typedef float ExternalType;
70 
72  {
73  m_Index = vpa.m_Index;
74  }
75  ExternalType Get(const InternalType& input) const
76  {
77  return static_cast<ExternalType>(input[m_Index]);
78  }
79  void SetIndex(unsigned int index)
80  {
81  m_Index = index;
82  }
83 private:
84  unsigned int m_Index;
85 };
86 // Software Guide : EndCodeSnippet
87 
88 // Software Guide : BeginLatex
89 //
90 // The \code{Get()} method simply returns the \emph{i}-th component of
91 // the vector as indicated by the index. The assignment operator transfers the
92 // value of the index member variable from one instance of the pixel accessor
93 // to another.
94 //
95 // Software Guide : EndLatex
96 
97 //-------------------------
98 //
99 // Main code
100 //
101 //-------------------------
102 
103 int main(int argc, char *argv[])
104 {
105  if (argc < 4)
106  {
107  std::cerr << "Usage: " << std::endl;
108  std::cerr << "ImageAdaptor3 inputFileName outputComponentFileName ";
109  std::cerr << " indexOfComponentToExtract" << std::endl;
110  return -1;
111  }
112 
113 // Software Guide : BeginLatex
114 //
115 // In order to test the pixel accessor, we generate an image of vectors using
116 // the \doxygen{itk}{GradientRecursiveGaussianImageFilter}. This
117 // filter produces an output image of \doxygen{itk}{CovariantVector} pixel type.
118 // Covariant vectors are the natural representation for gradients since they
119 // are the equivalent of normals to iso-values manifolds.
120 //
121 // Software Guide : EndLatex
122 
123 // Software Guide : BeginCodeSnippet
124  typedef unsigned char InputPixelType;
125  const unsigned int Dimension = 2;
126  typedef otb::Image<InputPixelType, Dimension> InputImageType;
127  typedef itk::CovariantVector<float, Dimension> VectorPixelType;
128  typedef otb::Image<VectorPixelType, Dimension> VectorImageType;
129  typedef itk::GradientRecursiveGaussianImageFilter<InputImageType,
130  VectorImageType>
131  GradientFilterType;
132 
133  GradientFilterType::Pointer gradient = GradientFilterType::New();
134 // Software Guide : EndCodeSnippet
135 
136 // Software Guide : BeginLatex
137 //
138 // We instantiate the ImageAdaptor using the vector image type as
139 // the first template parameter and the pixel accessor as the second
140 // template parameter.
141 //
142 // Software Guide : EndLatex
143 
144 // Software Guide : BeginCodeSnippet
145  typedef itk::ImageAdaptor<VectorImageType,
146  VectorPixelAccessor> ImageAdaptorType;
147 
148  ImageAdaptorType::Pointer adaptor = ImageAdaptorType::New();
149 // Software Guide : EndCodeSnippet
150 
151 // Software Guide : BeginLatex
152 //
153 // The index of the component to be extracted is specified
154 // from the command line. In the following, we create the accessor,
155 // set the index and connect the accessor to the image adaptor using
156 // the \code{SetPixelAccessor()} method.
157 //
158 // Software Guide : EndLatex
159 
160 // Software Guide : BeginCodeSnippet
161  VectorPixelAccessor accessor;
162  accessor.SetIndex(atoi(argv[3]));
163  adaptor->SetPixelAccessor(accessor);
164 // Software Guide : EndCodeSnippet
165 
166 // Software Guide : BeginLatex
167 //
168 // We create a reader to load the image specified from the
169 // command line and pass its output as the input to the gradient filter.
170 //
171 // Software Guide : EndLatex
172 
173 // Software Guide : BeginCodeSnippet
174  typedef otb::ImageFileReader<InputImageType> ReaderType;
175  ReaderType::Pointer reader = ReaderType::New();
176  gradient->SetInput(reader->GetOutput());
177 
178  reader->SetFileName(argv[1]);
179  gradient->Update();
180 // Software Guide : EndCodeSnippet
181 
182 // Software Guide : BeginLatex
183 //
184 // We now connect the output of the gradient filter as input to the
185 // image adaptor. The adaptor emulates a scalar image whose pixel values
186 // are taken from the selected component of the vector image.
187 //
188 // Software Guide : EndLatex
189 
190 // Software Guide : BeginCodeSnippet
191  adaptor->SetImage(gradient->GetOutput());
192 // Software Guide : EndCodeSnippet
193 
194  typedef otb::Image<unsigned char, Dimension> OutputImageType;
196  RescalerType;
197  RescalerType::Pointer rescaler = RescalerType::New();
198  typedef otb::ImageFileWriter<OutputImageType> WriterType;
199  WriterType::Pointer writer = WriterType::New();
200 
201  writer->SetFileName(argv[2]);
202 
203  rescaler->SetOutputMinimum(0);
204  rescaler->SetOutputMaximum(255);
205 
206  rescaler->SetInput(adaptor);
207  writer->SetInput(rescaler->GetOutput());
208  writer->Update();
209 
210  return EXIT_SUCCESS;
211 }

Generated at Sat Feb 2 2013 23:22:53 for Orfeo Toolbox with doxygen 1.8.1.1