Orfeo Toolbox  3.16
ImageAdaptor1.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 how the \doxygen{itk}{ImageAdaptor} can be used to cast
23 // an image from one pixel type to another. In particular, we will
24 // \emph{adapt} an \code{unsigned char} image to make it appear as an image of
25 // pixel type \code{float}.
26 //
27 // \index{itk::ImageAdaptor!Instantiation}
28 // \index{itk::ImageAdaptor!Header}
29 //
30 // We begin by including the relevant headers.
31 //
32 // Software Guide : EndLatex
33 
34 // Software Guide : BeginCodeSnippet
35 #include "otbImage.h"
36 #include "itkImageAdaptor.h"
37 // Software Guide : EndCodeSnippet
38 
40 #include "otbImageFileReader.h"
41 
42 // Software Guide : BeginLatex
43 //
44 // First, we need to define a \emph{pixel accessor} class that does the actual
45 // conversion. Note that in general, the only valid operations for pixel
46 // accessors are those that only require the value of the input pixel. As
47 // such, neighborhood type operations are not possible. A pixel accessor must
48 // provide methods \code{Set()} and \code{Get()}, and define the types of
49 // \code{InternalPixelType} and \code{ExternalPixelType}. The
50 // \code{InternalPixelType} corresponds to the pixel type of the image to be
51 // adapted (\code{unsigned char} in this example). The \code{ExternalPixelType}
52 // corresponds to the pixel type we wish to emulate with the ImageAdaptor
53 // (\code{float} in this case).
54 //
55 // Software Guide : EndLatex
56 
57 // Software Guide : BeginCodeSnippet
59 {
60 public:
61  typedef unsigned char InternalType;
62  typedef float ExternalType;
63 
64  static void Set(InternalType& output, const ExternalType& input)
65  {
66  output = static_cast<InternalType>(input);
67  }
68 
69  static ExternalType Get(const InternalType& input)
70  {
71  return static_cast<ExternalType>(input);
72  }
73 };
74 // Software Guide : EndCodeSnippet
75 
76 //-------------------------
77 //
78 // Main code
79 //
80 //-------------------------
81 
82 int main(int argc, char *argv[])
83 {
84  if (argc < 2)
85  {
86  std::cerr << "Usage: " << std::endl;
87  std::cerr << "ImageAdaptor1 inputFileName" << std::endl;
88  return -1;
89  }
90 
91 // Software Guide : BeginLatex
92 //
93 // The CastPixelAccessor class simply applies a
94 // \code{static\_cast} to the pixel values. We now use this pixel accessor
95 // to define the image adaptor type and create an instance using
96 // the standard \code{New()} method.
97 //
98 // Software Guide : EndLatex
99 
100 // Software Guide : BeginCodeSnippet
101  typedef unsigned char InputPixelType;
102  const unsigned int Dimension = 2;
103  typedef otb::Image<InputPixelType, Dimension> ImageType;
104 
105  typedef itk::ImageAdaptor<ImageType, CastPixelAccessor> ImageAdaptorType;
106  ImageAdaptorType::Pointer adaptor = ImageAdaptorType::New();
107 // Software Guide : EndCodeSnippet
108 
109 // Software Guide : BeginLatex
110 //
111 // We also create an image reader templated over the input image type and
112 // read the input image from file.
113 //
114 // Software Guide : EndLatex
115 
116 // Software Guide : BeginCodeSnippet
117  typedef otb::ImageFileReader<ImageType> ReaderType;
118  ReaderType::Pointer reader = ReaderType::New();
119 // Software Guide : EndCodeSnippet
120 
121  reader->SetFileName(argv[1]);
122  reader->Update();
123 
124 // Software Guide : BeginLatex
125 //
126 // The output of the reader is then connected as the input to the image
127 // adaptor.
128 //
129 // Software Guide : EndLatex
130 
131 // Software Guide : BeginCodeSnippet
132  adaptor->SetImage(reader->GetOutput());
133 // Software Guide : EndCodeSnippet
134 
135 // Software Guide : BeginLatex
136 //
137 // In the following code, we visit the image using an iterator
138 // instantiated using the adapted image type and compute the
139 // sum of the pixel values.
140 //
141 // Software Guide : EndLatex
142 
143 // Software Guide : BeginCodeSnippet
145  IteratorType it(adaptor, adaptor->GetBufferedRegion());
146 
147  double sum = 0.0;
148  it.GoToBegin();
149  while (!it.IsAtEnd())
150  {
151  float value = it.Get();
152  sum += value;
153  ++it;
154  }
155 // Software Guide : EndCodeSnippet
156 
157  std::cout << "Sum of pixels is: " << sum << std::endl;
158 
159 // Software Guide : BeginLatex
160 //
161 // Although in this example, we are just performing a simple summation, the key
162 // concept is that access to pixels is performed as if the pixel is of type
163 // \code{float}. Additionally, it should be noted that the adaptor is used
164 // as if it was an actual image and not as a filter. ImageAdaptors conform
165 // to the same API as the \doxygen{otb}{Image} class.
166 //
167 // Software Guide : EndLatex
168 
169  return EXIT_SUCCESS;
170 }

Generated at Sat Jun 15 2013 23:24:25 for Orfeo Toolbox with doxygen 1.8.3.1