TsTexture.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/unittest/rp/texture/TsTexture.cpp
22 
23  \brief A test suit for Texture interface.
24 */
25 
26 // TerraLib
27 #include "../Config.h"
28 #include <terralib/rp.h>
29 #include <terralib/raster.h>
30 
31 // Boost
32 #define BOOST_TEST_NO_MAIN
33 #include <boost/test/unit_test.hpp>
34 #include <boost/shared_ptr.hpp>
35 
36 BOOST_AUTO_TEST_SUITE (texture_tests)
37 
38 BOOST_AUTO_TEST_CASE(GLCM_test)
39 {
40  /* First open the input image */
41 
42  std::map<std::string, std::string> rinfo;
43  rinfo["URI"] = TERRALIB_DATA_DIR"/geotiff/cbers2b_rgb342_crop.tif";
45 
46  // Retrieve the minimum and maximum values of the band to normalize GLCM
47  double maxPixel, minPixel;
48  te::rst::GetDataTypeRanges(rin->getBandDataType(1), minPixel, maxPixel);
49  if ((maxPixel - minPixel) > 255) {
50  maxPixel = rin->getBand(1)->getMaxValue(true).real();
51  minPixel = rin->getBand(1)->getMinValue(true).real();
52  }
53 
54  /* Use raster attributes to compute GLCM matrix, in northeast direction */
55 
56  te::rp::RasterAttributes rattributes;
57  boost::numeric::ublas::matrix<double> glcm = rattributes.getGLCM(*rin, 1, 1, -1, minPixel, maxPixel);
58  te::rp::Texture metrics = rattributes.getGLCMMetrics(glcm);
59 
60  /* Compare texture metrics */
61 
62  double myEpsilon = 1e-4;
63  BOOST_CHECK(std::abs(metrics.m_contrast - 72.554) < myEpsilon);
64  BOOST_CHECK(std::abs(metrics.m_dissimilarity - 5.56127) < myEpsilon);
65  BOOST_CHECK(std::abs(metrics.m_energy - 0.0806309) < myEpsilon);
66  BOOST_CHECK(std::abs(metrics.m_entropy - 6.00814) < myEpsilon);
67  BOOST_CHECK(std::abs(metrics.m_homogeneity - 0.286813) < myEpsilon);
68 
69  /* Clean up */
70  delete rin;
71 }
72 
73 BOOST_AUTO_TEST_SUITE_END()
BOOST_AUTO_TEST_SUITE(texture_tests) BOOST_AUTO_TEST_CASE(GLCM_test)
Definition: TsTexture.cpp:36
TERASTEREXPORT void GetDataTypeRanges(const int &dataType, double &min, double &max)
Return the values range of a given data type.
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
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.
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.
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
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.
BOOST_AUTO_TEST_CASE(encoding_test_utf8_latin1)
A structure to hold the set of GLCM metrics.
Definition: Texture.h:44
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.