Orfeo Toolbox  3.16
GradientAnisotropicDiffusionImageFilter.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 : BeginCommandLineArgs
21 // INPUTS: {QB_Suburb.png}
22 // OUTPUTS: {GradientAnisotropicDiffusionImageFilterOutput.png}
23 // 5 0.125 3
24 // Software Guide : EndCommandLineArgs
25 // Software Guide : BeginLatex
26 // The \doxygen{itk}{GradientAnisotropicDiffusionImageFilter} implements an
27 // $N$-dimensional version of the classic Perona-Malik anisotropic diffusion
28 // equation for scalar-valued images \cite{Perona1990}.
29 //
30 // The conductance term for this implementation is chosen as a function of the
31 // gradient magnitude of the image at each point, reducing the strength of
32 // diffusion at edge pixels.
33 //
34 // \begin{equation}
35 // C(\mathbf{x}) = e^{-(\frac{\parallel \nabla U(\mathbf{x}) \parallel}{K})^2}
36 // \end{equation}
37 //
38 // The numerical implementation of this equation is similar to that described
39 // in the Perona-Malik paper \cite{Perona1990}, but uses a more robust technique
40 // for gradient magnitude estimation and has been generalized to $N$-dimensions.
41 //
42 // \index{itk::Gradient\-Anisotropic\-Diffusion\-Image\-Filter}
43 //
44 // Software Guide : EndLatex
45 
46 #include "otbImage.h"
47 #include "otbImageFileReader.h"
48 #include "otbImageFileWriter.h"
50 
51 // Software Guide : BeginLatex
52 //
53 // The first step required to use this filter is to include its header file.
54 //
55 // \index{itk::Gradient\-Anisotropic\-Diffusion\-Image\-Filter!header}
56 //
57 // Software Guide : EndLatex
58 
59 // Software Guide : BeginCodeSnippet
61 // Software Guide : EndCodeSnippet
62 
63 int main(int argc, char * argv[])
64 {
65  if (argc < 6)
66  {
67  std::cerr << "Usage: " << std::endl;
68  std::cerr << argv[0] << " inputImageFile outputImageFile ";
69  std::cerr << "numberOfIterations timeStep conductance" << std::endl;
70  return EXIT_FAILURE;
71  }
72 
73  // Software Guide : BeginLatex
74  //
75  // Types should be selected based on the pixel types required for the
76  // input and output images. The image types are defined using the pixel
77  // type and the dimension.
78  //
79  // Software Guide : EndLatex
80 
81  // Software Guide : BeginCodeSnippet
82  typedef float InputPixelType;
83  typedef float OutputPixelType;
84 
85  typedef otb::Image<InputPixelType, 2> InputImageType;
86  typedef otb::Image<OutputPixelType, 2> OutputImageType;
87  // Software Guide : EndCodeSnippet
88 
89  typedef otb::ImageFileReader<InputImageType> ReaderType;
90 
91  // Software Guide : BeginLatex
92  //
93  // The filter type is now instantiated using both the input image and the
94  // output image types. The filter object is created by the \code{New()}
95  // method.
96  //
97  // \index{itk::Gradient\-Anisotropic\-Diffusion\-Image\-Filter!instantiation}
98  // \index{itk::Gradient\-Anisotropic\-Diffusion\-Image\-Filter!New()}
99  // \index{itk::Gradient\-Anisotropic\-Diffusion\-Image\-Filter!Pointer}
100  //
101  // Software Guide : EndLatex
102 
103  // Software Guide : BeginCodeSnippet
105  InputImageType, OutputImageType> FilterType;
107  // Software Guide : EndCodeSnippet
108 
109  ReaderType::Pointer reader = ReaderType::New();
110  reader->SetFileName(argv[1]);
111 
112  // Software Guide : BeginLatex
113  //
114  // The input image can be obtained from the output of another filter. Here,
115  // an image reader is used as source.
116  //
117  // Software Guide : EndLatex
118 
119  // Software Guide : BeginCodeSnippet
120  filter->SetInput(reader->GetOutput());
121  // Software Guide : EndCodeSnippet
122 
123  const unsigned int numberOfIterations = atoi(argv[3]);
124 
125  const double timeStep = atof(argv[4]);
126 
127  const double conductance = atof(argv[5]);
128 
129  // Software Guide : BeginLatex
130  //
131  // This filter requires three parameters, the number of iterations to be
132  // performed, the time step and the conductance parameter used in the
133  // computation of the level set evolution. These parameters are set using
134  // the methods \code{SetNumberOfIterations()}, \code{SetTimeStep()} and
135  // \code{SetConductanceParameter()} respectively. The filter can be
136  // executed by invoking Update().
137  //
138  // \index{itk::Gradient\-Anisotropic\-Diffusion\-Image\-Filter!Update()}
139  // \index{itk::Gradient\-Anisotropic\-Diffusion\-Image\-Filter!SetTimeStep()}
140  // \index{itk::Gradient\-Anisotropic\-Diffusion\-Image\-Filter!SetConductanceParameter()}
141  // \index{itk::Gradient\-Anisotropic\-Diffusion\-Image\-Filter!SetNumberOfIterations()}
142  // \index{SetTimeStep()!itk::Gradient\-Anisotropic\-Diffusion\-Image\-Filter}
143  // \index{SetNumberOfIterations()!itk::Gradient\-Anisotropic\-Diffusion\-Image\-Filter}
144  //
145  // Software Guide : EndLatex
146 
147  // Software Guide : BeginCodeSnippet
148  filter->SetNumberOfIterations(numberOfIterations);
149  filter->SetTimeStep(timeStep);
150  filter->SetConductanceParameter(conductance);
151 
152  filter->Update();
153  // Software Guide : EndCodeSnippet
154 
155  // Software Guide : BeginLatex
156  //
157  // A typical value for the time step is $0.125$. The number of
158  // iterations is typically set to $5$; more iterations result in
159  // further smoothing and will increase the computing time linearly.
160  //
161  // Software Guide : EndLatex
162 
163  //
164  // The output of the filter is rescaled here and then sent to a writer.
165  //
166  typedef unsigned char WritePixelType;
167  typedef otb::Image<WritePixelType, 2> WriteImageType;
169  OutputImageType, WriteImageType> RescaleFilterType;
170 
171  RescaleFilterType::Pointer rescaler = RescaleFilterType::New();
172  rescaler->SetOutputMinimum(0);
173  rescaler->SetOutputMaximum(255);
174 
175  typedef otb::ImageFileWriter<WriteImageType> WriterType;
176 
177  WriterType::Pointer writer = WriterType::New();
178  writer->SetFileName(argv[2]);
179 
180  rescaler->SetInput(filter->GetOutput());
181  writer->SetInput(rescaler->GetOutput());
182  writer->Update();
183 
184  // Software Guide : BeginLatex
185  //
186  // \begin{figure} \center
187  // \includegraphics[width=0.44\textwidth]{QB_Suburb.eps}
188  // \includegraphics[width=0.44\textwidth]{GradientAnisotropicDiffusionImageFilterOutput.eps}
189  // \itkcaption[GradientAnisotropicDiffusionImageFilter output]{Effect of the
190  // GradientAnisotropicDiffusionImageFilter.}
191  // \label{fig:GradientAnisotropicDiffusionImageFilterInputOutput}
192  // \end{figure}
193  //
194  // Figure
195  // \ref{fig:GradientAnisotropicDiffusionImageFilterInputOutput}
196  // illustrates the effect of this filter. In this example the
197  // filter was run with a time step of $0.125$, and $5$ iterations.
198  // The figure shows how homogeneous regions are
199  // smoothed and
200  // edges are preserved.
201  //
202  // \relatedClasses
203  // \begin{itemize}
204  // \item \doxygen{itk}{BilateralImageFilter}
205  // \item \doxygen{itk}{CurvatureAnisotropicDiffusionImageFilter}
206  // \item \doxygen{itk}{CurvatureFlowImageFilter}
207  // \end{itemize}
208  //
209  // Software Guide : EndLatex
210 
211  return EXIT_SUCCESS;
212 }

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