Orfeo Toolbox  3.16
ImageLinearIteratorWithIndex.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 // The \doxygen{itk}{ImageLinearIteratorWithIndex} is designed for line-by-line
23 // processing of an image. It walks a linear path along a selected image
24 // direction parallel to one of the coordinate axes of the image. This
25 // iterator conceptually breaks an image into a set of parallel lines
26 // that span the selected image dimension.
27 //
28 // \index{Iterators!and image lines}
29 //
30 // Like all image iterators, movement of the
31 // ImageLinearIteratorWithIndex is constrained within an
32 // image region $R$. The line $\ell$ through which the iterator moves is
33 // defined by selecting a direction and an origin. The line $\ell$
34 // extends from the origin to the upper boundary of $R$. The origin can be
35 // moved to any position along the lower boundary of $R$.
36 //
37 // Several additional methods are defined for this iterator to control movement
38 // of the iterator along the line $\ell$ and movement of the origin of $\ell$.
39 //
40 // %Might need a figure here to describe this iterator.
41 //
42 // \begin{itemize}
43 //
44 // \index{itk::ImageLinearIteratorWithIndex!NextLine()}
45 //
46 // \item \textbf{\code{NextLine()}} Moves the iterator to the beginning pixel
47 // location of the next line in the image. The origin of the next line is
48 // determined by incrementing the current origin along the fastest increasing
49 // dimension of the subspace of the image that excludes the selected dimension.
50 //
51 //
52 // \index{itk::ImageLinearIteratorWithIndex!PreviousLine()}
53 //
54 // \item \textbf{\code{PreviousLine()}} Moves the iterator to the \emph{last valid
55 // pixel location} in the previous line. The origin of the previous line is
56 // determined by decrementing the current origin along the fastest increasing
57 // dimension of the subspace of the image that excludes the selected dimension.
58 //
59 // \index{itk::ImageLinearIteratorWithIndex!GoToBeginOfLine()}
60 // \index{itk::ImageLinearIteratorWithIndex!GoToReverseBeginOfLine()}
61 //
62 // \item \textbf{\code{GoToBeginOfLine()}} Moves the iterator to the beginning
63 // pixel of the current line.
64 //
65 // \item \textbf{\code{GoToEndOfLine()}} Move the iterator to
66 // \emph{one past} the last valid pixel of the current line.
67 //
68 //
69 // \index{itk::ImageLinearIteratorWithIndex!IsAtReverseEndOfLine()}
70 // \index{itk::ImageLinearIteratorWithIndex!IsAtEndOfLine()}
71 //
72 // \item \textbf{\code{IsAtReverseEndOfLine()}}
73 // Returns true if the iterator points
74 // to \emph{one position before} the beginning pixel of the current line.
75 //
76 // \item \textbf{\code{IsAtEndOfLine()}}
77 // Returns true if the iterator points to
78 // \emph{one position past} the last valid pixel of the current line.
79 // \end{itemize}
80 //
81 // The following code example shows how to use the
82 // ImageLinearIteratorWithIndex. It implements the same algorithm as
83 // in the previous example, flipping an image across its $x$-axis. Two line
84 // iterators are iterated in opposite directions across the $x$-axis.
85 // After each line is traversed, the iterator origins are stepped along
86 // the $y$-axis to the
87 // next line.
88 //
89 // \index{itk::ImageLinearIteratorWithIndex!example of using|(}
90 //
91 // Headers for both the const and non-const versions are needed.
92 //
93 // Software Guide : EndLatex
94 
95 #include "otbImage.h"
96 #include "itkRGBPixel.h"
97 // Software Guide : BeginCodeSnippet
100 // Software Guide : EndCodeSnippet
101 #include "otbImageFileReader.h"
102 #include "otbImageFileWriter.h"
103 
104 int main(int argc, char *argv[])
105 {
106  // Verify the number of parameters on the command line.
107  if (argc < 3)
108  {
109  std::cerr << "Missing parameters. " << std::endl;
110  std::cerr << "Usage: " << std::endl;
111  std::cerr << argv[0]
112  << " inputImageFile outputImageFile"
113  << std::endl;
114  return -1;
115  }
116 
117 // Software Guide : BeginLatex
118 //
119 // The RGB image and pixel types are defined as in the previous example. The
120 // ImageLinearIteratorWithIndex class and its const version each have
121 // single template parameters, the image type.
122 //
123 // Software Guide : EndLatex
124 
125  const unsigned int Dimension = 2;
126 
127  typedef itk::RGBPixel<unsigned char> RGBPixelType;
128  typedef otb::Image<RGBPixelType, Dimension> ImageType;
129 
130 // Software Guide : BeginCodeSnippet
132  typedef itk::ImageLinearConstIteratorWithIndex<ImageType> ConstIteratorType;
133 // Software Guide : EndCodeSnippet
134 
135  typedef otb::ImageFileReader<ImageType> ReaderType;
136  typedef otb::ImageFileWriter<ImageType> WriterType;
137 
138  ImageType::ConstPointer inputImage;
139  ReaderType::Pointer reader = ReaderType::New();
140  reader->SetFileName(argv[1]);
141  try
142  {
143  reader->Update();
144  inputImage = reader->GetOutput();
145  }
146  catch (itk::ExceptionObject& err)
147  {
148  std::cout << "ExceptionObject caught a !" << std::endl;
149  std::cout << err << std::endl;
150  return -1;
151  }
152 
153 // Software Guide : BeginLatex
154 //
155 // After reading the input image, we allocate an output image that of the same
156 // size, spacing, and origin.
157 //
158 // Software Guide : EndLatex
159 
160 // Software Guide : BeginCodeSnippet
161  ImageType::Pointer outputImage = ImageType::New();
162  outputImage->SetRegions(inputImage->GetRequestedRegion());
163  outputImage->CopyInformation(inputImage);
164  outputImage->Allocate();
165 // Software Guide : EndCodeSnippet
166 
167 // Software Guide : BeginLatex
168 //
169 // Next we create the two iterators. The const iterator walks the input image,
170 // and the non-const iterator walks the output image. The iterators are
171 // initialized over the same region. The direction of iteration is set to 0,
172 // the $x$ dimension.
173 //
174 // Software Guide : EndLatex
175 
176 // Software Guide : BeginCodeSnippet
177  ConstIteratorType inputIt(inputImage, inputImage->GetRequestedRegion());
178  IteratorType outputIt(outputImage, inputImage->GetRequestedRegion());
179 
180  inputIt.SetDirection(0);
181  outputIt.SetDirection(0);
182 // Software Guide : EndCodeSnippet
183 
184 // Software Guide: BeginLatex
185 //
186 // Each line in the input is copied to the output. The input iterator moves
187 // forward across columns while the output iterator moves backwards.
188 //
189 // Software Guide : EndLatex
190 
191 // Software Guide : BeginCodeSnippet
192  for (inputIt.GoToBegin(), outputIt.GoToBegin(); !inputIt.IsAtEnd();
193  outputIt.NextLine(), inputIt.NextLine())
194  {
195  inputIt.GoToBeginOfLine();
196  outputIt.GoToEndOfLine();
197  --outputIt;
198  while (!inputIt.IsAtEndOfLine())
199  {
200  outputIt.Set(inputIt.Get());
201  ++inputIt;
202  --outputIt;
203  }
204  }
205 // Software Guide : EndCodeSnippet
206 
207  WriterType::Pointer writer = WriterType::New();
208  writer->SetFileName(argv[2]);
209  writer->SetInput(outputImage);
210  try
211  {
212  writer->Update();
213  }
214  catch (itk::ExceptionObject& err)
215  {
216  std::cout << "ExceptionObject caught !" << std::endl;
217  std::cout << err << std::endl;
218  return -1;
219  }
220 
221 // Software Guide : BeginLatex
222 //
223 // Running this example on \code{ROI\_QB\_MUL\_1.tif} produces
224 // the same output image shown in
225 // Figure~\ref{fig:ImageRegionIteratorWithIndexExample}.
226 //
227 // \index{itk::ImageLinearIteratorWithIndex!example of using|)}
228 // Software Guide : EndLatex
229 
230  return EXIT_SUCCESS;
231 }

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