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  //overload
107  bool serialize ( AlgorithmParametersSerializer& serializer ) const;
108  };
109 
110  /*!
111  \class OutputParameters
112  \brief Filter output parameters
113  */
115  {
116  public:
117 
118  std::string m_rType; //!< Output raster data source type (as described in te::raster::RasterFactory ).
119 
120  std::map< std::string, std::string > m_rInfo; //!< The necessary information to create the raster (as described in te::raster::RasterFactory).
121 
122  std::unique_ptr< te::rst::Raster > m_outputRasterPtr; //!< A pointer the ge generated output raster (label image).
123 
125 
127 
129 
130  //overload
131  void reset() _NOEXCEPT_OP(false);
132 
133  //overload
134  const OutputParameters& operator=( const OutputParameters& params );
135 
136  //overload
137  AbstractParameters* clone() const;
138  };
139 
141 
142  ~Filter();
143 
144  //overload
145  bool execute( AlgorithmOutputParameters& outputParams ) _NOEXCEPT_OP(false);
146 
147  //overload
148  void reset() _NOEXCEPT_OP(false);
149 
150  //overload
151  bool initialize( const AlgorithmInputParameters& inputParams ) _NOEXCEPT_OP(false);
152 
153  //overload
154  bool isInitialized() const;
155 
156  protected:
157 
158  /*!
159  \brief Type definition for a filter method pointer.
160  \param srcRaster Source raster.
161  \param srcBandIdx Source raster band index.
162  \param dstRaster Destination raster.
163  \param dstBandIdx Destination raster band index.
164  \param useProgress if true, the progress interface must be used.
165  \return true if ok, false on errors.
166  */
167  typedef bool (Filter::*FilterMethodPointerT)( const te::rst::Raster& srcRaster,
168  const unsigned int srcBandIdx, te::rst::Raster& dstRaster,
169  const unsigned int dstBandIdx, const bool useProgress );
170 
171  bool m_isInitialized; //!< Is this instance already initialized?
172 
173  Filter::InputParameters m_inputParameters; //!< Input parameters.
174 
175  double** m_convBuffer; //!< Convolution Buffer.
176 
177  unsigned int m_convBufferLines; //!< Convolution Buffer lines.
178 
179  unsigned int m_convBufferColumns; //!< Convolution Buffer columns.
180 
181  double** m_convMatrix; //!< Convolution matrix element.
182 
183  unsigned int m_convMatrixLines; //!< Convolution matrix element lines.
184 
185  unsigned int m_convMatrixColumns; //!< Convolution matrix element columns.
186 
187  /*!
188  \brief Applay the Roberts filter over the source raster band.
189  \param srcRaster Source raster.
190  \param srcBandIdx Source raster band index.
191  \param dstRaster Destination raster.
192  \param dstBandIdx Destination raster band index.
193  \param useProgress if true, the progress interface must be used.
194  */
195  bool RobertsFilter( const te::rst::Raster& srcRaster,
196  const unsigned int srcBandIdx, te::rst::Raster& dstRaster,
197  const unsigned int dstBandIdx, const bool useProgress );
198 
199  /*!
200  \brief Applay the Sobel filter over the source raster band.
201  \param srcRaster Source raster.
202  \param srcBandIdx Source raster band index.
203  \param dstRaster Destination raster.
204  \param dstBandIdx Destination raster band index.
205  \param useProgress if true, the progress interface must be used.
206  */
207  bool SobelFilter( const te::rst::Raster& srcRaster,
208  const unsigned int srcBandIdx, te::rst::Raster& dstRaster,
209  const unsigned int dstBandIdx, const bool useProgress );
210 
211  /*!
212  \brief Applay the mean filter over the source raster band.
213  \param srcRaster Source raster.
214  \param srcBandIdx Source raster band index.
215  \param dstRaster Destination raster.
216  \param dstBandIdx Destination raster band index.
217  \param useProgress if true, the progress interface must be used.
218  */
219  bool MeanFilter( const te::rst::Raster& srcRaster,
220  const unsigned int srcBandIdx, te::rst::Raster& dstRaster,
221  const unsigned int dstBandIdx, const bool useProgress );
222 
223  /*!
224  \brief Applay the mode filter over the source raster band.
225  \param srcRaster Source raster.
226  \param srcBandIdx Source raster band index.
227  \param dstRaster Destination raster.
228  \param dstBandIdx Destination raster band index.
229  \param useProgress if true, the progress interface must be used.
230  */
231  bool ModeFilter( const te::rst::Raster& srcRaster,
232  const unsigned int srcBandIdx, te::rst::Raster& dstRaster,
233  const unsigned int dstBandIdx, const bool useProgress );
234 
235  /*!
236  \brief Applay the median filter over the source raster band.
237  \param srcRaster Source raster.
238  \param srcBandIdx Source raster band index.
239  \param dstRaster Destination raster.
240  \param dstBandIdx Destination raster band index.
241  \param useProgress if true, the progress interface must be used.
242  */
243  bool MedianFilter( const te::rst::Raster& srcRaster,
244  const unsigned int srcBandIdx, te::rst::Raster& dstRaster,
245  const unsigned int dstBandIdx, const bool useProgress );
246 
247  /*!
248  \brief Applay the dilation filter over the source raster band.
249  \param srcRaster Source raster.
250  \param srcBandIdx Source raster band index.
251  \param dstRaster Destination raster.
252  \param dstBandIdx Destination raster band index.
253  \param useProgress if true, the progress interface must be used.
254  */
255  bool DilationFilter( const te::rst::Raster& srcRaster,
256  const unsigned int srcBandIdx, te::rst::Raster& dstRaster,
257  const unsigned int dstBandIdx, const bool useProgress );
258 
259  /*!
260  \brief Applay the erosion filter over the source raster band.
261  \param srcRaster Source raster.
262  \param srcBandIdx Source raster band index.
263  \param dstRaster Destination raster.
264  \param dstBandIdx Destination raster band index.
265  \param useProgress if true, the progress interface must be used.
266  */
267  bool ErosionFilter( const te::rst::Raster& srcRaster,
268  const unsigned int srcBandIdx, te::rst::Raster& dstRaster,
269  const unsigned int dstBandIdx, const bool useProgress );
270 
271  /*!
272  \brief Applay the user defined filter over the source raster band.
273  \param srcRaster Source raster.
274  \param srcBandIdx Source raster band index.
275  \param dstRaster Destination raster.
276  \param dstBandIdx Destination raster band index.
277  \param useProgress if true, the progress interface must be used.
278  */
279  bool UserDefinedFilter( const te::rst::Raster& srcRaster,
280  const unsigned int srcBandIdx, te::rst::Raster& dstRaster,
281  const unsigned int dstBandIdx, const bool useProgress );
282 
283  /*!
284  \brief Returns true if i < j.
285  \return Returns true if i < j.
286  */
287  static bool OrderFunction(double i, double j);
288 
289  /*!
290  \brief Resets the convolution buffer.
291  \param lines Convolution buffer lines.
292  \param columns Convolution buffer columns.
293  */
294  void ResetConvBuffer(unsigned int lines, unsigned int columns);
295 
296  /*!
297  \brief Updates the convolution buffer with a new raster line (rool up + read new raster line).
298  \param inBand Source raster band.
299  \param line Raster line.
300  \param band Raster band.
301  */
302  void UpdateConvBuffer(const te::rst::Band& inBand, unsigned int line);
303 
304  /*!
305  \brief Resets the convolution matrix.
306  \param lines Convolution matrix lines.
307  \param columns Convolution matrix columns.
308  */
309  void ResetConvMatrix(unsigned int lines, unsigned int columns);
310 
311  /*!
312  \brief Reset the convolution matrix and fills it with the user supplied matrix or with 1's if that matrix wasn't supplied.
313  */
314  void setMorfConvMatrix();
315 
316  };
317 
318  } // end namespace rp
319 } // end namespace te
320 
321 #endif
322 
#define _NOEXCEPT_OP(x)
Raster Processing algorithm input parameters base interface.
Raster Processing algorithm output parameters base interface.
A class to standardize algorithm parameters serialization.
Raster Processing algorithm base interface.
Definition: Algorithm.h:42
Filter input parameters.
Definition: Filter.h:56
te::rst::Raster const * m_inRasterPtr
Input raster.
Definition: Filter.h:75
unsigned int m_iterationsNumber
The number of iterations to perform (default:1).
Definition: Filter.h:79
void reset() _NOEXCEPT_OP(false)
Clear all internal allocated resources and reset the parameters instance to its initial state.
FilterType m_filterType
The edge filter type.
Definition: Filter.h:73
unsigned int m_windowW
The width of the convolution window.
Definition: Filter.h:83
bool m_enableProgress
Enable/Disable the progress interface (default:false).
Definition: Filter.h:85
InputParameters(const InputParameters &)
std::vector< unsigned int > m_inRasterBands
Bands to be used from the input raster 1.
Definition: Filter.h:77
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
bool m_enableRasterCache
Enable/Disable the use of a raster data cache (default:true).
Definition: Filter.h:89
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
Filter output parameters.
Definition: Filter.h:115
std::string m_rType
Output raster data source type (as described in te::raster::RasterFactory ).
Definition: Filter.h:118
std::unique_ptr< te::rst::Raster > m_outputRasterPtr
A pointer the ge generated output raster (label image).
Definition: Filter.h:122
std::map< std::string, std::string > m_rInfo
The necessary information to create the raster (as described in te::raster::RasterFactory).
Definition: Filter.h:120
OutputParameters(const OutputParameters &)
void reset() _NOEXCEPT_OP(false)
Clear all internal allocated resources and reset the parameters instance to its initial state.
A series of well-known filtering algorithms for images, linear and non-linear..
Definition: Filter.h:48
An abstract class for raster data strucutures.
Definition: Raster.h:72
TerraLib.
#define TERPEXPORT
You can use this macro in order to export/import classes and functions from this module.
Definition: Config.h:139
Abstract algorithm.