All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
Filter.cpp
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.cpp
22  \brief A series of well-known filtering algorithms for images, linear and non-linear.
23 */
24 
25 #include "Filter.h"
26 #include "Macros.h"
27 #include "Functions.h"
28 #include "../statistics/core/SummaryFunctions.h"
29 #include "../raster/RasterFactory.h"
30 #include "../raster/BandProperty.h"
31 #include "../raster/Grid.h"
32 #include "../raster/Band.h"
33 #include "../raster/BandIterator.h"
34 #include "../raster/BandIteratorWindow.h"
35 #include "../common/progress/TaskProgress.h"
36 
37 #include <memory>
38 #include <cmath>
39 
40 namespace te
41 {
42  namespace rp
43  {
44 
46  {
47  reset();
48  }
49 
51  {
52  reset();
53  operator=( other );
54  }
55 
57  {
58  reset();
59  }
60 
61  void Filter::InputParameters::reset() throw( te::rp::Exception )
62  {
63  m_filterType = InputParameters::InvalidFilterT;
64  m_inRasterPtr = 0;
65  m_inRasterBands.clear();
66  m_iterationsNumber = 1;
67  m_windowH = 3;
68  m_windowW = 3;
69  m_enableProgress = false;
70  m_window.clear();
71  }
72 
74  const Filter::InputParameters& params )
75  {
76  reset();
77 
78  m_filterType = params.m_filterType;
79  m_inRasterPtr = params.m_inRasterPtr;
80  m_inRasterBands = params.m_inRasterBands;
81  m_iterationsNumber = params.m_iterationsNumber;
82  m_windowH = params.m_windowH;
83  m_windowW = params.m_windowW;
84  m_enableProgress = params.m_enableProgress;
85 
86  if (m_filterType == InputParameters::UserDefinedWindowT)
87  m_window = params.m_window;
88 
89  return *this;
90  }
91 
93  {
94  return new InputParameters( *this );
95  }
96 
98  {
99  reset();
100  }
101 
103  {
104  reset();
105  operator=( other );
106  }
107 
109  {
110  reset();
111  }
112 
113  void Filter::OutputParameters::reset() throw( te::rp::Exception )
114  {
115  m_rType.clear();
116  m_rInfo.clear();
117  m_outputRasterPtr.reset();
118  }
119 
121  const Filter::OutputParameters& params )
122  {
123  reset();
124 
125  m_rType = params.m_rType;
126  m_rInfo = params.m_rInfo;
127 
128  return *this;
129  }
130 
132  {
133  return new OutputParameters( *this );
134  }
135 
137  {
138  reset();
139  }
140 
142  {
143  }
144 
146  throw( te::rp::Exception )
147  {
148  if( ! m_isInitialized ) return false;
149 
150  Filter::OutputParameters* outParamsPtr = dynamic_cast<
151  Filter::OutputParameters* >( &outputParams );
152  TERP_TRUE_OR_THROW( outParamsPtr, "Invalid paramters" );
153 
154  // Initializing the output rasters
155 
156  std::auto_ptr< te::rst::Raster > bufferRaster1Ptr;
157  std::auto_ptr< te::rst::Raster > bufferRaster2Ptr;
158 
159  {
160  std::vector< te::rst::BandProperty* > outRasterBandsProperties;
161  std::vector< te::rst::BandProperty* > bufferRaster1BandsProperties;
162  std::vector< te::rst::BandProperty* > bufferRaster2BandsProperties;
163 
164  for( unsigned int inRasterBandsIdx = 0 ; inRasterBandsIdx <
165  m_inputParameters.m_inRasterBands.size() ; ++inRasterBandsIdx )
166  {
167  const unsigned int& inRasterBandIndex =
168  m_inputParameters.m_inRasterBands[ inRasterBandsIdx ];
169 
170  assert( inRasterBandIndex <
172 
173  outRasterBandsProperties.push_back( new te::rst::BandProperty(
174  *( m_inputParameters.m_inRasterPtr->getBand( inRasterBandIndex )->getProperty() ) ) );
175 
177  {
178  bufferRaster1BandsProperties.push_back( new te::rst::BandProperty(
179  *outRasterBandsProperties[ inRasterBandsIdx ] ) );
180  bufferRaster1BandsProperties[ inRasterBandsIdx ]->m_type =
182  }
183 
185  {
186  bufferRaster2BandsProperties.push_back( new te::rst::BandProperty(
187  *outRasterBandsProperties[ inRasterBandsIdx ] ) );
188  bufferRaster2BandsProperties[ inRasterBandsIdx ]->m_type =
190  }
191  }
192 
193  outParamsPtr->m_outputRasterPtr.reset(
195  outParamsPtr->m_rType,
197  outRasterBandsProperties,
198  outParamsPtr->m_rInfo,
199  0,
200  0 ) );
201  TERP_TRUE_OR_RETURN_FALSE( outParamsPtr->m_outputRasterPtr.get(),
202  "Output raster creation error" );
203 
204  std::map< std::string, std::string > dummyRInfo;
205 
207  {
208  bufferRaster1Ptr.reset(
210  "MEM",
212  bufferRaster1BandsProperties,
213  dummyRInfo,
214  0,
215  0 ) );
216  TERP_TRUE_OR_RETURN_FALSE( bufferRaster1Ptr.get(),
217  "Output raster creation error" );
218  }
219 
221  {
222  bufferRaster2Ptr.reset(
224  "MEM",
226  bufferRaster2BandsProperties,
227  dummyRInfo,
228  0,
229  0 ) );
230  TERP_TRUE_OR_RETURN_FALSE( bufferRaster2Ptr.get(),
231  "Output raster creation error" );
232  }
233  }
234 
235  // Filtering
236 
237  FilterMethodPointerT filterPointer = 0;
239  {
241  {
242  filterPointer = &Filter::RobertsFilter;
243  break;
244  }
246  {
247  filterPointer = &Filter::SobelFilter;
248  break;
249  }
251  {
252  filterPointer = &Filter::MeanFilter;
253  break;
254  }
256  {
257  filterPointer = &Filter::ModeFilter;
258  break;
259  }
261  {
262  filterPointer = &Filter::MedianFilter;
263  break;
264  }
266  {
267  filterPointer = &Filter::DilationFilter;
268  break;
269  }
271  {
272  filterPointer = &Filter::ErosionFilter;
273  break;
274  }
276  {
277  filterPointer = &Filter::UserDefinedFilter;
278  break;
279  }
280  default :
281  {
282  TERP_LOG_AND_THROW( "Invalid filter type" );
283  break;
284  }
285  }
286 
287  const bool useGlobalProgress = ( ( m_inputParameters.m_iterationsNumber *
288  m_inputParameters.m_inRasterBands.size() ) != 1 );
289 
290  std::auto_ptr< te::common::TaskProgress > task;
291  if( m_inputParameters.m_enableProgress && useGlobalProgress )
292  {
293  task.reset( new te::common::TaskProgress(TE_TR("Filtering"),
296  m_inputParameters.m_inRasterBands.size() ) ) );
297  }
298 
299  te::rst::Raster const* srcRasterPtr = 0;
300  te::rst::Raster* dstRasterPtr = 0;
301  te::rst::Raster const* auxRasterPtr = 0;
302 
303  for( unsigned int iteration = 0 ; iteration <
304  m_inputParameters.m_iterationsNumber ; ++iteration )
305  {
306  // defining the source raster and
307  // destination raster
308 
309  if( iteration == 0 )
310  {
311  srcRasterPtr = m_inputParameters.m_inRasterPtr;
312 
314  {
315  dstRasterPtr = outParamsPtr->m_outputRasterPtr.get();
316  }
317  else
318  {
319  dstRasterPtr = bufferRaster1Ptr.get();
320  }
321  }
322  else if( iteration == ( m_inputParameters.m_iterationsNumber - 1 ) )
323  {
324  srcRasterPtr = dstRasterPtr;
325 
326  dstRasterPtr = outParamsPtr->m_outputRasterPtr.get();
327  }
328  else if( iteration == 1 )
329  {
330  srcRasterPtr = dstRasterPtr;
331 
332  dstRasterPtr = bufferRaster2Ptr.get();
333  }
334  else
335  {
336  auxRasterPtr = srcRasterPtr;
337  srcRasterPtr = dstRasterPtr;
338  dstRasterPtr = (te::rst::Raster*)auxRasterPtr;
339  }
340 
341  for( unsigned int inRasterBandsIdx = 0 ; inRasterBandsIdx <
342  m_inputParameters.m_inRasterBands.size() ; ++inRasterBandsIdx )
343  {
344  if( (this->*(filterPointer))( *srcRasterPtr, ( iteration == 0 ) ?
345  m_inputParameters.m_inRasterBands[ inRasterBandsIdx ] :
346  inRasterBandsIdx, *dstRasterPtr, inRasterBandsIdx,
347  ( m_inputParameters.m_enableProgress && (!useGlobalProgress) ) ) ==
348  false )
349  {
350  TERP_LOG_AND_RETURN_FALSE( TE_TR( "Filter error" ) );
351  }
352  }
353 
354  }
355 
356  return true;
357  }
358 
359  void Filter::reset() throw( te::rp::Exception )
360  {
361  m_isInitialized = false;
363  }
364 
365  bool Filter::initialize( const AlgorithmInputParameters& inputParams )
366  throw( te::rp::Exception )
367  {
368  reset();
369 
370  Filter::InputParameters const* inputParamsPtr = dynamic_cast<
371  Filter::InputParameters const* >( &inputParams );
372  TERP_TRUE_OR_RETURN_FALSE( inputParamsPtr, TE_TR( "Invalid parameters" ) );
373 
374 
375  TERP_TRUE_OR_RETURN_FALSE( inputParamsPtr->m_filterType !=
377  TE_TR( "Invalid filter type" ) );
378 
379  TERP_TRUE_OR_RETURN_FALSE( inputParamsPtr->m_inRasterPtr,
380  TE_TR( "Invalid raster pointer" ) );
383  TE_TR( "Invalid raster" ) );
384 
385  TERP_TRUE_OR_RETURN_FALSE( inputParamsPtr->m_inRasterBands.size() > 0,
386  TE_TR( "Invalid raster bands number" ) );
387  for( unsigned int inRasterBandsIdx = 0 ; inRasterBandsIdx <
388  inputParamsPtr->m_inRasterBands.size() ; ++inRasterBandsIdx )
389  {
391  inputParamsPtr->m_inRasterBands[ inRasterBandsIdx ] <
392  inputParamsPtr->m_inRasterPtr->getNumberOfBands(),
393  TE_TR( "Invalid raster bands" ) );
394  }
395 
396  TERP_TRUE_OR_RETURN_FALSE( inputParamsPtr->m_iterationsNumber > 0,
397  TE_TR( "Invalid iterations number" ) );
398 
399  TERP_TRUE_OR_RETURN_FALSE( ( inputParamsPtr->m_windowH > 2 ) &&
400  ( ( inputParamsPtr->m_windowH % 2 ) != 0 ),
401  TE_TR( "Invalid mask window height" ) );
402 
403  TERP_TRUE_OR_RETURN_FALSE( ( inputParamsPtr->m_windowW > 2 ) &&
404  ( ( inputParamsPtr->m_windowW % 2 ) != 0 ),
405  TE_TR( "Invalid mask window width" ) );
406 
407  if( inputParamsPtr->m_filterType == InputParameters::UserDefinedWindowT )
408  {
409  TERP_TRUE_OR_RETURN_FALSE( inputParamsPtr->m_window.size1() ==
410  inputParamsPtr->m_window.size2(),
411  TE_TR( "Invalid convolution matrix" ) );
412 
413  TERP_TRUE_OR_RETURN_FALSE( ( ( inputParamsPtr->m_window.size1() % 2 ) != 0 ),
414  TE_TR( "Invalid convolution matrix" ) );
415  }
416 
417  m_inputParameters = *inputParamsPtr;
418  m_isInitialized = true;
419 
420  return true;
421  }
422 
424  {
425  return m_isInitialized;
426  }
427 
428  bool Filter::RobertsFilter( const te::rst::Raster& srcRaster,
429  const unsigned int srcBandIdx, te::rst::Raster& dstRaster,
430  const unsigned int dstBandIdx, const bool useProgress )
431  {
433  dstRaster.getNumberOfColumns(), "Internal error" );
435  dstRaster.getNumberOfRows(), "Internal error" );
436  TERP_DEBUG_TRUE_OR_THROW( srcBandIdx < srcRaster.getNumberOfBands(),
437  "Internal error" );
438  TERP_DEBUG_TRUE_OR_THROW( dstBandIdx < dstRaster.getNumberOfBands(),
439  "Internal error" );
440 
441  const unsigned int nRows = (unsigned int)( srcRaster.getNumberOfRows() );
442  const unsigned int rowsBound = (unsigned int)( nRows ?
443  ( nRows - 1 ) : 0 );
444 
445  const unsigned int nCols = (unsigned int)( srcRaster.getNumberOfColumns() );
446  const unsigned int colsBound = (unsigned int)( nCols ?
447  ( nCols - 1 ) : 0 );
448 
449  std::auto_ptr< te::common::TaskProgress > task;
450  if( useProgress )
451  {
452  task.reset( new te::common::TaskProgress(TE_TR("Mean Filter"),
454  srcRaster.getNumberOfRows() - 2) );
455  }
456 
457  const te::rst::Band& srcBand = *srcRaster.getBand( srcBandIdx );
458  te::rst::Band& dstBand = *dstRaster.getBand( dstBandIdx );
459 
460  const double srcNoDataValue = srcBand.getProperty()->m_noDataValue;
461  const double dstNoDataValue = dstBand.getProperty()->m_noDataValue;
462 
463  double dstBandAllowedMin = 0;
464  double dstBandAllowedMax = 0;
465  te::rp::GetDataTypeRange( dstBand.getProperty()->getType(), dstBandAllowedMin,
466  dstBandAllowedMax );
467 
468  unsigned int col = 0;
469  double value1diag = 0;
470  double value2diag = 0;
471  double value1adiag = 0;
472  double value2adiag = 0;
473  double diagDiff = 0;
474  double adiagDiff = 0;
475  double outValue = 0;
476 
477  for( unsigned int row = 0; row < nRows ; ++row )
478  {
479  for( col = 0 ; col < nCols ; ++col )
480  {
481  if( ( row < rowsBound ) && ( col < colsBound ) )
482  {
483  srcBand.getValue( col, row, value1diag );
484  if( value1diag == srcNoDataValue )
485  {
486  dstBand.setValue( col, row, dstNoDataValue );
487  continue;
488  }
489 
490  srcBand.getValue( col + 1, row + 1, value2diag );
491  if( value2diag == srcNoDataValue )
492  {
493  dstBand.setValue( col, row, dstNoDataValue );
494  continue;
495  }
496 
497  srcBand.getValue( col, row + 1, value1adiag );
498  if( value1adiag == srcNoDataValue )
499  {
500  dstBand.setValue( col, row, dstNoDataValue );
501  continue;
502  }
503 
504  srcBand.getValue( col + 1, row, value2adiag );
505  if( value2adiag == srcNoDataValue )
506  {
507  dstBand.setValue( col, row, dstNoDataValue );
508  continue;
509  }
510 
511  diagDiff = value1diag - value2diag;
512  adiagDiff = value1adiag - value2adiag;
513 
514  outValue = std::sqrt( ( diagDiff * diagDiff ) +
515  ( adiagDiff * adiagDiff ) );
516  outValue = std::max( outValue, dstBandAllowedMin );
517  outValue = std::min( outValue, dstBandAllowedMax );
518 
519  dstBand.setValue( col, row, outValue );
520  }
521  else
522  {
523  dstBand.setValue( col, row, dstNoDataValue );
524  }
525  }
526 
527  if( useProgress )
528  {
529  task->pulse();
530  if( !task->isActive() ) return false;
531  }
532  }
533 
534  return true;
535  }
536 
537  bool Filter::SobelFilter( const te::rst::Raster& srcRaster,
538  const unsigned int srcBandIdx, te::rst::Raster& dstRaster,
539  const unsigned int dstBandIdx, const bool useProgress )
540  {
542  dstRaster.getNumberOfColumns(), "Internal error" );
544  dstRaster.getNumberOfRows(), "Internal error" );
545  TERP_DEBUG_TRUE_OR_THROW( srcBandIdx < srcRaster.getNumberOfBands(),
546  "Internal error" );
547  TERP_DEBUG_TRUE_OR_THROW( dstBandIdx < dstRaster.getNumberOfBands(),
548  "Internal error" );
549 
550  const unsigned int nRows = (unsigned int)( srcRaster.getNumberOfRows() );
551  const unsigned int rowsBound = (unsigned int)( nRows ?
552  ( nRows - 1 ) : 0 );
553 
554  const unsigned int nCols = (unsigned int)( srcRaster.getNumberOfColumns() );
555  const unsigned int colsBound = (unsigned int)( nCols ?
556  ( nCols - 1 ) : 0 );
557 
558  std::auto_ptr< te::common::TaskProgress > task;
559  if( useProgress )
560  {
561  task.reset( new te::common::TaskProgress(TE_TR("Mean Filter"),
563  srcRaster.getNumberOfRows() - 2) );
564  }
565 
566  const te::rst::Band& srcBand = *srcRaster.getBand( srcBandIdx );
567  te::rst::Band& dstBand = *dstRaster.getBand( dstBandIdx );
568 
569  const double srcNoDataValue = srcBand.getProperty()->m_noDataValue;
570  const double dstNoDataValue = dstBand.getProperty()->m_noDataValue;
571 
572  double dstBandAllowedMin = 0;
573  double dstBandAllowedMax = 0;
574  te::rp::GetDataTypeRange( dstBand.getProperty()->getType(), dstBandAllowedMin,
575  dstBandAllowedMax );
576 
577  unsigned int col = 0;
578  double value1 = 0;
579  double value2 = 0;
580  double value3 = 0;
581  double value4 = 0;
582  double value5 = 0;
583  double value6 = 0;
584  double value7 = 0;
585  double value8 = 0;
586  double gY = 0;
587  double gX = 0;
588  double outValue = 0;
589 
590  for( unsigned int row = 0; row < nRows ; ++row )
591  {
592  for( col = 0 ; col < nCols ; ++col )
593  {
594  if( ( row > 0 ) && ( col > 0 ) && ( row < rowsBound ) &&
595  ( col < colsBound ) )
596  {
597  srcBand.getValue( col - 1, row - 1, value1 );
598  if( value1 == srcNoDataValue )
599  {
600  dstBand.setValue( col, row, dstNoDataValue );
601  continue;
602  }
603 
604  srcBand.getValue( col, row - 1, value2 );
605  if( value2 == srcNoDataValue )
606  {
607  dstBand.setValue( col, row, dstNoDataValue );
608  continue;
609  }
610 
611  srcBand.getValue( col + 1, row - 1, value3 );
612  if( value3 == srcNoDataValue )
613  {
614  dstBand.setValue( col, row, dstNoDataValue );
615  continue;
616  }
617 
618  srcBand.getValue( col - 1, row, value4 );
619  if( value4 == srcNoDataValue )
620  {
621  dstBand.setValue( col, row, dstNoDataValue );
622  continue;
623  }
624 
625  srcBand.getValue( col + 1, row, value5 );
626  if( value5 == srcNoDataValue )
627  {
628  dstBand.setValue( col, row, dstNoDataValue );
629  continue;
630  }
631 
632  srcBand.getValue( col - 1, row + 1, value6 );
633  if( value6 == srcNoDataValue )
634  {
635  dstBand.setValue( col, row, dstNoDataValue );
636  continue;
637  }
638 
639  srcBand.getValue( col, row + 1, value7 );
640  if( value7 == srcNoDataValue )
641  {
642  dstBand.setValue( col, row, dstNoDataValue );
643  continue;
644  }
645 
646  srcBand.getValue( col + 1, row + 1, value8 );
647  if( value8 == srcNoDataValue )
648  {
649  dstBand.setValue( col, row, dstNoDataValue );
650  continue;
651  }
652 
653  gY = value6 + ( 2.0 * value7 ) + value8 -
654  ( value1 + ( 2.0 * value2 ) + value3 );
655  gX = value3 + ( 2.0 * value5 ) + value8 -
656  ( value1 + ( 2.0 * value4 ) + value6 );
657 
658  outValue = std::sqrt( ( gY * gY ) +
659  ( gX * gX ) );
660  outValue = std::max( outValue, dstBandAllowedMin );
661  outValue = std::min( outValue, dstBandAllowedMax );
662 
663  dstBand.setValue( col, row, outValue );
664  }
665  else
666  {
667  dstBand.setValue( col, row, dstNoDataValue );
668  }
669  }
670 
671  if( useProgress )
672  {
673  task->pulse();
674  if( !task->isActive() ) return false;
675  }
676  }
677 
678  return true;
679  }
680 
681  bool Filter::MeanFilter( const te::rst::Raster& srcRaster,
682  const unsigned int srcBandIdx, te::rst::Raster& dstRaster,
683  const unsigned int dstBandIdx, const bool useProgress )
684  {
686  dstRaster.getNumberOfColumns(), "Internal error" );
688  dstRaster.getNumberOfRows(), "Internal error" );
689  TERP_DEBUG_TRUE_OR_THROW( srcBandIdx < srcRaster.getNumberOfBands(),
690  "Internal error" );
691  TERP_DEBUG_TRUE_OR_THROW( dstBandIdx < dstRaster.getNumberOfBands(),
692  "Internal error" );
693 
694  const unsigned int H = m_inputParameters.m_windowH;
695  const unsigned int W = m_inputParameters.m_windowW;
696 
697  std::auto_ptr< te::common::TaskProgress > task;
698  if( useProgress )
699  {
700  task.reset( new te::common::TaskProgress(TE_TR("Mean Filter"),
702  }
703 
704  boost::numeric::ublas::matrix<double> window_mean(H, W);
705  double pixel_mean;
706  for (unsigned int i = 0; i < H; i++)
707  for (unsigned int j = 0; j < W; j++)
708  window_mean(i, j) = 1.0 / (W * H);
709 
710  unsigned int R = 0;
711  unsigned int C = 0;
712  const unsigned int MR = srcRaster.getNumberOfRows();
713  const unsigned int MC = srcRaster.getNumberOfColumns();
714 
715  double dstBandAllowedMin = 0;
716  double dstBandAllowedMax = 0;
717  te::rp::GetDataTypeRange( dstRaster.getBand( dstBandIdx )->getProperty()->getType(), dstBandAllowedMin,
718  dstBandAllowedMax );
719 
720  te::rst::Band const * const band = srcRaster.getBand(srcBandIdx);
723 
724  while (it != itend)
725  {
726  R = it.getRow();
727  C = it.getColumn();
728 
729  if ((R >= (unsigned)(H / 2) && R < MR - (H / 2)) &&
730  (C >= (unsigned)(W / 2) && C < MC - (W / 2)))
731  {
732  pixel_mean = 0.0;
733  for (int r = -1 * (int) (H / 2), rw = 0; r <= (int) (H / 2); r++, rw++)
734  for (int c = -1 * (int) (W / 2), cw = 0; c <= (int) (W / 2); c++, cw++)
735  pixel_mean += window_mean(rw, cw) * it.getValue(c, r);
736  }
737  else
738  pixel_mean = it.getValue();
739 
740  pixel_mean = std::max( pixel_mean, dstBandAllowedMin );
741  pixel_mean = std::min( pixel_mean, dstBandAllowedMax );
742  dstRaster.setValue(C, R, pixel_mean, dstBandIdx);
743 
744  if( useProgress )
745  {
746  task->setCurrentStep( R / srcRaster.getNumberOfRows() );
747  if( !task->isActive() ) return false;
748  }
749  ++it;
750  }
751 
752  return true;
753  }
754 
755  bool Filter::ModeFilter( const te::rst::Raster& srcRaster,
756  const unsigned int srcBandIdx, te::rst::Raster& dstRaster,
757  const unsigned int dstBandIdx, const bool useProgress )
758  {
760  dstRaster.getNumberOfColumns(), "Internal error" );
762  dstRaster.getNumberOfRows(), "Internal error" );
763  TERP_DEBUG_TRUE_OR_THROW( srcBandIdx < srcRaster.getNumberOfBands(),
764  "Internal error" );
765  TERP_DEBUG_TRUE_OR_THROW( dstBandIdx < dstRaster.getNumberOfBands(),
766  "Internal error" );
767 
768  const unsigned int H = m_inputParameters.m_windowH;
769  const unsigned int W = m_inputParameters.m_windowW;
770 
771  std::auto_ptr< te::common::TaskProgress > task;
772  if( useProgress )
773  {
774  task.reset( new te::common::TaskProgress(TE_TR("Mode Filter"),
776  }
777 
778  unsigned int R = 0;
779  unsigned int C = 0;
780  const unsigned int MR = srcRaster.getNumberOfRows();
781  const unsigned int MC = srcRaster.getNumberOfColumns();
782 
783  double dstBandAllowedMin = 0;
784  double dstBandAllowedMax = 0;
785  te::rp::GetDataTypeRange( dstRaster.getBand( dstBandIdx )->getProperty()->getType(), dstBandAllowedMin,
786  dstBandAllowedMax );
787 
788  std::vector<double> pixels_in_window;
789  double pixel_mode = 0;
790 
791  te::rst::Band const * const band = srcRaster.getBand(srcBandIdx);
794 
795  while (it != itend)
796  {
797  R = it.getRow();
798  C = it.getColumn();
799 
800  pixel_mode = it.getValue();
801  if ((R >= (unsigned)(H / 2) && R < MR - (H / 2)) &&
802  (C >= (unsigned)(W / 2) && C < MC - (W / 2)))
803  {
804  pixels_in_window.clear();
805  for (int r = -1 * (int) (H / 2); r <= (int) (H / 2); r++)
806  for (int c = -1 * (int) (W / 2); c <= (int) (W / 2); c++)
807  pixels_in_window.push_back(it.getValue(c, r));
808  std::vector<double> vector_mode = te::stat::Mode(pixels_in_window);
809  if (vector_mode.size() > 0)
810  pixel_mode = vector_mode[0];
811  }
812 
813  pixel_mode = std::max( pixel_mode, dstBandAllowedMin );
814  pixel_mode = std::min( pixel_mode, dstBandAllowedMax );
815  dstRaster.setValue(C, R, pixel_mode, dstBandIdx);
816 
817  if( useProgress )
818  {
819  task->setCurrentStep( R / srcRaster.getNumberOfRows() );
820  if( !task->isActive() ) return false;
821  }
822 
823  ++it;
824  }
825 
826  return true;
827  }
828 
829  bool Filter::MedianFilter( const te::rst::Raster& srcRaster,
830  const unsigned int srcBandIdx, te::rst::Raster& dstRaster,
831  const unsigned int dstBandIdx, const bool useProgress )
832  {
834  dstRaster.getNumberOfColumns(), "Internal error" );
836  dstRaster.getNumberOfRows(), "Internal error" );
837  TERP_DEBUG_TRUE_OR_THROW( srcBandIdx < srcRaster.getNumberOfBands(),
838  "Internal error" );
839  TERP_DEBUG_TRUE_OR_THROW( dstBandIdx < dstRaster.getNumberOfBands(),
840  "Internal error" );
841 
842  const unsigned int H = m_inputParameters.m_windowH;
843  const unsigned int W = m_inputParameters.m_windowW;
844 
845  std::auto_ptr< te::common::TaskProgress > task;
846  if( useProgress )
847  {
848  task.reset( new te::common::TaskProgress(TE_TR("Median Filter"),
850  }
851 
852  unsigned int R = 0;
853  unsigned int C = 0;
854  const unsigned int MR = srcRaster.getNumberOfRows();
855  const unsigned int MC = srcRaster.getNumberOfColumns();
856 
857  double dstBandAllowedMin = 0;
858  double dstBandAllowedMax = 0;
859  te::rp::GetDataTypeRange( dstRaster.getBand( dstBandIdx )->getProperty()->getType(), dstBandAllowedMin,
860  dstBandAllowedMax );
861 
862  std::vector<double> pixels_in_window;
863  double pixel_median = 0.0;
864 
865  te::rst::Band const * const band = srcRaster.getBand(srcBandIdx);
868 
869  while (it != itend)
870  {
871  R = it.getRow();
872  C = it.getColumn();
873 
874  if ((R >= (unsigned)(H / 2) && R < MR - (H / 2)) &&
875  (C >= (unsigned)(W / 2) && C < MC - (W / 2)))
876  {
877  pixel_median = 0.0;
878  pixels_in_window.clear();
879  for (int r = -1 * (int) (H / 2); r <= (int) (H / 2); r++)
880  for (int c = -1 * (int) (W / 2); c <= (int) (W / 2); c++)
881  pixels_in_window.push_back(it.getValue(c, r));
882 
883  std::sort(pixels_in_window.begin(), pixels_in_window.end(), OrderFunction);
884  pixel_median = pixels_in_window[pixels_in_window.size() / 2];
885  }
886  else
887  pixel_median = it.getValue();
888 
889  pixel_median = std::max( pixel_median, dstBandAllowedMin );
890  pixel_median = std::min( pixel_median, dstBandAllowedMax );
891  dstRaster.setValue(C, R, pixel_median, dstBandIdx);
892 
893  if( useProgress )
894  {
895  task->setCurrentStep( R / srcRaster.getNumberOfRows() );
896  if( !task->isActive() ) return false;
897  }
898 
899  ++it;
900  }
901 
902  return true;
903  }
904 
905  bool Filter::DilationFilter( const te::rst::Raster& srcRaster,
906  const unsigned int srcBandIdx, te::rst::Raster& dstRaster,
907  const unsigned int dstBandIdx, const bool useProgress )
908  {
910  dstRaster.getNumberOfColumns(), "Internal error" );
912  dstRaster.getNumberOfRows(), "Internal error" );
913  TERP_DEBUG_TRUE_OR_THROW( srcBandIdx < srcRaster.getNumberOfBands(),
914  "Internal error" );
915  TERP_DEBUG_TRUE_OR_THROW( dstBandIdx < dstRaster.getNumberOfBands(),
916  "Internal error" );
917 
918  const unsigned int H = m_inputParameters.m_windowH;
919  const unsigned int W = m_inputParameters.m_windowW;
920 
921  std::auto_ptr< te::common::TaskProgress > task;
922  if( useProgress )
923  {
924  task.reset( new te::common::TaskProgress(TE_TR("Dilation Filter"),
926  }
927 
928  unsigned int R = 0;
929  unsigned int C = 0;
930  const unsigned int MR = srcRaster.getNumberOfRows();
931  const unsigned int MC = srcRaster.getNumberOfColumns();
932 
933  double dstBandAllowedMin = 0;
934  double dstBandAllowedMax = 0;
935  te::rp::GetDataTypeRange( dstRaster.getBand( dstBandIdx )->getProperty()->getType(), dstBandAllowedMin,
936  dstBandAllowedMax );
937 
938  double pixel_dilation = 0;
939  double pixel = 0;
940 
941  te::rst::Band const * const band = srcRaster.getBand(srcBandIdx);
944 
945  while (it != itend)
946  {
947  R = it.getRow();
948  C = it.getColumn();
949 
950  if ((R >= (unsigned)(H / 2) && R < MR - (H / 2)) &&
951  (C >= (unsigned)(W / 2) && C < MC - (W / 2)))
952  {
953  pixel_dilation = -1.0 * std::numeric_limits<double>::max();
954  for (int r = -1 * (int) (H / 2); r <= (int) (H / 2); r++)
955  for (int c = -1 * (int) (W / 2); c <= (int) (W / 2); c++)
956  {
957  pixel = it.getValue(c, r);
958  if (pixel > pixel_dilation)
959  pixel_dilation = pixel;
960  }
961  }
962  else
963  pixel_dilation = it.getValue();
964 
965  pixel_dilation = std::max( pixel_dilation, dstBandAllowedMin );
966  pixel_dilation = std::min( pixel_dilation, dstBandAllowedMax );
967  dstRaster.setValue(C, R, pixel_dilation, dstBandIdx);
968 
969  if( useProgress )
970  {
971  task->setCurrentStep( R / srcRaster.getNumberOfRows() );
972  if( !task->isActive() ) return false;
973  }
974 
975  ++it;
976  }
977 
978  return true;
979  }
980 
981  bool Filter::ErosionFilter( const te::rst::Raster& srcRaster,
982  const unsigned int srcBandIdx, te::rst::Raster& dstRaster,
983  const unsigned int dstBandIdx, const bool useProgress )
984  {
986  dstRaster.getNumberOfColumns(), "Internal error" );
988  dstRaster.getNumberOfRows(), "Internal error" );
989  TERP_DEBUG_TRUE_OR_THROW( srcBandIdx < srcRaster.getNumberOfBands(),
990  "Internal error" );
991  TERP_DEBUG_TRUE_OR_THROW( dstBandIdx < dstRaster.getNumberOfBands(),
992  "Internal error" );
993 
994  const unsigned int H = m_inputParameters.m_windowH;
995  const unsigned int W = m_inputParameters.m_windowW;
996 
997  std::auto_ptr< te::common::TaskProgress > task;
998  if( useProgress )
999  {
1000  task.reset( new te::common::TaskProgress(TE_TR("Erosion Filter"),
1002  }
1003 
1004  unsigned int R = 0;
1005  unsigned int C = 0;
1006  const unsigned int MR = srcRaster.getNumberOfRows();
1007  const unsigned int MC = srcRaster.getNumberOfColumns();
1008 
1009  double dstBandAllowedMin = 0;
1010  double dstBandAllowedMax = 0;
1011  te::rp::GetDataTypeRange( dstRaster.getBand( dstBandIdx )->getProperty()->getType(), dstBandAllowedMin,
1012  dstBandAllowedMax );
1013 
1014  double pixel_erosion = 0;
1015  double pixel = 0;
1016 
1017  te::rst::Band const * const band = srcRaster.getBand(srcBandIdx);
1020 
1021  while (it != itend)
1022  {
1023  R = it.getRow();
1024  C = it.getColumn();
1025 
1026  if ((R >= (unsigned)(H / 2) && R < MR - (H / 2)) &&
1027  (C >= (unsigned)(W / 2) && C < MC - (W / 2)))
1028  {
1029  pixel_erosion = std::numeric_limits<double>::max();
1030  for (int r = -1 * (int) (H / 2); r <= (int) (H / 2); r++)
1031  for (int c = -1 * (int) (W / 2); c <= (int) (W / 2); c++)
1032  {
1033  pixel = it.getValue(c, r);
1034  if (pixel < pixel_erosion)
1035  pixel_erosion = pixel;
1036  }
1037  }
1038  else
1039  pixel_erosion = it.getValue();
1040 
1041  pixel_erosion = std::max( pixel_erosion, dstBandAllowedMin );
1042  pixel_erosion = std::min( pixel_erosion, dstBandAllowedMax );
1043  dstRaster.setValue(C, R, pixel_erosion, dstBandIdx);
1044 
1045  if( useProgress )
1046  {
1047  task->setCurrentStep( R / srcRaster.getNumberOfRows() );
1048  if( !task->isActive() ) return false;
1049  }
1050 
1051  ++it;
1052  }
1053 
1054  return true;
1055  }
1056 
1058  const unsigned int srcBandIdx, te::rst::Raster& dstRaster,
1059  const unsigned int dstBandIdx, const bool useProgress )
1060  {
1062  dstRaster.getNumberOfColumns(), "Internal error" );
1064  dstRaster.getNumberOfRows(), "Internal error" );
1065  TERP_DEBUG_TRUE_OR_THROW( srcBandIdx < srcRaster.getNumberOfBands(),
1066  "Internal error" );
1067  TERP_DEBUG_TRUE_OR_THROW( dstBandIdx < dstRaster.getNumberOfBands(),
1068  "Internal error" );
1069 
1070  const unsigned int H = m_inputParameters.m_windowH;
1071  const unsigned int W = m_inputParameters.m_windowW;
1072 
1073  std::auto_ptr< te::common::TaskProgress > task;
1074  if( useProgress )
1075  {
1076  task.reset( new te::common::TaskProgress(TE_TR("User Defined Filter"),
1078  }
1079 
1080  unsigned int R = 0;
1081  unsigned int C = 0;
1082  const unsigned int MR = srcRaster.getNumberOfRows();
1083  const unsigned int MC = srcRaster.getNumberOfColumns();
1084  double pixels_in_window = 0;
1085 
1086  double dstBandAllowedMin = 0;
1087  double dstBandAllowedMax = 0;
1088  te::rp::GetDataTypeRange( dstRaster.getBand( dstBandIdx )->getProperty()->getType(), dstBandAllowedMin,
1089  dstBandAllowedMax );
1090 
1091  te::rst::Band const * const band = srcRaster.getBand(srcBandIdx);
1094 
1095  while (it != itend)
1096  {
1097  R = it.getRow();
1098  C = it.getColumn();
1099  if ((R >= (unsigned)(H / 2) && R < MR - (H / 2)) &&
1100  (C >= (unsigned)(W / 2) && C < MC - (W / 2)))
1101  {
1102  pixels_in_window = 0.0;
1103  for (int r = -1 * (int) (H / 2), rw = 0; r <= (int) (H / 2); r++, rw++)
1104  for (int c = -1 * (int) (W / 2), cw = 0; c <= (int) (W / 2); c++, cw++)
1105  pixels_in_window += m_inputParameters.m_window(rw, cw) * it.getValue(c, r);
1106  }
1107  else
1108  pixels_in_window = it.getValue();
1109 
1110  pixels_in_window = std::max( pixels_in_window, dstBandAllowedMin );
1111  pixels_in_window = std::min( pixels_in_window, dstBandAllowedMax );
1112  dstRaster.setValue(C, R, pixels_in_window, dstBandIdx);
1113 
1114  if( useProgress )
1115  {
1116  task->setCurrentStep( R / srcRaster.getNumberOfRows() );
1117  if( !task->isActive() ) return false;
1118  }
1119 
1120  ++it;
1121  }
1122 
1123  return true;
1124  }
1125 
1126  bool Filter::OrderFunction(double i, double j)
1127  {
1128  return (i < j);
1129  }
1130 
1131  } // end namespace rp
1132 } // end namespace te
1133 
The resultant pixel will be the mode of pixels in the convolution window. When the window is multimod...
Definition: Filter.h:66
virtual void setValue(unsigned int c, unsigned int r, const double value, std::size_t b=0)
Sets the attribute value in a band of a cell.
Definition: Raster.cpp:233
bool MeanFilter(const te::rst::Raster &srcRaster, const unsigned int srcBandIdx, te::rst::Raster &dstRaster, const unsigned int dstBandIdx, const bool useProgress)
Applay the mean filter over the source raster band.
Definition: Filter.cpp:681
The resultant pixel will be the mean of pixels in the convolution window.
Definition: Filter.h:65
bool ModeFilter(const te::rst::Raster &srcRaster, const unsigned int srcBandIdx, te::rst::Raster &dstRaster, const unsigned int dstBandIdx, const bool useProgress)
Applay the mode filter over the source raster band.
Definition: Filter.cpp:755
virtual void getValue(unsigned int c, unsigned int r, double &value) const =0
Returns the cell attribute value.
bool m_enableProgress
Enable/Disable the progress interface (default:false).
Definition: Filter.h:85
TESTATEXPORT std::vector< double > Mode(const std::vector< double > &values)
A raster band description.
Definition: BandProperty.h:61
AbstractParameters * clone() const
Create a clone copy of this instance.
Definition: Filter.cpp:92
unsigned int getNumberOfColumns() const
Returns the raster number of columns.
Definition: Raster.cpp:213
virtual const Band * getBand(std::size_t i) const =0
Returns the raster i-th band.
unsigned int getColumn() const
Returns the current column in iterator.
The resultant pixel will be the lowest pixel value in the convolution window.
Definition: Filter.h:69
static bool OrderFunction(double i, double j)
Returns true if i < j.
Definition: Filter.cpp:1126
void TERPEXPORT GetDataTypeRange(const int dataType, double &min, double &max)
Returns the real data type range (all values that can be represented by the given data type)...
Definition: Functions.h:180
This class can be used to inform the progress of a task.
Definition: TaskProgress.h:53
Filter::InputParameters m_inputParameters
Input parameters.
Definition: Filter.h:168
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
double m_noDataValue
Value to indicate elements where there is no data, default is std::numeric_limits::max().
Definition: BandProperty.h:136
bool ErosionFilter(const te::rst::Raster &srcRaster, const unsigned int srcBandIdx, te::rst::Raster &dstRaster, const unsigned int dstBandIdx, const bool useProgress)
Applay the erosion filter over the source raster band.
Definition: Filter.cpp:981
#define TE_TR(message)
It marks a string in order to get translated.
Definition: Translator.h:347
void reset()
Clear all internal allocated resources and reset the parameters instance to its initial state...
Definition: Filter.cpp:61
unsigned int getRow() const
Returns the current row in iterator.
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
Raster Processing functions.
#define TERP_TRUE_OR_RETURN_FALSE(value, message)
Checks if value is true. For false values a warning message will be logged and a return of context wi...
Definition: Macros.h:183
const Algorithm & operator=(const Algorithm &)
Definition: Algorithm.cpp:43
te::common::AccessPolicy getAccessPolicy() const
Returns the raster access policy.
Definition: Raster.cpp:89
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
bool(Filter::* FilterMethodPointerT)(const te::rst::Raster &srcRaster, const unsigned int srcBandIdx, te::rst::Raster &dstRaster, const unsigned int dstBandIdx, const bool useProgress)
Type definition for a filter method pointer.
Definition: Filter.h:162
The resultant pixel will be the median of pixels in the convolution window.
Definition: Filter.h:67
unsigned int m_iterationsNumber
The number of iterations to perform (default:1).
Definition: Filter.h:79
The resultant pixel will be the highest pixel value in the convolution window.
Definition: Filter.h:68
An abstract class for raster data strucutures.
Definition: Raster.h:71
unsigned int getNumberOfRows() const
Returns the raster number of rows.
Definition: Raster.cpp:208
#define TERP_LOG_AND_THROW(message)
Logs a error message and throws.
Definition: Macros.h:138
BandProperty * getProperty()
Returns the band property.
Definition: Band.cpp:428
virtual std::size_t getNumberOfBands() const =0
Returns the number of bands (dimension of cells attribute values) in the raster.
AbstractParameters * clone() const
Create a clone copy of this instance.
Definition: Filter.cpp:131
bool m_isInitialized
Is this instance already initialized?
Definition: Filter.h:166
bool SobelFilter(const te::rst::Raster &srcRaster, const unsigned int srcBandIdx, te::rst::Raster &dstRaster, const unsigned int dstBandIdx, const bool useProgress)
Applay the Sobel filter over the source raster band.
Definition: Filter.cpp:537
bool isInitialized() const
Returns true if the algorithm instance is initialized and ready for execution.
Definition: Filter.cpp:423
A raster band description.
Definition: Band.h:63
Grid * getGrid()
It returns the raster grid.
Definition: Raster.cpp:94
const InputParameters & operator=(const InputParameters &params)
Definition: Filter.cpp:73
static BandIteratorWindow end(Band const *const b, std::size_t w, const std::size_t h)
Returns an iterator referring to after the end of the iterator.
bool RobertsFilter(const te::rst::Raster &srcRaster, const unsigned int srcBandIdx, te::rst::Raster &dstRaster, const unsigned int dstBandIdx, const bool useProgress)
Applay the Roberts filter over the source raster band.
Definition: Filter.cpp:428
virtual void setValue(unsigned int c, unsigned int r, const double value)=0
Sets the cell attribute value.
Filter input parameters.
Definition: Filter.h:55
Abstract parameters base interface.
#define TERP_LOG_AND_RETURN_FALSE(message)
Logs a warning message will and return false.
Definition: Macros.h:236
A series of well-known filtering algorithms for images, linear and non-linear.
bool initialize(const AlgorithmInputParameters &inputParams)
Initialize the algorithm instance making it ready for execution.
Definition: Filter.cpp:365
unsigned int m_windowW
The width of the convolution window.
Definition: Filter.h:83
static Raster * make()
It creates and returns an empty raster with default raster driver.
FilterType m_filterType
The edge filter type.
Definition: Filter.h:73
Raster Processing algorithm input parameters base interface.
std::auto_ptr< te::rst::Raster > m_outputRasterPtr
A pointer the ge generated output raster (label image).
Definition: Filter.h:117
te::rst::Raster const * m_inRasterPtr
Input raster.
Definition: Filter.h:75
int getType() const
It returns the data type of the elements in the band.
Definition: BandProperty.h:113
void reset()
Clear all internal allocated resources and reset the parameters instance to its initial state...
Definition: Filter.cpp:113
bool UserDefinedFilter(const te::rst::Raster &srcRaster, const unsigned int srcBandIdx, te::rst::Raster &dstRaster, const unsigned int dstBandIdx, const bool useProgress)
Applay the user defined filter over the source raster band.
Definition: Filter.cpp:1057
const OutputParameters & operator=(const OutputParameters &params)
Definition: Filter.cpp:120
#define TERP_DEBUG_TRUE_OR_THROW(value, message)
Checks if value is true and throws an exception if not.
Definition: Macros.h:356
Filter output parameters.
Definition: Filter.h:109
void reset()
Clear all internal allocated objects and reset the algorithm to its initial state.
Definition: Filter.cpp:359
A rectified grid is the spatial support for raster data.
Definition: Grid.h:68
It implements and iterator to "navigate" over a single band, optimized by a window structure (e...
bool MedianFilter(const te::rst::Raster &srcRaster, const unsigned int srcBandIdx, te::rst::Raster &dstRaster, const unsigned int dstBandIdx, const bool useProgress)
Applay the median filter over the source raster band.
Definition: Filter.cpp:829
static BandIteratorWindow begin(Band const *const b, std::size_t w, const std::size_t h)
Returns an iterator referring to the first value of the band.
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
T getValue() const
Returns the value in current position (column, row) from iterator.
#define TERP_TRUE_OR_THROW(value, message)
Checks if value is true and throws an exception if not.
Definition: Macros.h:149
The user will define the weights of a convolution window.
Definition: Filter.h:70
bool DilationFilter(const te::rst::Raster &srcRaster, const unsigned int srcBandIdx, te::rst::Raster &dstRaster, const unsigned int dstBandIdx, const bool useProgress)
Applay the dilation filter over the source raster band.
Definition: Filter.cpp:905
bool execute(AlgorithmOutputParameters &outputParams)
Executes the algorithm using the supplied parameters.
Definition: Filter.cpp:145