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