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" 
   65       m_inRasterBands.clear();
 
   66       m_iterationsNumber = 1;
 
   69       m_enableProgress = 
false;
 
  117       m_outputRasterPtr.reset();
 
  146       throw( te::rp::Exception )
 
  156       std::auto_ptr< te::rst::Raster > bufferRaster1Ptr;
 
  157       std::auto_ptr< te::rst::Raster > bufferRaster2Ptr;
 
  160         std::vector< te::rst::BandProperty* > outRasterBandsProperties;
 
  161         std::vector< te::rst::BandProperty* > bufferRaster1BandsProperties;
 
  162         std::vector< te::rst::BandProperty* > bufferRaster2BandsProperties;
 
  164         for( 
unsigned int inRasterBandsIdx = 0 ; inRasterBandsIdx <
 
  167           const unsigned int& inRasterBandIndex =
 
  170           assert( inRasterBandIndex <
 
  179               *outRasterBandsProperties[ inRasterBandsIdx ] ) );
 
  180             bufferRaster1BandsProperties[ inRasterBandsIdx ]->m_type =
 
  187               *outRasterBandsProperties[ inRasterBandsIdx ] ) );
 
  188             bufferRaster2BandsProperties[ inRasterBandsIdx ]->m_type =
 
  197             outRasterBandsProperties,
 
  202           "Output raster creation error" );
 
  204         std::map< std::string, std::string > dummyRInfo;
 
  208           bufferRaster1Ptr.reset(
 
  212               bufferRaster1BandsProperties,
 
  217             "Output raster creation error" );
 
  222           bufferRaster2Ptr.reset(
 
  226               bufferRaster2BandsProperties,
 
  231             "Output raster creation error" );
 
  290       std::auto_ptr< te::common::TaskProgress > task;
 
  303       for( 
unsigned int iteration = 0 ; iteration <
 
  319             dstRasterPtr = bufferRaster1Ptr.get();
 
  324           srcRasterPtr = dstRasterPtr;
 
  328         else if( iteration == 1 )
 
  330           srcRasterPtr = dstRasterPtr;
 
  332           dstRasterPtr = bufferRaster2Ptr.get();
 
  336           auxRasterPtr = srcRasterPtr;
 
  337           srcRasterPtr = dstRasterPtr;
 
  341         for( 
unsigned int inRasterBandsIdx = 0 ; inRasterBandsIdx <
 
  344           if( (this->*(filterPointer))( *srcRasterPtr, ( iteration == 0 ) ?
 
  346             inRasterBandsIdx, *dstRasterPtr, inRasterBandsIdx,
 
  366       throw( te::rp::Exception )
 
  377         TR_RP( 
"Invalid filter type" ) );
 
  380         TR_RP( 
"Invalid raster pointer" ) );
 
  383         TR_RP( 
"Invalid raster" ) );
 
  386         TR_RP( 
"Invalid raster bands number" ) );
 
  387       for( 
unsigned int inRasterBandsIdx = 0 ; inRasterBandsIdx <
 
  393           TR_RP( 
"Invalid raster bands" ) );
 
  397         TR_RP( 
"Invalid iterations number" ) );
 
  400         ( ( inputParamsPtr->
m_windowH % 2 ) != 0 ),
 
  401         TR_RP( 
"Invalid mask window height" ) );
 
  404         ( ( inputParamsPtr->
m_windowW % 2 ) != 0 ),
 
  405         TR_RP( 
"Invalid mask window width" ) );
 
  411           TR_RP( 
"Invalid convolution matrix" ) );
 
  414           TR_RP( 
"Invalid convolution matrix" ) );
 
  430       const unsigned int dstBandIdx, 
