MeanImageFilter.cxx 6.41 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/*
 * Copyright (C) 2005-2017 Centre National d'Etudes Spatiales (CNES)
 *
 * This file is part of Orfeo Toolbox
 *
 *     https://www.orfeo-toolbox.org/
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
Patrick Imbo's avatar
nomsg  
Patrick Imbo committed
20

21

OTB Bot's avatar
OTB Bot committed
22 23

//  Software Guide : BeginCommandLineArgs
Jordi Inglada's avatar
Jordi Inglada committed
24 25
//    INPUTS: {QB_Suburb.png}
//    OUTPUTS: {MeanImageFilterOutput.png}
OTB Bot's avatar
OTB Bot committed
26 27 28 29
//  Software Guide : EndCommandLineArgs

//  Software Guide : BeginLatex
//
Jordi Inglada's avatar
Jordi Inglada committed
30 31 32 33 34 35 36
//  The \doxygen{itk}{MeanImageFilter} is commonly used for noise
//  reduction. The filter computes the value of each output pixel by
//  finding the statistical mean of the neighborhood of the
//  corresponding input pixel.  The following figure illustrates the
//  local effect of the MeanImageFilter. The statistical mean of the
//  neighborhood on the left is passed as the output value associated
//  with the pixel at the center of the neighborhood.
OTB Bot's avatar
OTB Bot committed
37 38
//
//  \begin{center}
OTB Bot's avatar
STYLE  
OTB Bot committed
39 40 41 42 43 44 45 46 47 48 49 50 51 52
//  \begin{picture}(200, 46)
//  \put(   5.0,  0.0 ){\framebox(30.0, 15.0){25}}
//  \put(  35.0,  0.0 ){\framebox(30.0, 15.0){30}}
//  \put(  65.0,  0.0 ){\framebox(30.0, 15.0){32}}
//  \put(   5.0, 15.0 ){\framebox(30.0, 15.0){27}}
//  \put(  35.0, 15.0 ){\framebox(30.0, 15.0){25}}
//  \put(  65.0, 15.0 ){\framebox(30.0, 15.0){29}}
//  \put(   5.0, 30.0 ){\framebox(30.0, 15.0){28}}
//  \put(  35.0, 30.0 ){\framebox(30.0, 15.0){26}}
//  \put(  65.0, 30.0 ){\framebox(30.0, 15.0){50}}
//  \put( 100.0, 22.0 ){\vector(1, 0){20.0}}
//  \put( 125.0, 15.0 ){\framebox(34.0, 15.0){30.22}}
//  \put( 160.0, 22.0 ){\vector(1, 0){20.0}}
//  \put( 185.0, 15.0 ){\framebox(30.0, 15.0){30}}
OTB Bot's avatar
OTB Bot committed
53 54 55
//  \end{picture}
//  \end{center}
//
Jordi Inglada's avatar
Jordi Inglada committed
56 57 58 59 60
//  Note that this algorithm is sensitive to the presence of outliers in the
//  neighborhood.  This filter will work on images of any dimension thanks to
//  the internal use of \doxygen{itk}{SmartNeighborhoodIterator} and
//  \doxygen{itk}{NeighborhoodOperator}.  The size of the neighborhood over which
//  the mean is computed can be set by the user.
OTB Bot's avatar
OTB Bot committed
61 62 63
//
//  \index{itk::MeanImageFilter}
//
64
//  Software Guide : EndLatex
OTB Bot's avatar
OTB Bot committed
65

66 67 68
#include "otbImage.h"
#include "otbImageFileReader.h"
#include "otbImageFileWriter.h"
69
#include "itkUnaryFunctorImageFilter.h"
OTB Bot's avatar
OTB Bot committed
70 71 72 73 74 75 76

//  Software Guide : BeginLatex
//
//  The header file corresponding to this filter should be included first.
//
//  \index{itk::MeanImageFilter!header}
//
77
//  Software Guide : EndLatex
OTB Bot's avatar
OTB Bot committed
78 79 80 81 82

// Software Guide : BeginCodeSnippet
#include "itkMeanImageFilter.h"
// Software Guide : EndCodeSnippet

OTB Bot's avatar
STYLE  
OTB Bot committed
83
int main(int argc, char * argv[])
OTB Bot's avatar
OTB Bot committed
84
{
OTB Bot's avatar
STYLE  
OTB Bot committed
85 86
  if (argc < 3)
    {
OTB Bot's avatar
OTB Bot committed
87
    std::cerr << "Usage: " << std::endl;
Jordi Inglada's avatar
Jordi Inglada committed
88
    std::cerr << argv[0] << "  inputImageFile   outputImageFile" << std::endl;
OTB Bot's avatar
OTB Bot committed
89
    return EXIT_FAILURE;
OTB Bot's avatar
STYLE  
OTB Bot committed
90
    }
OTB Bot's avatar
OTB Bot committed
91 92 93 94 95 96

  //  Software Guide : BeginLatex
  //
  //  Then the pixel types for input and output image must be defined and, with
  //  them, the image types can be instantiated.
  //
97
  //  Software Guide : EndLatex
OTB Bot's avatar
OTB Bot committed
98 99

  // Software Guide : BeginCodeSnippet
OTB Bot's avatar
STYLE  
OTB Bot committed
100 101
  typedef   unsigned char InputPixelType;
  typedef   unsigned char OutputPixelType;
OTB Bot's avatar
OTB Bot committed
102

OTB Bot's avatar
STYLE  
OTB Bot committed
103 104
  typedef otb::Image<InputPixelType,  2> InputImageType;
  typedef otb::Image<OutputPixelType, 2> OutputImageType;
OTB Bot's avatar
OTB Bot committed
105 106
  // Software Guide : EndCodeSnippet

OTB Bot's avatar
STYLE  
OTB Bot committed
107 108
  typedef otb::ImageFileReader<InputImageType>  ReaderType;
  typedef otb::ImageFileWriter<OutputImageType> WriterType;
OTB Bot's avatar
OTB Bot committed
109 110 111 112

  ReaderType::Pointer reader = ReaderType::New();
  WriterType::Pointer writer = WriterType::New();

OTB Bot's avatar
STYLE  
OTB Bot committed
113 114
  reader->SetFileName(argv[1]);
  writer->SetFileName(argv[2]);
OTB Bot's avatar
OTB Bot committed
115 116 117 118

  //  Software Guide : BeginLatex
  //
  //  Using the image types it is now possible to instantiate the filter type
119
  //  and create the filter object.
OTB Bot's avatar
OTB Bot committed
120 121 122 123
  //
  //  \index{itk::MeanImageFilter!instantiation}
  //  \index{itk::MeanImageFilter!New()}
  //  \index{itk::MeanImageFilter!Pointer}
124 125
  //
  //  Software Guide : EndLatex
OTB Bot's avatar
OTB Bot committed
126 127 128

  // Software Guide : BeginCodeSnippet
  typedef itk::MeanImageFilter<
OTB Bot's avatar
STYLE  
OTB Bot committed
129
      InputImageType, OutputImageType>  FilterType;
OTB Bot's avatar
OTB Bot committed
130 131 132 133 134 135 136 137 138

  FilterType::Pointer filter = FilterType::New();
  // Software Guide : EndCodeSnippet

  //  Software Guide : BeginLatex
  //
  //  The size of the neighborhood is defined along every dimension by
  //  passing a \code{SizeType} object with the corresponding values. The
  //  value on each dimension is used as the semi-size of a rectangular
OTB Bot's avatar
STYLE  
OTB Bot committed
139
  //  box. For example, in $2D$ a size of \(1, 2\) will result in a $3 \times
OTB Bot's avatar
OTB Bot committed
140 141 142 143 144
  //  5$ neighborhood.
  //
  //  \index{itk::MeanImageFilter!Radius}
  //  \index{itk::MeanImageFilter!Neighborhood}
  //
145
  //  Software Guide : EndLatex
OTB Bot's avatar
OTB Bot committed
146 147 148

  // Software Guide : BeginCodeSnippet
  InputImageType::SizeType indexRadius;
149

Jordi Inglada's avatar
Jordi Inglada committed
150 151
  indexRadius[0] = 1; // radius along x
  indexRadius[1] = 1; // radius along y
OTB Bot's avatar
OTB Bot committed
152

OTB Bot's avatar
STYLE  
OTB Bot committed
153
  filter->SetRadius(indexRadius);
OTB Bot's avatar
OTB Bot committed
154 155 156 157 158 159 160 161 162 163 164 165
  // Software Guide : EndCodeSnippet

  //  Software Guide : BeginLatex
  //
  //  The input to the filter can be taken from any other filter, for example
  //  a reader. The output can be passed down the pipeline to other filters,
  //  for example, a writer. An update call on any downstream filter will
  //  trigger the execution of the mean filter.
  //
  //  \index{itk::MeanImageFilter!SetInput()}
  //  \index{itk::MeanImageFilter!GetOutput()}
  //
166
  //  Software Guide : EndLatex
OTB Bot's avatar
OTB Bot committed
167 168

  // Software Guide : BeginCodeSnippet
OTB Bot's avatar
STYLE  
OTB Bot committed
169 170
  filter->SetInput(reader->GetOutput());
  writer->SetInput(filter->GetOutput());
OTB Bot's avatar
OTB Bot committed
171 172 173 174
  writer->Update();
  // Software Guide : EndCodeSnippet

  //  Software Guide : BeginLatex
175
  //
OTB Bot's avatar
OTB Bot committed
176 177
  // \begin{figure}
  // \center
Jordi Inglada's avatar
Jordi Inglada committed
178 179 180 181
  // \includegraphics[width=0.44\textwidth]{QB_Suburb.eps}
  // \includegraphics[width=0.44\textwidth]{MeanImageFilterOutput.eps}
  // \itkcaption[Effect of the MedianImageFilter]{Effect of the MeanImageFilter.}
  // \label{fig:MeanImageFilterOutput}
OTB Bot's avatar
OTB Bot committed
182 183
  // \end{figure}
  //
Jordi Inglada's avatar
Jordi Inglada committed
184 185
  //  Figure \ref{fig:MeanImageFilterOutput} illustrates the effect of this
  //  filter using neighborhood radii of
OTB Bot's avatar
STYLE  
OTB Bot committed
186
  //  \(1, 1\) which corresponds to a $ 3 \times 3 $ classical neighborhood.
Jordi Inglada's avatar
Jordi Inglada committed
187 188
  //  It can be seen from this picture that edges are rapidly degraded by the
  //  diffusion of intensity values among neighbors.
OTB Bot's avatar
OTB Bot committed
189
  //
190
  //  Software Guide : EndLatex
OTB Bot's avatar
OTB Bot committed
191 192 193

  return EXIT_SUCCESS;
}