Orfeo Toolbox  3.16
ImageRegionIterator.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 // Software Guide : BeginCommandLineArgs
20 // INPUTS: {QB_Suburb.png}
21 // OUTPUTS: {ImageRegionIteratorOutput.png}
22 // 10 10 110 140
23 // Software Guide : EndCommandLineArgs
24 
25 // Software Guide : BeginLatex
26 //
27 // \index{Iterators!speed}
28 // The \doxygen{itk}{ImageRegionIterator} is optimized for
29 // iteration speed and is the first choice for iterative, pixel-wise operations
30 // when location in the image is not
31 // important. ImageRegionIterator is the least specialized of the ITK
32 // image iterator classes. It implements all of the methods described in the
33 // preceding section.
34 //
35 // The following example illustrates the use of
36 // \doxygen{itk}{ImageRegionConstIterator} and ImageRegionIterator.
37 // Most of the code constructs introduced apply to other ITK iterators as
38 // well. This simple application crops a subregion from an image by copying
39 // its pixel values into to a second, smaller image.
40 //
41 // \index{Iterators!and image regions}
42 // \index{itk::ImageRegionIterator!example of using|(}
43 // We begin by including the appropriate header files.
44 // Software Guide : EndLatex
45 
46 #include "otbImage.h"
47 // Software Guide : BeginCodeSnippet
49 #include "itkImageRegionIterator.h"
50 // Software Guide : EndCodeSnippet
51 #include "otbImageFileReader.h"
52 #include "otbImageFileWriter.h"
53 
54 int main(int argc, char *argv[])
55 {
56  // Verify the number of parameters on the command line.
57  if (argc < 7)
58  {
59  std::cerr << "Missing parameters. " << std::endl;
60  std::cerr << "Usage: " << std::endl;
61  std::cerr << argv[0]
62  << " inputImageFile outputImageFile startX startY sizeX sizeY"
63  << std::endl;
64  return -1;
65  }
66 
67 // Software Guide : BeginLatex
68 //
69 // Next we define a pixel type and corresponding image type. ITK iterator
70 // classes expect the image type as their template parameter.
71 //
72 // Software Guide : EndLatex
73 
74  // Software Guide : BeginCodeSnippet
75  const unsigned int Dimension = 2;
76 
77  typedef unsigned char PixelType;
78  typedef otb::Image<PixelType, Dimension> ImageType;
79 
80  typedef itk::ImageRegionConstIterator<ImageType> ConstIteratorType;
81  typedef itk::ImageRegionIterator<ImageType> IteratorType;
82  // Software Guide : EndCodeSnippet
83 
84  typedef otb::ImageFileReader<ImageType> ReaderType;
85  typedef otb::ImageFileWriter<ImageType> WriterType;
86 
87 // Software Guide : BeginLatex
88 //
89 // Information about the subregion to copy is read from the command line. The
90 // subregion is defined by an \doxygen{itk}{ImageRegion} object, with a starting
91 // grid index and a size (Section~\ref{sec:ImageSection}).
92 //
93 // Software Guide : EndLatex
94 
95 // Software Guide : BeginCodeSnippet
96  ImageType::RegionType inputRegion;
97 
98  ImageType::RegionType::IndexType inputStart;
99  ImageType::RegionType::SizeType size;
100 
101  inputStart[0] = ::atoi(argv[3]);
102  inputStart[1] = ::atoi(argv[4]);
103 
104  size[0] = ::atoi(argv[5]);
105  size[1] = ::atoi(argv[6]);
106 
107  inputRegion.SetSize(size);
108  inputRegion.SetIndex(inputStart);
109 // Software Guide : EndCodeSnippet
110 
111 // Software Guide : BeginLatex
112 //
113 // The destination region in the output image is defined using the input region
114 // size, but a different start index. The starting index for the destination
115 // region is the corner of the newly generated image.
116 //
117 // Software Guide : EndLatex
118 
119 // Software Guide : BeginCodeSnippet
120  ImageType::RegionType outputRegion;
121 
122  ImageType::RegionType::IndexType outputStart;
123 
124  outputStart[0] = 0;
125  outputStart[1] = 0;
126 
127  outputRegion.SetSize(size);
128  outputRegion.SetIndex(outputStart);
129 // Software Guide : EndCodeSnippet
130 
131  ReaderType::Pointer reader = ReaderType::New();
132  reader->SetFileName(argv[1]);
133  try
134  {
135  reader->Update();
136  }
137  catch (itk::ExceptionObject& err)
138  {
139  std::cerr << "ExceptionObject caught !" << std::endl;
140  std::cerr << err << std::endl;
141  return -1;
142  }
143 
144  // Check that the region is contained within the input image.
145  if (!reader->GetOutput()->GetRequestedRegion().IsInside(inputRegion))
146  {
147  std::cerr << "Error" << std::endl;
148  std::cerr << "The region " << inputRegion <<
149  "is not contained within the input image region "
150  << reader->GetOutput()->GetRequestedRegion() << std::endl;
151  return -1;
152  }
153 
154 // Software Guide : BeginLatex
155 //
156 // After reading the input image and checking that the desired subregion is,
157 // in fact, contained in the input, we allocate an output image. It is
158 // fundamental to set valid values to some of the basic image information
159 // during the copying process.
160 // In particular, the starting index of the output region
161 // is now filled up with zero values and the coordinates of the physical
162 // origin are computed as a shift from the origin of the input image. This is
163 // quite important since it will allow us to later
164 // register the extracted region against the original image.
165 //
166 // Software Guide : EndLatex
167 
168 // Software Guide : BeginCodeSnippet
169  ImageType::Pointer outputImage = ImageType::New();
170  outputImage->SetRegions(outputRegion);
171  const ImageType::SpacingType& spacing = reader->GetOutput()->GetSpacing();
172  const ImageType::PointType& inputOrigin = reader->GetOutput()->GetOrigin();
173  double outputOrigin[Dimension];
174 
175  for (unsigned int i = 0; i < Dimension; ++i)
176  {
177  outputOrigin[i] = inputOrigin[i] + spacing[i] * inputStart[i];
178  }
179 
180  outputImage->SetSpacing(spacing);
181  outputImage->SetOrigin(outputOrigin);
182  outputImage->Allocate();
183 // Software Guide : EndCodeSnippet
184 
185 // Software Guide : BeginLatex
186 //
187 // \index{Iterators!construction of} \index{Iterators!and image regions}
188 // The necessary images and region definitions are now in place. All that is
189 // left to do is to create the iterators and perform the copy. Note that image
190 // iterators are not accessed via smart pointers so they are light-weight
191 // objects that are instantiated on the stack. Also notice how the input and
192 // output iterators are defined over the \emph{same corresponding region}. Though the
193 // images are different sizes, they both contain the same target subregion.
194 //
195 // Software Guide : EndLatex
196 
197 // Software Guide : BeginCodeSnippet
198  ConstIteratorType inputIt(reader->GetOutput(), inputRegion);
199  IteratorType outputIt(outputImage, outputRegion);
200 
201  for (inputIt.GoToBegin(), outputIt.GoToBegin(); !inputIt.IsAtEnd();
202  ++inputIt, ++outputIt)
203  {
204  outputIt.Set(inputIt.Get());
205  }
206 // Software Guide : EndCodeSnippet
207 
208 // Software Guide : BeginLatex
209 //
210 // \index{Iterators!image dimensionality}
211 // The \code{for} loop above is a common
212 // construct in ITK/OTB. The beauty of these four lines of code is that they are
213 // equally valid for one, two, three, or even ten dimensional data, and no
214 // knowledge of the size of the image is necessary. Consider the ugly
215 // alternative of ten nested \code{for} loops for traversing an image.
216 //
217 // Software Guide : EndLatex
218 
219  WriterType::Pointer writer = WriterType::New();
220  writer->SetFileName(argv[2]);
221  writer->SetInput(outputImage);
222 
223  try
224  {
225  writer->Update();
226  }
227  catch (itk::ExceptionObject& err)
228  {
229  std::cerr << "ExceptionObject caught !" << std::endl;
230  std::cerr << err << std::endl;
231  return -1;
232  }
233 
234 // Software Guide : BeginLatex
235 //
236 // Let's run this example on the image \code{QB\_Suburb.png} found
237 // in \code{Examples/Data}. The command line arguments specify the
238 // input and output file names, then the $x$, $y$ origin and the $x$, $y$ size
239 // of the cropped subregion.
240 //
241 // \small
242 // \begin{verbatim}
243 // ImageRegionIterator QB_Suburb.png ImageRegionIteratorOutput.png 20 70 210 140
244 // \end{verbatim}
245 // \normalsize
246 //
247 // The output is the cropped subregion shown in
248 // Figure~\ref{fig:ImageRegionIteratorOutput}.
249 //
250 // \begin{figure}
251 // \centering
252 // \includegraphics[width=0.4\textwidth]{QB_Suburb.eps}
253 // \includegraphics[width=0.3\textwidth]{ImageRegionIteratorOutput.eps}
254 // \itkcaption[Copying an image subregion using ImageRegionIterator]{Cropping a
255 // region from an image. The original image is shown at left. The image on
256 // the right is the result of applying the ImageRegionIterator example code.}
257 // \protect\label{fig:ImageRegionIteratorOutput}
258 // \end{figure}
259 //
260 // \index{itk::ImageRegionIterator!example of using|)}
261 //
262 // Software Guide : EndLatex
263 
264  return EXIT_SUCCESS;
265 }

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