const bool useProgress )
 
  441       const unsigned int nRows = (
unsigned int)( srcRaster.
getNumberOfRows() );
 
  442       const unsigned int rowsBound = (
unsigned int)( nRows ?
 
  446       const unsigned int colsBound = (
unsigned int)( nCols ?
 
  449       std::auto_ptr< te::common::TaskProgress > task;
 
  463       double dstBandAllowedMin = 0;
 
  464       double dstBandAllowedMax = 0;
 
  468       unsigned int col = 0;
 
  469       double value1diag = 0;
 
  470       double value2diag = 0;
 
  471       double value1adiag = 0;
 
  472       double value2adiag = 0;
 
  474       double adiagDiff = 0;
 
  477       for( 
unsigned int row = 0; row < nRows ; ++row )
 
  479         for( col = 0 ; col < nCols ; ++col )
 
  481           if( ( row < rowsBound ) && ( col < colsBound ) )
 
  483             srcBand.
getValue( col, row, value1diag );
 
  484             if( value1diag == srcNoDataValue )
 
  486               dstBand.
setValue( col, row, dstNoDataValue );
 
  490             srcBand.
getValue( col + 1, row + 1, value2diag );
 
  491             if( value2diag == srcNoDataValue )
 
  493               dstBand.
setValue( col, row, dstNoDataValue );
 
  497             srcBand.
getValue( col, row + 1, value1adiag );
 
  498             if( value1adiag == srcNoDataValue )
 
  500               dstBand.
setValue( col, row, dstNoDataValue );
 
  504             srcBand.
getValue( col + 1, row, value2adiag );
 
  505             if( value2adiag == srcNoDataValue )
 
  507               dstBand.
setValue( col, row, dstNoDataValue );
 
  511             diagDiff = value1diag - value2diag;
 
  512             adiagDiff = value1adiag - value2adiag;
 
  514             outValue = std::sqrt( ( diagDiff * diagDiff ) +
 
  515               ( adiagDiff * adiagDiff ) );
 
  516             outValue = std::max( outValue, dstBandAllowedMin );
 
  517             outValue = std::min( outValue, dstBandAllowedMax );
 
  519             dstBand.
setValue( col, row, outValue );
 
  523             dstBand.
setValue( col, row, dstNoDataValue );
 
  530           if( !task->isActive() ) 
return false;
 
  539       const unsigned int dstBandIdx, 
const bool useProgress )
 
  550       const unsigned int nRows = (
unsigned int)( srcRaster.
getNumberOfRows() );
 
  551       const unsigned int rowsBound = (
unsigned int)( nRows ?
 
  555       const unsigned int colsBound = (
unsigned int)( nCols ?
 
  558       std::auto_ptr< te::common::TaskProgress > task;
 
  572       double dstBandAllowedMin = 0;
 
  573       double dstBandAllowedMax = 0;
 
  577       unsigned int col = 0;
 
  590       for( 
unsigned int row = 0; row < nRows ; ++row )
 
  592         for( col = 0 ; col < nCols ; ++col )
 
  594           if( ( row > 0 ) && ( col > 0 ) && ( row < rowsBound ) &&
 
  595             ( col < colsBound ) )
 
  597             srcBand.
getValue( col - 1, row - 1, value1 );
 
  598             if( value1 == srcNoDataValue )
 
  600               dstBand.
setValue( col, row, dstNoDataValue );
 
  604             srcBand.
getValue( col, row - 1, value2 );
 
  605             if( value2 == srcNoDataValue )
 
  607               dstBand.
setValue( col, row, dstNoDataValue );
 
  611             srcBand.
getValue( col + 1, row - 1, value3 );
 
  612             if( value3 == srcNoDataValue )
 
  614               dstBand.
setValue( col, row, dstNoDataValue );
 
  618             srcBand.
getValue( col - 1, row, value4 );
 
  619             if( value4 == srcNoDataValue )
 
  621               dstBand.
setValue( col, row, dstNoDataValue );
 
  625             srcBand.
getValue( col + 1, row, value5 );
 
  626             if( value5 == srcNoDataValue )
 
  628               dstBand.
setValue( col, row, dstNoDataValue );
 
  632             srcBand.
getValue( col - 1, row + 1, value6 );
 
  633             if( value6 == srcNoDataValue )
 
  635               dstBand.
setValue( col, row, dstNoDataValue );
 
  639             srcBand.
getValue( col, row + 1, value7 );
 
  640             if( value7 == srcNoDataValue )
 
  642               dstBand.
setValue( col, row, dstNoDataValue );
 
  646             srcBand.
getValue( col + 1, row + 1, value8 );
 
  647             if( value8 == srcNoDataValue )
 
  649               dstBand.
setValue( col, row, dstNoDataValue );
 
  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 );
 
  658             outValue = std::sqrt( ( gY * gY ) +
 
  660             outValue = std::max( outValue, dstBandAllowedMin );
 
  661             outValue = std::min( outValue, dstBandAllowedMax );
 
  663             dstBand.
setValue( col, row, outValue );
 
  667             dstBand.
setValue( col, row, dstNoDataValue );
 
  674           if( !task->isActive() ) 
return false;
 
  683       const unsigned int dstBandIdx, 
const bool useProgress )
 
  697       std::auto_ptr< te::common::TaskProgress > task;
 
  704       boost::numeric::ublas::matrix<double> window_mean(H, W);
 
  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);
 
  715       double dstBandAllowedMin = 0;
 
  716       double dstBandAllowedMax = 0;
 
  729         if ((R >= (
unsigned)(H / 2) && R < MR - (H / 2)) &&
 
  730             (C >= (
unsigned)(W / 2) && C < MC - (W / 2)))
 
  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);
 
  740         pixel_mean = std::max( pixel_mean, dstBandAllowedMin );
 
  741         pixel_mean = std::min( pixel_mean, dstBandAllowedMax );
 
  742         dstRaster.
