examples/rp/Texture.cpp
Go to the documentation of this file.
1 #include "RPExamples.h"
2 
3 // TerraLib
4 #include <terralib/geometry.h>
5 #include <terralib/raster.h>
6 #include <terralib/rp.h>
7 
8 #include <boost/numeric/ublas/matrix.hpp>
9 
10 void Texture()
11 {
12  try
13  {
14  std::cout << "Texture example using cooccurrence matrix." << std::endl << std::endl;
15 
16 // first open the input image
17  std::map<std::string, std::string> rinfo;
18  rinfo["URI"] = TERRALIB_DATA_DIR"/geotiff/cbers2b_rgb342_crop.tif";
20 
21  {
22  // Retrieve the minimum and maximum values of the band to normalize GLCM
23  double maxPixel, minPixel;
24  te::rst::GetDataTypeRanges(rin->getBandDataType(1), minPixel, maxPixel);
25  if ((maxPixel - minPixel) > 255) {
26  maxPixel = rin->getBand(1)->getMaxValue(true).real();
27  minPixel = rin->getBand(1)->getMinValue(true).real();
28  }
29 
30 // use raster attributes to compute GLCM matrix, in northeast direction
31  te::rp::RasterAttributes rattributes;
32  boost::numeric::ublas::matrix<double> glcm_b1 = rattributes.getGLCM(*rin, 1, 1, -1, minPixel, maxPixel);
33  te::rp::Texture metrics = rattributes.getGLCMMetrics(glcm_b1);
34 
35 // display texture metrics
36  std::cout << "GLCM metrics of full image, band 1" << std::endl;
37  std::cout << " contrast: " << metrics.m_contrast << std::endl;
38  std::cout << " dissimilarity: " << metrics.m_dissimilarity << std::endl;
39  std::cout << " energy: " << metrics.m_energy << std::endl;
40  std::cout << " entropy: " << metrics.m_entropy << std::endl;
41  std::cout << " homogeneity: " << metrics.m_homogeneity << std::endl;
42  }
43 
44 // create a polygon inside raster to obtain GLCM metrics
45  double xc = (rin->getExtent()->getUpperRightX() + rin->getExtent()->getLowerLeftX()) / 2;
46  double yc = (rin->getExtent()->getUpperRightY() + rin->getExtent()->getLowerLeftY()) / 2;
48  lr->setPoint(0, rin->getExtent()->getLowerLeftX(), rin->getExtent()->getLowerLeftY()); // lower left
49  lr->setPoint(1, (rin->getExtent()->getLowerLeftX() + xc) / 2, (rin->getExtent()->getLowerLeftY() + yc) / 2);
50  lr->setPoint(2, rin->getExtent()->getLowerLeftX(), yc); // upper left
51  lr->setPoint(3, xc, yc); // upper rigth
52  lr->setPoint(4, xc, rin->getExtent()->getLowerLeftY()); // lower rigth
53  lr->setPoint(5, rin->getExtent()->getLowerLeftX(), rin->getExtent()->getLowerLeftY()); // closing
54 
56  polygon->push_back(lr);
57 
58  {
59  // Retrieve the minimum and maximum values of the band to normalize GLCM
60  double maxPixel, minPixel;
61  te::rst::GetDataTypeRanges(rin->getBandDataType(1), minPixel, maxPixel);
62  if ((maxPixel - minPixel) > 255) {
63  maxPixel = rin->getBand(1)->getMaxValue(true).real();
64  minPixel = rin->getBand(1)->getMinValue(true).real();
65  }
66 
67 // use raster attributes to compute GLCM matrix from band 2, in southeast direction
68  te::rp::RasterAttributes rattributes;
69  boost::numeric::ublas::matrix<double> glcm_b2 = rattributes.getGLCM(*rin, 2, 1, 1, *polygon, minPixel, maxPixel);
70  te::rp::Texture metrics = rattributes.getGLCMMetrics(glcm_b2);
71 
72 // display texture metrics
73  std::cout << "GLCM metrics of image crop image, band 2" << std::endl;
74  std::cout << " contrast: " << metrics.m_contrast << std::endl;
75  std::cout << " dissimilarity: " << metrics.m_dissimilarity << std::endl;
76  std::cout << " energy: " << metrics.m_energy << std::endl;
77  std::cout << " entropy: " << metrics.m_entropy << std::endl;
78  std::cout << " homogeneity: " << metrics.m_homogeneity << std::endl;
79  }
80 // clean up
81  delete rin;
82 
83  std::cout << "Done!" << std::endl << std::endl;
84  }
85  catch(const std::exception& e)
86  {
87  std::cout << std::endl << "An exception has occurred in Texture(): " << e.what() << std::endl;
88  }
89  catch(...)
90  {
91  std::cout << std::endl << "An unexpected exception has occurred in Texture()!" << std::endl;
92  }
93 }
94 
te::gm::Envelope * getExtent()
Returns the geographic extension of the raster data.
void push_back(Curve *ring)
It adds the curve to the curve polygon.
TERASTEREXPORT void GetDataTypeRanges(const int &dataType, double &min, double &max)
Return the values range of a given data type.
These routines show how to use the RP (raster processing) module.
const double & getUpperRightX() const
It returns a constant refernce to the x coordinate of the upper right corner.
const double & getLowerLeftY() const
It returns a constant refernce to the y coordinate of the lower left corner.
const double & getUpperRightY() const
It returns a constant refernce to the x coordinate of the upper right corner.
double m_energy
GLCM metric Energy (the square root of Angular Second Moment) ${{i,j=0}^{N-1}P_{i,j}^2}$.
Definition: Texture.h:77
Extraction of attributes from Raster, Bands, and Polygons.
double m_homogeneity
GLCM metric Homogeneity (also called Inverse Difference Moment) ${i,j=0}^{N-1}{P_{i,j}}{1+(i-j)^2}$.
Definition: Texture.h:79
A LinearRing is a LineString that is both closed and simple.
Definition: LinearRing.h:53
virtual std::complex< double > getMaxValue(bool readall=false, unsigned int rs=0, unsigned int cs=0, unsigned int rf=0, unsigned int cf=0) const
It computes and returns the maximum occurring value in a window of the band.
void setPoint(std::size_t i, const double &x, const double &y)
It sets the value of the specified point.
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.
An abstract class for raster data strucutures.
double m_dissimilarity
GLCM metric Dissimilarity ${i,j=0}^{N-1}P_{i,j}|i-j|$.
Definition: Texture.h:76
virtual int getBandDataType(std::size_t i) const =0
Returns the data type in a particular band (or dimension).
This file contains include headers for the TerraLib Raster Processing module.
virtual const Band * getBand(std::size_t i) const =0
Returns the raster i-th band.
void Texture()
te::rp::Texture getGLCMMetrics(boost::numeric::ublas::matrix< double > glcm, double noDataValue=0.0)
Compute texture metrics from GLCM matrix.
double m_entropy
GLCM metric Entropy ${i,j=0}^{N-1}P_{i,j}(-{P_{i,j}})$.
Definition: Texture.h:78
double m_contrast
GLCM metric Contrast (also called Sum of Squares Variance) ${i,j=0}^{N-1}P_{i,j}(i-j)^2$.
Definition: Texture.h:75
Polygon is a subclass of CurvePolygon whose rings are defined by linear rings.
Definition: Polygon.h:50
virtual std::complex< double > getMinValue(bool readall=false, unsigned int rs=0, unsigned int cs=0, unsigned int rf=0, unsigned int cf=0) const
It computes and returns the minimum occurring value in a window of the band.
const double & getLowerLeftX() const
It returns a constant reference to the x coordinate of the lower left corner.
A structure to hold the set of GLCM metrics.
Definition: Texture.h:44
This file contains include headers for the Vector Geometry model of TerraLib.
static Raster * open(const std::map< std::string, std::string > &rinfo, te::common::AccessPolicy p=te::common::RAccess)
It opens a raster with the given parameters and default raster driver.