27 #include "../common/progress/TaskProgress.h" 28 #include "../geometry.h" 29 #include "../raster/Band.h" 30 #include "../raster/BandIterator.h" 31 #include "../raster/Grid.h" 32 #include "../raster/PositionIterator.h" 33 #include "../raster/Raster.h" 34 #include "../raster/Utils.h" 35 #include "../statistics.h" 70 std::vector<std::complex<double> > values;
72 for (
unsigned int i = 0; i < dvalues.size(); i++)
73 values.push_back(dvalues[i]);
82 std::vector<double> values;
93 if((*it)[band] != noDataValue)
94 values.push_back((*it)[band]);
114 if((*it)[band] != noDataValue)
115 ++values[(*it)[
band]];
122 unsigned int rowstep,
unsigned int colstep)
124 assert(bands.size() > 0);
130 std::vector<std::vector<std::complex<double> > > allvalues;
131 std::complex<double> value;
139 for (
unsigned int i = 0; i < bands.size(); i++)
140 allvalues.push_back(std::vector<std::complex< double > > ());
144 for (
unsigned int i = 0; i < bands.size(); i++)
147 allvalues[i].push_back(value);
172 unsigned int rowstep,
unsigned int colstep)
174 assert(bands.size() > 0);
180 std::vector<std::vector<double> > allvalues;
187 unsigned int initrow = it.
getRow(), initcol;
189 for (
unsigned int i = 0; i < bands.size(); i++)
190 allvalues.push_back(std::vector<double> ());
193 if(!((it.
getRow() - initrow) % rowstep))
195 for (
unsigned int i = 0; i < bands.size(); i++)
201 if(noDataValue != value)
202 allvalues[i].push_back(value);
206 unsigned posstep = it.
getColumn() + colstep;
210 for(; initcol < posstep; initcol++)
219 assert(pixels.size() > 0);
234 std::vector<double> values;
235 std::map<double, int> modeMap;
239 double delta_n = 0.0;
240 double delta_n2 = 0.0;
247 while (it != itEnd) {
248 double value = (*it)[
band];
253 values.push_back(value);
254 summary.
m_sum += value;
259 delta = value - mean;
261 delta_n2 = delta_n * delta_n;
265 M3 += term1 * delta_n * (summary.
m_validCount - 2) - 3 * delta_n * M2;
310 std::sort(values.begin(), values.end());
321 std::map<double, int>::iterator itMode = modeMap.begin();
324 while (itMode != modeMap.end()) {
325 if (repeat < itMode->second) {
326 repeat = itMode->second;
328 summary.
m_mode.push_back(itMode->first);
330 else if (repeat == itMode->second) {
331 summary.
m_mode.push_back(itMode->first);
341 summary.
m_mean = noDataValue;
344 summary.
m_mode.push_back(noDataValue);
347 summary.
m_sum = noDataValue;
355 assert(pixels.size() > 0);
365 for (
unsigned int i = 0; i < vpixels.size(); i++)
366 assert(vpixels[i].size() > 0);
367 for (
unsigned int i = 1; i < vpixels.size(); i++)
368 assert(vpixels[0].size() == vpixels[i].size());
369 assert(vpixels.size() == vmeans.size());
374 unsigned int nbands =
static_cast<unsigned int>(vpixels.size());
375 unsigned int nvalues =
static_cast<unsigned int>(vpixels[0].size());
377 boost::numeric::ublas::matrix<std::complex < double > > covariance(nbands, nbands);
382 for (i = 0; i < nbands; i++)
384 for (j = 0; j < nbands; j++)
385 covariance(i, j) = 0.0;
386 covariance(i, i) = 1000.0;
393 std::unique_ptr<te::common::TaskProgress> taskProgress;
397 std::complex<double> sum;
398 for (i = 0; i < nbands; i++)
399 for (j = 0; j < nbands; j++)
402 taskProgress->pulse();
404 sum = std::complex<double> (0.0, 0.0);
406 for (k = 0; k < nvalues; k++)
407 sum += (vpixels[i][k] - vmeans[i]) * (vpixels[j][k] - vmeans[j]);
409 covariance(i, j) = sum / (nvalues - 1.0);
418 for (
unsigned int i = 0; i < vpixels.size(); i++)
419 assert(vpixels[i].size() > 0);
420 for (
unsigned int i = 1; i < vpixels.size(); i++)
421 assert(vpixels[0].size() == vpixels[i].size());
422 assert(vpixels.size() == vmeans.size());
427 const unsigned int nbands =
static_cast<unsigned int>(vpixels.size());
428 const unsigned int nvalues =
static_cast<unsigned int>(vpixels[0].size());
435 boost::numeric::ublas::matrix<double> covariance(nbands, nbands);
439 std::unique_ptr<te::common::TaskProgress> taskProgress;
445 for (i = 0; i < nbands; i++)
447 for (j = 0; j < nbands; j++)
450 taskProgress->pulse();
454 for (k = 0; k < nvalues; k++)
455 sum += (vpixels[i][k] - vmeans[i]) * (vpixels[j][k] - vmeans[j]);
457 covariance(i, j) = sum / ( (double)(nvalues - 1) );
465 double minPixel,
double maxPixel,
double gLevels)
469 if (maxPixel == 0 && minPixel == 0) {
473 double minValueNormalization, maxValueNormalization, matrixLimit;
474 bool normalize =
false;
476 if ((maxPixel - minPixel) > (gLevels - 1)) {
477 minValueNormalization = 0;
478 maxValueNormalization = gLevels - 1;
479 matrixLimit = gLevels;
482 matrixLimit = (maxPixel - minPixel) + 1;
486 boost::numeric::ublas::matrix<double> glcm(static_cast<unsigned int>(matrixLimit), static_cast<unsigned int>(matrixLimit));
489 double neighborPixel;
496 int column_start = 0;
510 std::unique_ptr<te::common::TaskProgress> taskProgress;
514 for (
int r = row_start; r < row_end; r++)
516 for (
int c = column_start; c < column_end; c++)
519 taskProgress->pulse();
523 if (pixel == noDataValue)
526 double auxPixel = pixel;
529 pixel = std::round((((maxValueNormalization - minValueNormalization) / (maxPixel - minPixel)) * (auxPixel - minPixel)) + minValueNormalization);
532 else if (pixel > maxValueNormalization)
533 pixel = maxValueNormalization;
537 rin.
getValue(c + dx, r + dy, neighborPixel, band);
538 if (neighborPixel == noDataValue)
541 double auxNeighbor = neighborPixel;
544 neighborPixel = std::round((((maxValueNormalization - minValueNormalization) / (maxPixel - minPixel)) * (auxNeighbor - minPixel)) + minValueNormalization);
545 if (neighborPixel < 0)
547 else if (neighborPixel > maxValueNormalization)
548 neighborPixel = maxValueNormalization;
552 glcm(static_cast<unsigned int>(pixel), static_cast<unsigned int>(neighborPixel)) = glcm(static_cast<unsigned int>(pixel), static_cast<unsigned int>(neighborPixel))+1;
559 for (
unsigned int i = 0; i < glcm.size1(); i++)
560 for (
unsigned int j = 0; j < glcm.size2(); j++)
561 glcm(i, j) = glcm(i, j) / N;
568 const te::gm::Polygon& polygon,
double minPixel,
double maxPixel,
double gLevels)
570 if (maxPixel == 0 && minPixel == 0) {
575 std::map<std::string, std::string> rcropinfo;
576 rcropinfo[
"FORCE_MEM_DRIVER"] =
"TRUE";
581 return getGLCM(*rcrop.get(),
band,
dx, dy, minPixel, maxPixel, gLevels);
583 return boost::numeric::ublas::matrix<double>();
595 double square_di_minus_dj;
597 if (glcm.size1() == 0 && glcm.size2() == 0) {
606 std::unique_ptr<te::common::TaskProgress> taskProgress;
609 for (i = 0, di = 0.0; i < glcm.size1(); i++, di++)
611 for (j = 0, dj = 0.0; j < glcm.size2(); j++, dj++)
614 taskProgress->pulse();
616 di_minus_dj = (di - dj);
617 square_di_minus_dj = di_minus_dj * di_minus_dj;
619 metrics.
m_contrast += glcm(i, j) * square_di_minus_dj;
621 metrics.
m_energy += glcm(i, j) * glcm(i, j);
623 metrics.
m_entropy += glcm(i, j) * (-1.0 * log(glcm(i, j)));
624 metrics.
m_homogeneity += glcm(i, j) / (1 + square_di_minus_dj);
TERASTEREXPORT te::rst::RasterPtr CropRaster(const te::rst::Raster &rin, const te::gm::Polygon &pin, const std::map< std::string, std::string > &rinfo, const std::string &rType=std::string("GDAL"))
Creates a raster crop using a polygon delimiter.
std::vector< std::vector< std::complex< double > > > getComplexValuesFromRaster(const te::rst::Raster &raster, const te::gm::Polygon &polygon, std::vector< unsigned int > bands, unsigned int rowstep=1, unsigned int colstep=1)
Returns the pixel values (real and imag) for all the bands in raster, inside the polygon.
A structure to hold the set of statistics from a set of numerical values.
boost::numeric::ublas::matrix< std::complex< double > > getComplexCovarianceMatrix(const std::vector< std::vector< std::complex< double > > > &vpixels, const std::vector< std::complex< double > > &vmeans)
Returns the covariance matrix between vectors of pixel values.
boost::numeric::ublas::matrix< double > getCovarianceMatrix(const std::vector< std::vector< double > > &vpixels, const std::vector< double > &vmeans)
Returns the covariance matrix between vectors of pixel values.
static te::dt::Date dx(2010, 12, 31)
unsigned int getNumberOfColumns() const
Returns the raster number of columns.
This class implements the strategy to iterate with spatial restriction, the iteration occurs inside a...
Extraction of attributes from Raster, Bands, and Polygons.
This class can be used to inform the progress of a task.
Raster Processing algorithm output parameters base interface.
std::vector< double > m_mode
bool initialize(const AlgorithmInputParameters &inputParams) _NOEXCEPT_OP(false)
Initialize the algorithm instance making it ready for execution.
unsigned int getRow() const
Returns the current row in iterator.
double m_noDataValue
Value to indicate elements where there is no data, default is std::numeric_limits<double>::max().
#define TE_TR(message)
It marks a string in order to get translated.
double m_energy
GLCM metric Energy (the square root of Angular Second Moment) ${{i,j=0}^{N-1}P_{i,j}^2}$.
bool m_enableTaskProgress
bool isInitialized() const
Returns true if the algorithm instance is initialized and ready for execution.
double m_homogeneity
GLCM metric Homogeneity (also called Inverse Difference Moment) ${i,j=0}^{N-1}{P_{i,j}}{1+(i-j)^2}$.
boost::shared_ptr< Raster > RasterPtr
static PolygonIterator end(const te::rst::Raster *r, const te::gm::Polygon *p)
Returns an iterator referring to after the end of the iterator.
boost::numeric::ublas::matrix< double > getGLCM(const te::rst::Raster &rin, unsigned int band, int dx, int dy, double minPixel, double maxPixel, double gLevels=256)
Computes the Gray-Level Co-occurrence Matrix (GLCM) from a raster band.
te::stat::NumericStatisticalComplexSummary getComplexStatistics(std::vector< std::complex< double > > &pixels)
Returns several statistics from a set of pixels (real and imag).
An abstract class for raster data strucutures.
unsigned int getNumberOfRows() const
Returns the raster number of rows.
virtual std::size_t getNumberOfBands() const =0
Returns the number of bands (dimension of cells attribute values) in the raster.
std::vector< std::complex< double > > getComplexValuesFromBand(const te::rst::Raster &raster, unsigned int band, const te::gm::Polygon &polygon)
Returns the pixel values (real and imag) for the band, inside the polygon.
BandProperty * getProperty()
Returns the band property.
static te::dt::Date di(2010, 06, 10)
double m_dissimilarity
GLCM metric Dissimilarity ${i,j=0}^{N-1}P_{i,j}|i-j|$.
std::vector< std::vector< double > > getValuesFromRaster(const te::rst::Raster &raster, const te::gm::Polygon &polygon, std::vector< unsigned int > bands, unsigned int rowstep=1, unsigned int colstep=1)
Returns the pixel values for all the bands in raster, inside the polygon.
TESTATEXPORT void GetNumericStatisticalSummary(std::vector< double > &values, te::stat::NumericStatisticalSummary &ss, double nullValue)
virtual const Band * getBand(std::size_t i) const =0
Returns the raster i-th band.
This class is designed to declare objects to be thrown as exceptions by TerraLib. ...
te::stat::NumericStatisticalSummary getStatistics(std::vector< double > &pixels)
Returns several statistics from a set of pixels.
te::rp::Texture getGLCMMetrics(boost::numeric::ublas::matrix< double > glcm, double noDataValue=0.0)
Compute texture metrics from GLCM matrix.
virtual void getValue(unsigned int c, unsigned int r, double &value, std::size_t b=0) const
Returns the attribute value of a band of a cell.
static PolygonIterator begin(const te::rst::Raster *r, const te::gm::Polygon *p, const IterationType iterationType)
Returns an iterator referring to the first value of the band.
double m_entropy
GLCM metric Entropy ${i,j=0}^{N-1}P_{i,j}(-{P_{i,j}})$.
RasterAttributes(bool enableTaskProgress=true)
Public constructor.
double m_contrast
GLCM metric Contrast (also called Sum of Squares Variance) ${i,j=0}^{N-1}P_{i,j}(i-j)^2$.
std::vector< double > getValuesFromBand(const te::rst::Raster &raster, unsigned int band, const te::gm::Polygon &polygon)
Returns the pixel values for the band, inside the polygon.
Polygon is a subclass of CurvePolygon whose rings are defined by linear rings.
A structure to hold the set of GLCM metrics.
TESTATEXPORT void GetNumericComplexStatisticalSummary(std::vector< std::complex< double > > &values, te::stat::NumericStatisticalComplexSummary &ss)
void reset() _NOEXCEPT_OP(false)
Clear all internal allocated objects and reset the algorithm to its initial state.
bool execute(AlgorithmOutputParameters &outputParams) _NOEXCEPT_OP(false)
Executes the algorithm using the supplied parameters.
virtual void reset() _NOEXCEPT_OP(false)
Clear all internal allocated objects and reset the algorithm to its initial state.
unsigned int getColumn() const
Returns the current column in iterator.
void getStatisticsFromPolygon(const te::rst::Raster &raster, unsigned int band, const te::gm::Polygon &polygon, te::stat::NumericStatisticalSummary &summary, double noDataValue=0.0)
Computes several statistics from a set of pixels inside a polygon.
TESTATEXPORT void Mode(const std::vector< double > &values, te::stat::NumericStatisticalSummary &ss)