setValue(C, R, pixel_mean, dstBandIdx);
 
  747           if( !task->isActive() ) 
return false;
 
  757       const unsigned int dstBandIdx, 
const bool useProgress )
 
  771       std::auto_ptr< te::common::TaskProgress > task;
 
  783       double dstBandAllowedMin = 0;
 
  784       double dstBandAllowedMax = 0;
 
  788       std::vector<double> pixels_in_window;
 
  789       double pixel_mode = 0;
 
  800         if ((R >= (
unsigned)(H / 2) && R < MR - (H / 2)) &&
 
  801             (C >= (
unsigned)(W / 2) && C < MC - (W / 2)))
 
  803           double pixel_mode = 0.0;
 
  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)
 
  812             pixel_mode = vector_mode[0];
 
  817         pixel_mode = std::max( pixel_mode, dstBandAllowedMin );
 
  818         pixel_mode = std::min( pixel_mode, dstBandAllowedMax );
 
  819         dstRaster.
setValue(C, R, pixel_mode, dstBandIdx);
 
  824           if( !task->isActive() ) 
return false;
 
  835       const unsigned int dstBandIdx, 
const bool useProgress )
 
  849       std::auto_ptr< te::common::TaskProgress > task;
 
  861       double dstBandAllowedMin = 0;
 
  862       double dstBandAllowedMax = 0;
 
  866       std::vector<double> pixels_in_window;
 
  867       double pixel_median = 0;
 
  878         if ((R >= (
unsigned)(H / 2) && R < MR - (H / 2)) &&
 
  879             (C >= (
unsigned)(W / 2) && C < MC - (W / 2)))
 
  882           pixels_in_window.clear();
 
  883           for (
int r = -1 * (
int) (H / 2); r <= (int) (H / 2); r++)
 
  884             for (
int c = -1 * (
int) (W / 2); c <= (int) (W / 2); c++)
 
  885               pixels_in_window.push_back(it.
getValue(c, r));
 
  887           std::sort(pixels_in_window.begin(), pixels_in_window.end(), 
OrderFunction);
 
  888           pixel_median = pixels_in_window[pixels_in_window.size() / 2];
 
  893         pixel_median = std::max( pixel_median, dstBandAllowedMin );
 
  894         pixel_median = std::min( pixel_median, dstBandAllowedMax );
 
  895         dstRaster.
setValue(C, R, pixel_median, dstBandIdx);
 
  900           if( !task->isActive() ) 
return false;
 
  911       const unsigned int dstBandIdx, 
