Orfeo Toolbox  3.16
ImageAdaptor2.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 to use the \doxygen{itk}{ImageAdaptor}
23 // to access the individual components of an RGB image. In this case, we
24 // create an ImageAdaptor that will accept a RGB image as input and
25 // presents it as a scalar image. The pixel data
26 // will be taken directly from the red channel of the original image.
27 //
28 // \index{itk::ImageAdaptor!Instantiation}
29 // \index{itk::ImageAdaptor!Header}
30 //
31 // Software Guide : EndLatex
32 
33 #include "otbImage.h"
34 #include "itkImageAdaptor.h"
36 #include "otbImageFileReader.h"
37 #include "otbImageFileWriter.h"
39 
40 // Software Guide : BeginLatex
41 //
42 // As with the previous example, the bulk of the effort in creating the image
43 // adaptor is associated with the definition of the pixel accessor class. In
44 // this case, the accessor converts a RGB vector to a scalar containing the
45 // red channel component. Note that in the following, we do not need to define
46 // the \code{Set()} method since we only expect the adaptor to be used for
47 // reading data from the image.
48 //
49 // Software Guide : EndLatex
50 
51 // Software Guide : BeginCodeSnippet
53 {
54 public:
56  typedef float ExternalType;
57 
58  static ExternalType Get(const InternalType& input)
59  {
60  return static_cast<ExternalType>(input.GetRed());
61  }
62 };
63 // Software Guide : EndCodeSnippet
64 
65 // Software Guide : BeginLatex
66 //
67 // The \code{Get()} method simply calls the \code{GetRed()} method
68 // defined in the \doxygen{itk}{RGBPixel} class.
69 //
70 // Software Guide : EndLatex
71 
72 //-------------------------
73 //
74 // Main code
75 //
76 //-------------------------
77 
78 int main(int argc, char *argv[])
79 {
80  if (argc < 3)
81  {
82  std::cerr << "Usage: " << std::endl;
83  std::cerr << "ImageAdaptor2 inputRGBFileName outputRedChannelFileName" <<
84  std::endl;
85  return -1;
86  }
87 
88 // Software Guide : BeginLatex
89 //
90 // Now we use the internal pixel type of the pixel accessor to define the
91 // input image type, and then proceed to instantiate the ImageAdaptor type.
92 //
93 // \index{PixelAccessor!RGB red channel}
94 // \index{itk::ImageAdaptor!RGB red channel}
95 // \index{ImageAdaptor!RGB red channel}
96 //
97 // Software Guide : EndLatex
98 
99 // Software Guide : BeginCodeSnippet
100  typedef RedChannelPixelAccessor::InternalType InputPixelType;
101  const unsigned int Dimension = 2;
102  typedef otb::Image<InputPixelType, Dimension> ImageType;
103 
104  typedef itk::ImageAdaptor<ImageType,
105  RedChannelPixelAccessor> ImageAdaptorType;
106 
107  ImageAdaptorType::Pointer adaptor = ImageAdaptorType::New();
108 // Software Guide : EndCodeSnippet
109 
110 // Software Guide : BeginLatex
111 //
112 // We create an image reader and connect the output to the adaptor
113 // as before.
114 //
115 // Software Guide : EndLatex
116 
117 // Software Guide : BeginCodeSnippet
118  typedef otb::ImageFileReader<ImageType> ReaderType;
119  ReaderType::Pointer reader = ReaderType::New();
120 // Software Guide : EndCodeSnippet
121 
122  reader->SetFileName(argv[1]);
123  reader->Update();
124 
125 // Software Guide : BeginCodeSnippet
126  adaptor->SetImage(reader->GetOutput());
127 // Software Guide : EndCodeSnippet
128 
129 // Software Guide : BeginLatex
130 //
131 // We create an \doxygen{itk}{RescaleIntensityImageFilter} and an
132 // \doxygen{otb}{ImageFileWriter} to rescale the dynamic range of the pixel values
133 // and send the extracted channel to an image file. Note that the image type
134 // used for the rescaling filter is the \code{ImageAdaptorType} itself. That
135 // is, the adaptor type is used in the same context as an image type.
136 //
137 // Software Guide : EndLatex
138 
139 // Software Guide : BeginCodeSnippet
140  typedef otb::Image<unsigned char, Dimension> OutputImageType;
141  typedef itk::RescaleIntensityImageFilter<ImageAdaptorType,
142  OutputImageType
143  > RescalerType;
144 
145  RescalerType::Pointer rescaler = RescalerType::New();
146  typedef otb::ImageFileWriter<OutputImageType> WriterType;
147  WriterType::Pointer writer = WriterType::New();
148 // Software Guide : EndCodeSnippet
149 
150  writer->SetFileName(argv[2]);
151 
152 // Software Guide : BeginLatex
153 //
154 // Now we connect the adaptor as the input to the rescaler and set the
155 // parameters for the intensity rescaling.
156 //
157 // Software Guide : EndLatex
158 
159 // Software Guide : BeginCodeSnippet
160  rescaler->SetOutputMinimum(0);
161  rescaler->SetOutputMaximum(255);
162 
163  rescaler->SetInput(adaptor);
164  writer->SetInput(rescaler->GetOutput());
165 // Software Guide : EndCodeSnippet
166 
167 // Software Guide : BeginLatex
168 //
169 // Finally, we invoke the \code{Update()} method on the writer and take
170 // precautions to catch any exception that may be thrown during
171 // the execution of the pipeline.
172 //
173 // Software Guide : EndLatex
174 
175 // Software Guide : BeginCodeSnippet
176  try
177  {
178  writer->Update();
179  }
180  catch (itk::ExceptionObject& excp)
181  {
182  std::cerr << "Exception caught " << excp << std::endl;
183  return 1;
184  }
185 // Software Guide : EndCodeSnippet
186 
187 // Software Guide : BeginLatex
188 //
189 // ImageAdaptors for the green and blue channels can easily be implemented by
190 // modifying the pixel accessor of the red channel and then using the
191 // new pixel accessor for instantiating the type of an image adaptor.
192 // The following define a green channel pixel accessor.
193 //
194 // \index{PixelAccessor!RGB green channel}
195 // \index{itk::ImageAdaptor!RGB green channel}
196 // \index{ImageAdaptor!RGB green channel}
197 //
198 // Software Guide : EndLatex
199 
200 // Software Guide : BeginCodeSnippet
201  class GreenChannelPixelAccessor
202  {
203 public:
204  typedef itk::RGBPixel<float> InternalType;
205  typedef float ExternalType;
206 
207  static ExternalType Get(const InternalType& input)
208  {
209  return static_cast<ExternalType>(input.GetGreen());
210  }
211  };
212 // Software Guide : EndCodeSnippet
213 
214 // Software Guide : BeginLatex
215 //
216 // A blue channel pixel accessor is similarly defined.
217 //
218 // \index{PixelAccessor!RGB blue channel}
219 // \index{itk::ImageAdaptor!RGB blue channel}
220 // \index{ImageAdaptor!RGB blue channel}
221 //
222 // Software Guide : EndLatex
223 
224 // Software Guide : BeginCodeSnippet
225  class BlueChannelPixelAccessor
226  {
227 public:
228  typedef itk::RGBPixel<float> InternalType;
229  typedef float ExternalType;
230 
231  static ExternalType Get(const InternalType& input)
232  {
233  return static_cast<ExternalType>(input.GetBlue());
234  }
235  };
236 // Software Guide : EndCodeSnippet
237 
238  return EXIT_SUCCESS;
239 }

Generated at Sat May 18 2013 23:24:31 for Orfeo Toolbox with doxygen 1.8.3.1