Orfeo Toolbox  3.16
NeighborhoodIterators2.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 #include "otbImage.h"
20 #include "otbImageFileReader.h"
21 #include "otbImageFileWriter.h"
24 #include "itkImageRegionIterator.h"
25 
26 // Software Guide : BeginLatex
27 //
28 // In this example, the Sobel edge-detection routine is rewritten using
29 // convolution filtering. Convolution filtering is a standard image processing
30 // technique that can be implemented numerically as the inner product of all
31 // image neighborhoods with a convolution kernel \cite{Gonzalez1993}
32 // \cite{Castleman1996}. In ITK, we use a class of objects called
33 // \emph{neighborhood operators} as convolution kernels and a special function
34 // object called \doxygen{itk}{NeighborhoodInnerProduct} to calculate inner
35 // products.
36 //
37 // The basic ITK convolution filtering routine is to step through the image
38 // with a neighborhood iterator and use NeighborhoodInnerProduct to
39 // find the inner product of each neighborhood with the desired kernel. The
40 // resulting values are written to an output image. This example uses a
41 // neighborhood operator called the \doxygen{itk}{SobelOperator}, but all
42 // neighborhood operators can be convolved with images using this basic
43 // routine. Other examples of neighborhood operators include derivative
44 // kernels, Gaussian kernels, and morphological
45 // operators. \doxygen{itk}{NeighborhoodOperatorImageFilter} is a generalization of
46 // the code in this section to ND images and arbitrary convolution kernels.
47 //
48 // We start writing this example by including the header files for the Sobel
49 // kernel and the inner product function.
50 //
51 // Software Guide : EndLatex
52 
53 // Software Guide : BeginCodeSnippet
54 #include "itkSobelOperator.h"
56 // Software Guide : EndCodeSnippet
57 
58 int main(int argc, char * argv[])
59 {
60  if (argc < 4)
61  {
62  std::cerr << "Missing parameters. " << std::endl;
63  std::cerr << "Usage: " << std::endl;
64  std::cerr << argv[0]
65  << " inputImageFile outputImageFile direction"
66  << std::endl;
67  return -1;
68  }
69 
70  typedef float PixelType;
71  typedef otb::Image<PixelType, 2> ImageType;
72  typedef otb::ImageFileReader<ImageType> ReaderType;
73 
74  typedef itk::ConstNeighborhoodIterator<ImageType> NeighborhoodIteratorType;
75  typedef itk::ImageRegionIterator<ImageType> IteratorType;
76 
77  ReaderType::Pointer reader = ReaderType::New();
78  reader->SetFileName(argv[1]);
79  try
80  {
81  reader->Update();
82  }
83  catch (itk::ExceptionObject& err)
84  {
85  std::cout << "ExceptionObject caught !" << std::endl;
86  std::cout << err << std::endl;
87  return -1;
88  }
89 
90  ImageType::Pointer output = ImageType::New();
91  output->SetRegions(reader->GetOutput()->GetRequestedRegion());
92  output->Allocate();
93 
94  IteratorType out(output, reader->GetOutput()->GetRequestedRegion());
95 
96 // Software Guide : BeginLatex
97 //
98 // \index{convolution!kernels}
99 // \index{convolution!operators}
100 // \index{iterators!neighborhood!and convolution}
101 //
102 // Refer to the previous example for a description of reading the input image and
103 // setting up the output image and iterator.
104 //
105 // The following code creates a Sobel operator. The Sobel operator requires
106 // a direction for its partial derivatives. This direction is read from the command line.
107 // Changing the direction of the derivatives changes the bias of the edge
108 // detection, i.e. maximally vertical or maximally horizontal.
109 //
110 // Software Guide : EndLatex
111 
112 // Software Guide : BeginCodeSnippet
113  itk::SobelOperator<PixelType, 2> sobelOperator;
114  sobelOperator.SetDirection(::atoi(argv[3]));
115  sobelOperator.CreateDirectional();
116 // Software Guide : EndCodeSnippet
117 
118 // Software Guide : BeginLatex
119 //
120 // The neighborhood iterator is initialized as before, except that now it takes
121 // its radius directly from the radius of the Sobel operator. The inner
122 // product function object is templated over image type and requires no
123 // initialization.
124 //
125 // Software Guide : EndLatex
126 
127 // Software Guide : BeginCodeSnippet
128  NeighborhoodIteratorType::RadiusType radius = sobelOperator.GetRadius();
129  NeighborhoodIteratorType it(radius, reader->GetOutput(),
130  reader->GetOutput()->
131  GetRequestedRegion());
132 
134 // Software Guide : EndCodeSnippet
135 
136 // Software Guide : BeginLatex
137 //
138 // Using the Sobel operator, inner product, and neighborhood iterator objects,
139 // we can now write a very simple \code{for} loop for performing convolution
140 // filtering. As before, out-of-bounds pixel values are supplied automatically
141 // by the iterator.
142 //
143 // Software Guide : EndLatex
144 
145 // Software Guide : BeginCodeSnippet
146  for (it.GoToBegin(), out.GoToBegin(); !it.IsAtEnd(); ++it, ++out)
147  {
148  out.Set(innerProduct(it, sobelOperator));
149  }
150 // Software Guide : EndCodeSnippet
151 
152 // Software Guide : BeginLatex
153 //
154 // The output is rescaled and written as in the previous example. Applying
155 // this example in the $x$ and $y$ directions produces the images at the center
156 // and right of Figure~\ref{fig:NeighborhoodExamples1}. Note that x-direction
157 // operator produces the same output image as in the previous example.
158 //
159 // Software Guide : EndLatex
160 
161  typedef unsigned char WritePixelType;
162  typedef otb::Image<WritePixelType, 2> WriteImageType;
163  typedef otb::ImageFileWriter<WriteImageType> WriterType;
164 
166  ImageType, WriteImageType> RescaleFilterType;
167 
168  RescaleFilterType::Pointer rescaler = RescaleFilterType::New();
169 
170  rescaler->SetOutputMinimum(0);
171  rescaler->SetOutputMaximum(255);
172  rescaler->SetInput(output);
173 
174  WriterType::Pointer writer = WriterType::New();
175  writer->SetFileName(argv[2]);
176  writer->SetInput(rescaler->GetOutput());
177  try
178  {
179  writer->Update();
180  }
181  catch (itk::ExceptionObject& err)
182  {
183  std::cout << "ExceptionObject caught !" << std::endl;
184  std::cout << err << std::endl;
185  return -1;
186  }
187 
188  return EXIT_SUCCESS;
189 }

Generated at Sun Feb 3 2013 00:15:40 for Orfeo Toolbox with doxygen 1.8.1.1