const bool useProgress )
 
  925       std::auto_ptr< te::common::TaskProgress > task;
 
  937       double dstBandAllowedMin = 0;
 
  938       double dstBandAllowedMax = 0;
 
  942       double pixel_dilation = 0;
 
  954         if ((R >= (
unsigned)(H / 2) && R < MR - (H / 2)) &&
 
  955             (C >= (
unsigned)(W / 2) && C < MC - (W / 2)))
 
  957           pixel_dilation = -1.0 * std::numeric_limits<double>::max();
 
  958           for (
int r = -1 * (
int) (H / 2); r <= (int) (H / 2); r++)
 
  959             for (
int c = -1 * (
int) (W / 2); c <= (int) (W / 2); c++)
 
  962               if (pixel > pixel_dilation)
 
  963                 pixel_dilation = pixel;
 
  969         pixel_dilation = std::max( pixel_dilation, dstBandAllowedMin );
 
  970         pixel_dilation = std::min( pixel_dilation, dstBandAllowedMax );
 
  971         dstRaster.
setValue(C, R, pixel_dilation, dstBandIdx);
 
  976           if( !task->isActive() ) 
return false;
 
  987       const unsigned int dstBandIdx, 
const bool useProgress )
 
 1001       std::auto_ptr< te::common::TaskProgress > task;
 
 1013       double dstBandAllowedMin = 0;
 
 1014       double dstBandAllowedMax = 0;
 
 1016         dstBandAllowedMax );
 
 1018       double pixel_erosion = 0;
 
 1030         if ((R >= (
unsigned)(H / 2) && R < MR - (H / 2)) &&
 
 1031             (C >= (
unsigned)(W / 2) && C < MC - (W / 2)))
 
 1033           pixel_erosion = std::numeric_limits<double>::max();
 
 1034           for (
int r = -1 * (
int) (H / 2); r <= (int) (H / 2); r++)
 
 1035             for (
int c = -1 * (
int) (W / 2); c <= (int) (W / 2); c++)
 
 1038               if (pixel < pixel_erosion)
 
 1039                 pixel_erosion = pixel;
 
 1045         pixel_erosion = std::max( pixel_erosion, dstBandAllowedMin );
 
 1046         pixel_erosion = std::min( pixel_erosion, dstBandAllowedMax );
 
 1047         dstRaster.
setValue(C, R, pixel_erosion, dstBandIdx);
 
 1052           if( !task->isActive() ) 
return false;
 
 1063       const unsigned int dstBandIdx, 
const bool useProgress )
 
 1077       std::auto_ptr< te::common::TaskProgress > task;
 
 1088       double pixels_in_window = 0;
 
 1090       double dstBandAllowedMin = 0;
 
 1091       double dstBandAllowedMax = 0;
 
 1093         dstBandAllowedMax );
 
 1103         if ((R >= (
unsigned)(H / 2) && R < MR - (H / 2)) &&
 
 1104             (C >= (
unsigned)(W / 2) && C < MC - (W / 2)))
 
 1106           pixels_in_window = 0.0;
 
 1107           for (
int r = -1 * (
int) (H / 2), rw = 0; r <= (int) (H / 2); r++, rw++)
 
 1108             for (
int c = -1 * (
int) (W / 2), cw = 0; c <= (int) (W / 2); c++, cw++)
 
 1114         pixels_in_window = std::max( pixels_in_window, dstBandAllowedMin );
 
 1115         pixels_in_window = std::min( pixels_in_window, dstBandAllowedMax );
 
 1116         dstRaster.
setValue(C, R, pixels_in_window, dstBandIdx);
 
 1121           if( !task->isActive() ) 
return false;
 
bool m_enableProgress
Enable/Disable the progress interface (default:false). 
 
A series of well-known filtering algorithms for images, linear and non-linear. 
 
#define TERP_DEBUG_TRUE_OR_THROW(value, message)
Checks if value is true and throws an exception if not. 
 
std::auto_ptr< te::rst::Raster > m_outputRasterPtr
A pointer the ge generated output raster (label image). 
 
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. 
 
Filter output parameters. 
 
#define TERP_LOG_AND_RETURN_FALSE(message)
Logs a warning message will and return false. 
 
unsigned int m_windowH
The height of the convolution window. (commonly 3, with W=3 to make a 3x3 window, and so on) ...
 
std::map< std::string, std::string > m_rInfo
The necessary information to create the raster (as described in te::raster::RasterFactory). 
 
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. 
 
int getType() const 
It returns the data type of the elements in the band. 
 
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. 
 
unsigned int getNumberOfRows() const 
Returns the raster number of rows. 
 
bool initialize(const AlgorithmInputParameters &inputParams)
Initialize the algorithm instance making it ready for execution. 
 
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. 
 
