Filter.h
Go to the documentation of this file.
1 /* Copyright (C) 2008 National Institute For Space Research (INPE) - Brazil.
2 
3  This file is part of the TerraLib - a Framework for building GIS enabled applications.
4 
5  TerraLib is free software: you can redistribute it and/or modify
6  it under the terms of the GNU Lesser General Public License as published by
7  the Free Software Foundation, either version 3 of the License,
8  or (at your option) any later version.
9 
10  TerraLib is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public License
16  along with TerraLib. See COPYING. If not, write to
17  TerraLib Team at <terralib-team@terralib.org>.
18  */
19 
20 /*!
21  \file terralib/rp/Filter.h
22  \brief A series of well-known filtering algorithms for images, linear and non-linear.
23  */
24 
25 #ifndef __TERRALIB_RP_INTERNAL_FILTER_H
26 #define __TERRALIB_RP_INTERNAL_FILTER_H
27 
28 #include "Algorithm.h"
29 #include "../raster/Raster.h"
30 
31 // Boost
32 #include <boost/numeric/ublas/io.hpp>
33 #include <boost/numeric/ublas/matrix.hpp>
34 
35 // STL
36 #include <vector>
37 
38 namespace te
39 {
40  namespace rp
41  {
42  /*!
43  \class Filter
44  \brief A series of well-known filtering algorithms for images, linear and non-linear..
45  \ingroup rp_enh
46  */
47  class TERPEXPORT Filter : public Algorithm
48  {
49  public:
50 
51  /*!
52  \class InputParameters
53  \brief Filter input parameters
54  */
56  {
57  public:
58 
59  /*! \enum The edge filter type.*/
61  {
62  InvalidFilterT = 0, //!< Invalid strategy.
63  SobelFilterT = 1, //!< Sobel filter type.
64  RobertsFilterT = 2, //!< Roberts filter type.
65  MeanFilterT = 3, //!< The resultant pixel will be the mean of pixels in the convolution window.
66  ModeFilterT = 4, //!< The resultant pixel will be the mode of pixels in the convolution window. When the window is multimodal, the first mode will be assumed.
67  MedianFilterT = 5, //!< The resultant pixel will be the median of pixels in the convolution window.
68  DilationFilterT = 6, //!< The resultant pixel will be the highest pixel value in the convolution window.
69  ErosionFilterT = 7, //!< The resultant pixel will be the lowest pixel value in the convolution window.
70  UserDefinedWindowT = 8 //!< The user will define the weights of a convolution window.
71  };
72 
73  FilterType m_filterType; //!< The edge filter type.
74 
75  te::rst::Raster const* m_inRasterPtr; //!< Input raster.
76 
77  std::vector< unsigned int > m_inRasterBands; //!< Bands to be used from the input raster 1.
78 
79  unsigned int m_iterationsNumber; //!< The number of iterations to perform (default:1).
80 
81  unsigned int m_windowH; //!< The height of the convolution window. (commonly 3, with W=3 to make a 3x3 window, and so on)
82 
83  unsigned int m_windowW; //!< The width of the convolution window.
84 
85  bool m_enableProgress; //!< Enable/Disable the progress interface (default:false).
86 
87  boost::numeric::ublas::matrix<double> m_window; //!< User defined convolution window. (The size must be equal to m_windowH x m_windowW. Default: an empty matrix)
88 
89  bool m_enableRasterCache; //!< Enable/Disable the use of a raster data cache (default:true).
90 
92 
94 
96 
97  //overload
98  void reset() _NOEXCEPT_OP(false);
99 
100  //overload
101  const InputParameters& operator=( const InputParameters& params );
102 
103  //overload
104  AbstractParameters* clone() const;
105  };
106 
107  /*!
108  \class OutputParameters
109  \brief Filter output parameters
110  */
112  {
113  public:
114 
115  std::string m_rType; //!< Output raster data source type (as described in te::raster::RasterFactory ).
116 
117  std::map< std::string, std::string > m_rInfo; //!< The necessary information to create the raster (as described in te::raster::RasterFactory).
118 
119  std::unique_ptr< te::rst::Raster > m_outputRasterPtr; //!< A pointer the ge generated output raster (label image).
120 
122 
124 
126 
127  //overload
128  void reset() _NOEXCEPT_OP(false);
129 
130  //overload
131  const OutputParameters& operator=( const OutputParameters& params );
132 
133  //overload
134  AbstractParameters* clone() const;
135  };
136 
138 
139  ~Filter();
140 
141  //overload
142  bool execute( AlgorithmOutputParameters& outputParams ) _NOEXCEPT_OP(false);
143 
144  //overload
145  void reset() _NOEXCEPT_OP(false);
146 
147  //overload
148  bool initialize( const AlgorithmInputParameters& inputParams ) _NOEXCEPT_OP(false);
149 
150  //overload
151  bool isInitialized() const;
152 
153  protected:
154 
155  /*!
156  \brief Type definition for a filter method pointer.
157  \param srcRaster Source raster.
158  \param srcBandIdx Source raster band index.
159  \param dstRaster Destination raster.
160  \param dstBandIdx Destination raster band index.
161  \param useProgress if true, the progress interface must be used.
162  \return true if ok, false on errors.
163  */
164  typedef bool (Filter::*FilterMethodPointerT)( const te::rst::Raster& srcRaster,
165  const unsigned int srcBandIdx, te::rst::Raster& dstRaster,
166  const unsigned int dstBandIdx, const bool useProgress );
167 
168  bool m_isInitialized; //!< Is this instance already initialized?
169 
170  Filter::InputParameters m_inputParameters; //!< Input parameters.
171 
172  double** m_convBuffer; //!< Convolution Buffer.
173 
174  unsigned int m_convBufferLines; //!< Convolution Buffer lines.
175 
176  unsigned int m_convBufferColumns; //!< Convolution Buffer columns.
177 
178  double** m_convMatrix; //!< Convolution matrix element.
179 
180  unsigned int m_convMatrixLines; //!< Convolution matrix element lines.
181 
182  unsigned int m_convMatrixColumns; //!< Convolution matrix element columns.
183 
184  /*!
185  \brief Applay the Roberts filter over the source raster band.
186  \param srcRaster Source raster.
187  \param srcBandIdx Source raster band index.
188  \param dstRaster Destination raster.
189  \param dstBandIdx Destination raster band index.
190  \param useProgress if true, the progress interface must be used.
191  */
192  bool RobertsFilter( const te::rst::Raster& srcRaster,
193  const unsigned int srcBandIdx, te::rst::Raster& dstRaster,
194  const unsigned int dstBandIdx, const bool useProgress );
195 
196  /*!
197  \brief Applay the Sobel filter over the source raster band.
198  \param srcRaster Source raster.
199  \param srcBandIdx Source raster band index.
200  \param dstRaster Destination raster.
201  \param dstBandIdx Destination raster band index.
202  \param useProgress if true, the progress interface must be used.
203  */
204  bool SobelFilter( const te::rst::Raster& srcRaster,
205  const unsigned int srcBandIdx, te::rst::Raster& dstRaster,
206  const unsigned int dstBandIdx, const bool useProgress );
207 
208  /*!
209  \brief Applay the mean filter over the source raster band.
210  \param srcRaster Source raster.
211  \param srcBandIdx Source raster band index.
212  \param dstRaster Destination raster.
213  \param dstBandIdx Destination raster band index.
214  \param useProgress if true, the progress interface must be used.
215  */
216  bool MeanFilter( const te::rst::Raster& srcRaster,
217  const unsigned int srcBandIdx, te::rst::Raster& dstRaster,
218  const unsigned int dstBandIdx, const bool useProgress );
219 
220  /*!
221  \brief Applay the mode filter over the source raster band.
222  \param srcRaster Source raster.
223  \param srcBandIdx Source raster band index.
224  \param dstRaster Destination raster.
225  \param dstBandIdx Destination raster band index.
226  \param useProgress if true, the progress interface must be used.
227  */
228  bool ModeFilter( const te::rst::Raster& srcRaster,
229  const unsigned int srcBandIdx, te::rst::Raster& dstRaster,
230  const unsigned int dstBandIdx, const bool useProgress );
231 
232  /*!
233  \brief Applay the median filter over the source raster band.
234  \param srcRaster Source raster.
235  \param srcBandIdx Source raster band index.
236  \param dstRaster Destination raster.
237  \param dstBandIdx Destination raster band index.
238  \param useProgress if true, the progress interface must be used.
239  */
240  bool MedianFilter( const te::rst::Raster& srcRaster,
241  const unsigned int srcBandIdx, te::rst::Raster& dstRaster,
242  const unsigned int dstBandIdx, const bool useProgress );
243 
244  /*!
245  \brief Applay the dilation filter over the source raster band.
246  \param srcRaster Source raster.
247  \param srcBandIdx Source raster band index.
248  \param dstRaster Destination raster.
249  \param dstBandIdx Destination raster band index.
250  \param useProgress if true, the progress interface must be used.
251  */
252  bool DilationFilter( const te::rst::Raster& srcRaster,
253  const unsigned int srcBandIdx, te::rst::Raster& dstRaster,
254  const unsigned int dstBandIdx, const bool useProgress );
255 
256  /*!
257  \brief Applay the erosion filter over the source raster band.
258  \param srcRaster Source raster.
259  \param srcBandIdx Source raster band index.
260  \param dstRaster Destination raster.
261  \param dstBandIdx Destination raster band index.
262  \param useProgress if true, the progress interface must be used.
263  */
264  bool ErosionFilter( const te::rst::Raster& srcRaster,
265  const unsigned int srcBandIdx, te::rst::Raster& dstRaster,
266  const unsigned int dstBandIdx, const bool useProgress );
267 
268  /*!
269  \brief Applay the user defined filter over the source raster band.
270  \param srcRaster Source raster.
271  \param srcBandIdx Source raster band index.
272  \param dstRaster Destination raster.
273  \param dstBandIdx Destination raster band index.
274  \param useProgress if true, the progress interface must be used.
275  */
276  bool UserDefinedFilter( const te::rst::Raster& srcRaster,
277  const unsigned int srcBandIdx, te::rst::Raster& dstRaster,
278  const unsigned int dstBandIdx, const bool useProgress );
279 
280  /*!
281  \brief Returns true if i < j.
282  \return Returns true if i < j.
283  */
284  static bool OrderFunction(double i, double j);
285 
286  /*!
287  \brief Resets the convolution buffer.
288  \param lines Convolution buffer lines.
289  \param columns Convolution buffer columns.
290  */
291  void ResetConvBuffer(unsigned int lines, unsigned int columns);
292 
293  /*!
294  \brief Updates the convolution buffer with a new raster line (rool up + read new raster line).
295  \param inBand Source raster band.
296  \param line Raster line.
297  \param band Raster band.
298  */
299  void UpdateConvBuffer(const te::rst::Band& inBand, unsigned int line);
300 
301  /*!
302  \brief Resets the convolution matrix.
303  \param lines Convolution matrix lines.
304  \param columns Convolution matrix columns.
305  */
306  void ResetConvMatrix(unsigned int lines, unsigned int columns);
307 
308  /*!
309  \brief Reset the convolution matrix and fills it with the user supplied matrix or with 1's if that matrix wasn't supplied.
310  */
311  void setMorfConvMatrix();
312 
313  };
314 
315  } // end namespace rp
316 } // end namespace te
317 
318 #endif
319 
te::rp::Filter::InputParameters::~InputParameters
~InputParameters()
te
TerraLib.
Definition: AddressGeocodingOp.h:52
te::rp::AlgorithmOutputParameters
Raster Processing algorithm output parameters base interface.
Definition: AlgorithmOutputParameters.h:40
te::rp::Filter::OutputParameters::m_outputRasterPtr
std::unique_ptr< te::rst::Raster > m_outputRasterPtr
A pointer the ge generated output raster (label image).
Definition: Filter.h:119
te::rp::Filter::OutputParameters::~OutputParameters
~OutputParameters()
te::rp::Filter::InputParameters::m_iterationsNumber
unsigned int m_iterationsNumber
The number of iterations to perform (default:1).
Definition: Filter.h:79
te::rst::Raster
An abstract class for raster data strucutures.
Definition: Raster.h:72
te::rp::AlgorithmInputParameters
Raster Processing algorithm input parameters base interface.
Definition: AlgorithmInputParameters.h:40
te::rp::Filter::InputParameters::m_window
boost::numeric::ublas::matrix< double > m_window
User defined convolution window. (The size must be equal to m_windowH x m_windowW....
Definition: Filter.h:87
te::rp::Filter::InputParameters::m_inRasterBands
std::vector< unsigned int > m_inRasterBands
Bands to be used from the input raster 1.
Definition: Filter.h:77
_NOEXCEPT_OP
#define _NOEXCEPT_OP(x)
Definition: NoExceptDefinition.h:36
te::rp::Filter::OutputParameters::OutputParameters
OutputParameters()
te::rp::Filter::OutputParameters::reset
void reset() _NOEXCEPT_OP(false)
Clear all internal allocated resources and reset the parameters instance to its initial state.
te::rp::Filter::OutputParameters
Filter output parameters.
Definition: Filter.h:112
te::rp::Filter::OutputParameters::m_rType
std::string m_rType
Output raster data source type (as described in te::raster::RasterFactory ).
Definition: Filter.h:115
te::rp::Filter::InputParameters::FilterType
FilterType
Definition: Filter.h:61
te::rp::Algorithm
Raster Processing algorithm base interface.
Definition: Algorithm.h:42
te::rp::Filter::InputParameters::InputParameters
InputParameters(const InputParameters &)
te::rp::Filter::InputParameters::m_windowW
unsigned int m_windowW
The width of the convolution window.
Definition: Filter.h:83
te::rp::Filter::InputParameters::m_filterType
FilterType m_filterType
The edge filter type.
Definition: Filter.h:73
te::rp::Filter::OutputParameters::m_rInfo
std::map< std::string, std::string > m_rInfo
The necessary information to create the raster (as described in te::raster::RasterFactory).
Definition: Filter.h:117
te::rp::Filter::InputParameters::m_inRasterPtr
te::rst::Raster const * m_inRasterPtr
Input raster.
Definition: Filter.h:75
te::rp::Filter::InputParameters::reset
void reset() _NOEXCEPT_OP(false)
Clear all internal allocated resources and reset the parameters instance to its initial state.
TERPEXPORT
#define TERPEXPORT
You can use this macro in order to export/import classes and functions from this module.
Definition: Config.h:139
te::rp::Filter::InputParameters::m_enableRasterCache
bool m_enableRasterCache
Enable/Disable the use of a raster data cache (default:true).
Definition: Filter.h:89
te::rp::Filter::OutputParameters::OutputParameters
OutputParameters(const OutputParameters &)
Algorithm.h
Abstract algorithm.
te::rp::Filter::InputParameters
Filter input parameters.
Definition: Filter.h:56
te::rp::Filter::InputParameters::m_enableProgress
bool m_enableProgress
Enable/Disable the progress interface (default:false).
Definition: Filter.h:85
te::rp::Filter::InputParameters::m_windowH
unsigned int m_windowH
The height of the convolution window. (commonly 3, with W=3 to make a 3x3 window, and so on)
Definition: Filter.h:81
te::rp::Filter
A series of well-known filtering algorithms for images, linear and non-linear..
Definition: Filter.h:48
te::rp::Filter::InputParameters::InputParameters
InputParameters()