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 TE_TR(
"Invalid filter type" ) );
380 TE_TR(
"Invalid raster pointer" ) );
383 TE_TR(
"Invalid raster" ) );
386 TE_TR(
"Invalid raster bands number" ) );
387 for(
unsigned int inRasterBandsIdx = 0 ; inRasterBandsIdx <
393 TE_TR(
"Invalid raster bands" ) );
397 TE_TR(
"Invalid iterations number" ) );
400 ( ( inputParamsPtr->
m_windowH % 2 ) != 0 ),
401 TE_TR(
"Invalid mask window height" ) );
404 ( ( inputParamsPtr->
m_windowW % 2 ) != 0 ),
405 TE_TR(
"Invalid mask window width" ) );
411 TE_TR(
"Invalid convolution matrix" ) );
414 TE_TR(
"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;
801 if ((R >= (
unsigned)(H / 2) && R < MR - (H / 2)) &&
802 (C >= (
unsigned)(W / 2) && C < MC - (W / 2)))
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];
813 pixel_mode = std::max( pixel_mode, dstBandAllowedMin );
814 pixel_mode = std::min( pixel_mode, dstBandAllowedMax );
815 dstRaster.
setValue(C, R, pixel_mode, dstBandIdx);
820 if( !task->isActive() )
return false;
831 const unsigned int dstBandIdx,
const bool useProgress )
845 std::auto_ptr< te::common::TaskProgress > task;
857 double dstBandAllowedMin = 0;
858 double dstBandAllowedMax = 0;
862 std::vector<double> pixels_in_window;
863 double pixel_median = 0.0;
874 if ((R >= (
unsigned)(H / 2) && R < MR - (H / 2)) &&
875 (C >= (
unsigned)(W / 2) && C < MC - (W / 2)))
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));
883 std::sort(pixels_in_window.begin(), pixels_in_window.end(),
OrderFunction);
884 pixel_median = pixels_in_window[pixels_in_window.size() / 2];
889 pixel_median = std::max( pixel_median, dstBandAllowedMin );
890 pixel_median = std::min( pixel_median, dstBandAllowedMax );
891 dstRaster.
setValue(C, R, pixel_median, dstBandIdx);
896 if( !task->isActive() )
return false;
907 const unsigned int dstBandIdx,
const bool useProgress )
921 std::auto_ptr< te::common::TaskProgress > task;
933 double dstBandAllowedMin = 0;
934 double dstBandAllowedMax = 0;
938 double pixel_dilation = 0;
950 if ((R >= (
unsigned)(H / 2) && R < MR - (H / 2)) &&
951 (C >= (
unsigned)(W / 2) && C < MC - (W / 2)))
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++)
958 if (pixel > pixel_dilation)
959 pixel_dilation = pixel;
965 pixel_dilation = std::max( pixel_dilation, dstBandAllowedMin );
966 pixel_dilation = std::min( pixel_dilation, dstBandAllowedMax );
967 dstRaster.
setValue(C, R, pixel_dilation, dstBandIdx);
972 if( !task->isActive() )
return false;
983 const unsigned int dstBandIdx,
const bool useProgress )
997 std::auto_ptr< te::common::TaskProgress > task;
1009 double dstBandAllowedMin = 0;
1010 double dstBandAllowedMax = 0;
1012 dstBandAllowedMax );
1014 double pixel_erosion = 0;
1026 if ((R >= (
unsigned)(H / 2) && R < MR - (H / 2)) &&
1027 (C >= (
unsigned)(W / 2) && C < MC - (W / 2)))
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++)
1034 if (pixel < pixel_erosion)
1035 pixel_erosion = pixel;
1041 pixel_erosion = std::max( pixel_erosion, dstBandAllowedMin );
1042 pixel_erosion = std::min( pixel_erosion, dstBandAllowedMax );
1043 dstRaster.
setValue(C, R, pixel_erosion, dstBandIdx);
1048 if( !task->isActive() )
return false;
1059 const unsigned int dstBandIdx,
const bool useProgress )
1073 std::auto_ptr< te::common::TaskProgress > task;
1084 double pixels_in_window = 0;
1086 double dstBandAllowedMin = 0;
1087 double dstBandAllowedMax = 0;
1089 dstBandAllowedMax );
1099 if ((R >= (
unsigned)(H / 2) && R < MR - (H / 2)) &&
1100 (C >= (
unsigned)(W / 2) && C < MC - (W / 2)))
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++)
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);
1117 if( !task->isActive() )
return false;
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.
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.
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.
virtual void getValue(unsigned int c, unsigned int r, double &value) const =0
Returns the cell attribute value.
TESTATEXPORT std::vector< double > Mode(const std::vector< double > &values)
A raster band description.
unsigned int getNumberOfColumns() const
Returns the raster number of columns.
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.
static bool OrderFunction(double i, double j)
Returns true if i < j.
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)...
This class can be used to inform the progress of a task.
Filter::InputParameters m_inputParameters
Input parameters.
Raster Processing algorithm output parameters base interface.
std::string m_rType
Output raster data source type (as described in te::raster::RasterFactory ).
double m_noDataValue
Value to indicate elements where there is no data, default is std::numeric_limits::max().
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.
#define TE_TR(message)
It marks a string in order to get translated.
unsigned int getRow() const
Returns the current row in iterator.
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...
const Algorithm & operator=(const Algorithm &)
te::common::AccessPolicy getAccessPolicy() const
Returns the raster access policy.
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.
An abstract class for raster data strucutures.
unsigned int getNumberOfRows() const
Returns the raster number of rows.
#define TERP_LOG_AND_THROW(message)
Logs a error message and throws.
BandProperty * getProperty()
Returns the band property.
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.
bool m_isInitialized
Is this instance already initialized?
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 isInitialized() const
Returns true if the algorithm instance is initialized and ready for execution.
A raster band description.
Grid * getGrid()
It returns the raster grid.
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.
virtual void setValue(unsigned int c, unsigned int r, const double value)=0
Sets the cell attribute value.
Abstract parameters base interface.
#define TERP_LOG_AND_RETURN_FALSE(message)
Logs a warning message will and return false.
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.
static Raster * make()
It creates and returns an empty raster with default raster driver.
std::auto_ptr< te::rst::Raster > m_outputRasterPtr
A pointer the ge generated output raster (label image).
int getType() const
It returns the data type of the elements in the band.
void reset()
Clear all internal allocated resources and reset the parameters instance to its initial state...
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.
const OutputParameters & operator=(const OutputParameters ¶ms)
#define TERP_DEBUG_TRUE_OR_THROW(value, message)
Checks if value is true and throws an exception if not.
Filter output parameters.
void reset()
Clear all internal allocated objects and reset the algorithm to its initial state.
A rectified grid is the spatial support for raster data.
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.
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.
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.
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 execute(AlgorithmOutputParameters &outputParams)
Executes the algorithm using the supplied parameters.