Raster Processing functions. 
 
const InputParameters & operator=(const InputParameters ¶ms)
 
AbstractParameters * clone() const 
Create a clone copy of this instance. 
 
Grid * getGrid()
It returns the raster grid. 
 
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. 
 
bool m_isInitialized
Is this instance already initialized? 
 
static Raster * make()
It creates and returns an empty raster with default raster driver. 
 
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. 
 
The resultant pixel will be the mode of pixels in the convolution window. When the window is multimod...
 
virtual void getValue(unsigned int c, unsigned int r, double &value) const =0
Returns the cell attribute value. 
 
void reset()
Clear all internal allocated objects and reset the algorithm to its initial state. 
 
The resultant pixel will be the median of pixels in the convolution window. 
 
virtual std::size_t getNumberOfBands() const =0
Returns the number of bands (dimension of cells attribute values) in the raster. 
 
The user will define the weights of a convolution window. 
 
#define TR_RP(message)
It marks a string in order to get translated. This is a special mark used in the Raster Processing mo...
 
unsigned int getColumn() const 
Returns the current column in iterator. 
 
unsigned int m_iterationsNumber
The number of iterations to perform (default:1). 
 
#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...
 
bool execute(AlgorithmOutputParameters &outputParams)
Executes the algorithm using the supplied parameters. 
 
Abstract parameters base interface. 
 
A raster band description. 
 
A rectified grid is the spatial support for raster data. 
 
virtual void setValue(unsigned int c, unsigned int r, const double value)=0
Sets the cell attribute value. 
 
T getValue() const 
Returns the value in current position (column, row) from iterator. 
 
std::vector< unsigned int > m_inRasterBands
Bands to be used from the input raster 1. 
 
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. 
 
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. 
 
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. 
 
The resultant pixel will be the mean of pixels in the convolution window. 
 
const OutputParameters & operator=(const OutputParameters ¶ms)
 
unsigned int getNumberOfColumns() const 
Returns the raster number of columns. 
 
const Algorithm & operator=(const Algorithm &)
 
AbstractParameters * clone() const 
Create a clone copy of this instance. 
 
bool isInitialized() const 
Returns true if the algorithm instance is initialized and ready for execution. 
 
boost::numeric::ublas::matrix< double > m_window
User defined convolution window. (The size must be equal to m_windowH x m_windowW) ...
 
A raster band description. 
 
double m_noDataValue
Value to indicate elements where there is no data, default is std::numeric_limits<double>::max(). 
 
It implements and iterator to "navigate" over a single band, optimized by a window structure (e...
 
unsigned int m_windowW
The width of the convolution window. 
 
#define TERP_TRUE_OR_THROW(value, message)
Checks if value is true and throws an exception if not. 
 
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. 
 
Filter::InputParameters m_inputParameters
Input parameters. 
 
void 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)...
 
std::string m_rType
Output raster data source type (as described in te::raster::RasterFactory ). 
 
The resultant pixel will be the lowest pixel value in the convolution window. 
 
Raster Processing algorithm output parameters base interface. 
 
te::rst::Raster const * m_inRasterPtr
Input raster. 
 
An abstract class for raster data strucutures. 
 
BandProperty * getProperty()
Returns the band property. 
 
virtual const Band * getBand(std::size_t i) const =0
Returns the raster i-th band. 
 
This class can be used to inform the progress of a task. 
 
#define TERP_LOG_AND_THROW(message)
Logs a error message and throws. 
 
void reset()
Clear all internal allocated resources and reset the parameters instance to its initial state...
 
TESTATEXPORT std::vector< double > Mode(const std::vector< double > &values)
 
static bool OrderFunction(double i, double j)
Returns true if i < j. 
 
unsigned int getRow() const 
Returns the current row in iterator. 
 
FilterType m_filterType
The edge filter type. 
 
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. 
 
The resultant pixel will be the highest pixel value in the convolution window. 
 
te::common::AccessPolicy getAccessPolicy() const 
Returns the raster access policy. 
 
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. 
 
Raster Processing algorithm input parameters base interface. 
 
void reset()
Clear all internal allocated resources and reset the parameters instance to its initial state...