27 #include "../common/MatrixUtils.h"
28 #include "../common/progress/TaskProgress.h"
29 #include "../geometry/Coord2D.h"
30 #include "../geometry/Envelope.h"
31 #include "../geometry/Point.h"
32 #include "../geometry/Polygon.h"
33 #include "../raster/Grid.h"
34 #include "../raster/PositionIterator.h"
35 #include "../raster/Utils.h"
48 #include <boost/numeric/ublas/io.hpp>
49 #include <boost/numeric/ublas/lu.hpp>
50 #include <boost/numeric/ublas/matrix.hpp>
80 m_acceptanceThreshold = 0;
102 m_covarianceMatrix(cm)
111 m_myCluster(rhs.m_myCluster),
129 m_meanVector.clear();
131 m_covarianceMatrix.clear();
133 m_covarianceInversion.clear();
140 assert(m_myCluster == 0);
143 for (
unsigned i = 0; i < m_meanVector.size(); i++)
165 unsigned int nBands = m_meanVector.size();
167 boost::numeric::ublas::matrix<double> term1(1, nBands);
168 boost::numeric::ublas::matrix<double> term2(nBands, 1);
170 for (
unsigned int i = 0; i < nBands; i++)
172 term1(0, i) = m_meanVector[i].real() - p->
m_meanVector[i].real();
174 term2(i, 0) = term1(0, i);
177 term1 = prod(term1, m_covarianceInversion);
178 term1 = prod(term1, term2);
188 assert(m_myCluster == 0);
191 return m_id == rhs.
m_id;
223 const double dmax = std::numeric_limits<double>::max();
224 double chiTable[10][6] =
227 { 1.32, 2.71, 3.84, 6.64, 10.83, dmax},
228 { 2.77, 4.61, 5.99, 9.21, 13.82, dmax},
229 { 4.11, 6.25, 7.82, 11.35, 16.27, dmax},
230 { 5.39, 7.78, 9.49, 13.28, 18.47, dmax},
231 { 6.63, 9.24, 11.07, 15.09, 20.52, dmax},
232 { 7.84, 10.65, 12.59, 16.81, 22.46, dmax},
233 { 9.04, 12.02, 14.07, 18.48, 24.32, dmax},
234 {10.22, 13.36, 15.51, 20.09, 26.13, dmax},
235 {11.39, 14.68, 16.92, 21.67, 27.88, dmax},
236 {12.55, 15.99, 18.31, 23.21, 29.59, dmax}
243 if(acceptanceThreshold < 90.0)
245 else if(acceptanceThreshold < 95.0)
247 else if(acceptanceThreshold < 99.0)
249 else if(acceptanceThreshold < 99.9)
251 else if(acceptanceThreshold < 100.0)
256 return chiTable[nBands-1][it];
260 const std::vector<te::gm::Polygon*>& inputPolygons,
te::rst::Raster& outputRaster,
261 const unsigned int outputRasterBand,
const bool enableProgressInterface)
throw(te::rp::Exception)
266 std::complex<double> mean;
271 for (
unsigned i = 0; i < inputPolygons.size(); i++)
275 std::vector<std::complex<double> > means = rattributes.getMeans(inputRaster, *polygon, inputRasterBands);
277 Pattern* region =
new Pattern(i, polygon->
getArea(), means, rattributes.getCovarianceMatrix(inputRaster, *polygon, inputRasterBands));
283 m_regions.insert(std::pair<double, Pattern*> (region->
m_area, region));
290 std::multimap<double, Pattern*, std::greater<double> >::iterator rit;
291 std::multimap<double, Pattern*, std::greater<double> >::iterator riti;
292 std::multimap<double, Pattern*, std::greater<double> >::iterator ritii;
297 std::set<std::pair<unsigned int, unsigned int> > compared;
310 for (riti = rit; riti !=
m_regions.end(); ++riti)
312 if (compared.find(std::pair<unsigned int, unsigned int>(rit->second->m_myCluster->m_id, riti->second->m_myCluster->m_id)) == compared.end())
314 compared.insert(std::pair<unsigned int, unsigned int>(rit->second->m_myCluster->m_id, riti->second->m_myCluster->m_id));
315 compared.insert(std::pair<unsigned int, unsigned int>(riti->second->m_myCluster->m_id, rit->second->m_myCluster->m_id));
320 if (rit->second->m_myCluster == riti->second->m_myCluster)
324 distance = rit->second->m_myCluster->getDistance(riti->second->m_myCluster);
326 distance2 = riti->second->m_myCluster->getDistance(rit->second->m_myCluster);
328 distance = (distance2 < distance? distance2: distance);
330 if (distance <= maxDistance)
335 oldid = riti->second->m_myCluster->m_id;
337 for (ritii = riti; ritii !=
m_regions.end(); ++ritii)
340 if (ritii->second->m_myCluster->m_id != oldid)
343 ritii->second->m_myCluster = rit->second->m_myCluster;
351 std::set<unsigned int> ulabels;
352 std::set<unsigned int>::iterator lit;
353 std::map<unsigned int, unsigned int> colormap;
356 ulabels.insert(rit->second->m_myCluster->m_id);
358 unsigned int color = 1;
359 for (lit = ulabels.begin(); lit != ulabels.end(); ++lit)
360 colormap[*lit] = color++;
363 unsigned int pattern;
378 if (startGridCoord.
y > endGridCoord.
y)
380 tmpCoord = startGridCoord.
y;
381 startGridCoord.
y = endGridCoord.
y;
382 endGridCoord.
y = tmpCoord;
385 pattern = rit->second->m_myCluster->m_id;
393 outputRaster.setValue(it.
getColumn(), it.
getRow(), colormap[pattern], outputRasterBand);
double m_acceptanceThreshold
The acceptance threshold (the closer to 100%, few clusters are created).
double m_area
The area of all regions inside a pattern.
AbstractParameters * clone() const
Create a clone copy of this instance.
Polygon is a subclass of CurvePolygon whose rings are defined by linear rings.
Raster strategy parameters base class.
double getArea() const
It returns the area of this surface, as measured in the spatial reference system of this surface...
unsigned int getRow() const
Returns the current row in iterator.
Raster Processing functions.
te::rp::ClassifierStrategy * build()
Concrete factories (derived from this one) must implement this method in order to create objects...
Describes a region or a cluster (group of regions with similar properties) to be used by ISOSeg metho...
boost::numeric::ublas::matrix< double > m_covarianceMatrix
The covariance matrix between bands.
double getThreshold(double acceptanceThreshold, unsigned nBands)
void reset()
Clear all internal allocated resources and reset the parameters instance to its initial state...
#define TR_RP(message)
It marks a string in order to get translated. This is a special mark used in the Raster Processing mo...
std::vector< std::complex< double > > m_meanVector
The vector of mean values, 1 value per band;.
An utility struct for representing 2D coordinates.
static PolygonIterator begin(const te::rst::Raster *r, const te::gm::Polygon *p)
Returns an iterator referring to the first value of the band.
Coord2D getUpperRight() const
It returns the upper right coordinate of the envelope.
#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...
double getDistance(Pattern *p)
Returns the Mahalanobis distance between two patterns.
ISOSeg strategy for segmentation-based classification.
Abstract parameters base interface.
Defines the exact sequence of characters that are acceptable.
Pattern * m_myCluster
The associated cluster of this pattern (optional).
const Parameters & operator=(const Parameters ¶ms)
boost::numeric::ublas::matrix< double > m_covarianceInversion
The inversion of covariance matrix between bands.
Raster classifier strategy factory base class.
Extraction of attributes from Raster, Bands, and Polygons.
This class implements the strategy to iterate with spatial restriction, the iteration occurs inside a...
std::multimap< double, Pattern *, std::greater< double > > m_regions
A descriptive set of regions (area, features).
bool m_isInitialized
True if this instance is initialized.
Extraction of attributes from Raster, Bands, and Polygons.
bool operator=(Pattern &rhs)
Return true if two clusters are equal.
ClassifierISOSegStrategyFactory()
void add(Pattern *p)
Add a region inside a cluster.
bool getInverseMatrix(const boost::numeric::ublas::matrix< T > &inputMatrix, boost::numeric::ublas::matrix< T > &outputMatrix)
Matrix inversion.
unsigned int getColumn() const
Returns the current column in iterator.
int m_id
The id of the region of the pattern.
~ClassifierISOSegStrategy()
ClassifierISOSegStrategy::Parameters m_parameters
Internal execution parameters.
bool execute(const te::rst::Raster &inputRaster, const std::vector< unsigned int > &inputRasterBands, const std::vector< te::gm::Polygon * > &inputPolygons, te::rst::Raster &outputRaster, const unsigned int outputRasterBand, const bool enableProgressInterface)
Executes the classification strategy.
An abstract class for raster data strucutures.
Raster ISOSeg Classifier strategy factory.
This class can be used to inform the progress of a task.
const Envelope * getMBR() const
It returns the minimum bounding rectangle for the geometry in an internal representation.
static PolygonIterator end(const te::rst::Raster *r, const te::gm::Polygon *p)
Returns an iterator referring to after the end of the iterator.
~ClassifierISOSegStrategyFactory()
Raster classifier strategy base class.
Coord2D getLowerLeft() const
It returns the lower left coordinate of the envelope.
bool initialize(StrategyParameters const *const strategyParams)
Initialize the classification strategy.
ClassifierISOSegStrategy()
ISOSeg strategy for OBIA classification. The algorithm orders regions by area (larger first)...