27 #include "../common/MatrixUtils.h" 28 #include "../common/progress/TaskProgress.h" 29 #include "../raster/Grid.h" 30 #include "../raster/Utils.h" 37 #include <boost/numeric/ublas/io.hpp> 38 #include <boost/numeric/ublas/matrix.hpp> 96 const std::vector<std::string>& inputSensorBands,
const std::map<std::string, std::vector<double> >& components,
97 std::vector<te::rst::Raster*>& outputRaster,
const bool normalize,
const bool enableProgressInterface)
throw(te::rp::Exception)
101 const unsigned int nComponents = (
unsigned int)components.size();
102 const unsigned int nBands = (
unsigned int)inputRasterBands.size();
104 double dummy = outputRaster[0]->getBand(0)->getProperty()->m_noDataValue;
106 std::vector<double> Qmax;
107 std::vector<double> Qmin;
108 for (
unsigned i = 0; i < inputSensorBands.size(); i++)
116 std::vector<te::rst::BandProperty*> tmpbandsProperties;
117 for (
unsigned i = 0; i < outputRaster[0]->getNumberOfBands(); i++)
122 tmpbandsProperties.push_back(bprop);
124 std::unique_ptr<te::rst::Raster> tmpRaster (
te::rst::RasterFactory::make(
"MEM", tmpgrid, tmpbandsProperties, std::map<std::string, std::string>()));
127 std::vector<double> tmp_min;
128 std::vector<double> tmp_max;
129 for (
unsigned i = 0; i < tmpRaster->getNumberOfBands(); i++)
131 tmp_min.push_back(std::numeric_limits< double >::max());
132 tmp_max.push_back(-std::numeric_limits< double >::max());
136 m_max.push_back(255);
140 m_min.push_back(std::numeric_limits< double >::max());
141 m_max.push_back(-std::numeric_limits< double >::max());
143 m_minerror.push_back(std::numeric_limits< double >::max());
144 m_maxerror.push_back(-std::numeric_limits< double >::max());
153 boost::numeric::ublas::matrix<double> productAtA = boost::numeric::ublas::matrix<double>(
m_transposeA.size1(),
m_transfMatrix.size2());
157 boost::numeric::ublas::matrix<double> invertAtA = boost::numeric::ublas::matrix<double>(productAtA.size1(), productAtA.size2());
160 boost::numeric::ublas::matrix<double> matrixR = boost::numeric::ublas::matrix<double>(
m_transfMatrix.size1(), 1);
161 boost::numeric::ublas::matrix<double> productAtR = boost::numeric::ublas::matrix<double>(
m_transposeA.size1(), matrixR.size2());
162 boost::numeric::ublas::matrix<double> matrixX = boost::numeric::ublas::matrix<double>(invertAtA.size1(), productAtR.size2());
163 boost::numeric::ublas::matrix<double> productAX = boost::numeric::ublas::matrix<double>(
m_transfMatrix.size1(), matrixX.size2());
164 boost::numeric::ublas::matrix<double> matrixE = boost::numeric::ublas::matrix<double>(matrixR.size1(), matrixR.size2());
169 for (
unsigned r = 0; r < inputRaster.getNumberOfRows(); r++)
171 for (
unsigned c = 0; c < inputRaster.getNumberOfColumns(); c++)
174 for (
unsigned b = 0;
b < nBands;
b++)
176 inputRaster.getValue(c, r, value, inputRasterBands[
b]);
180 matrixR(b, 0) = value / Qmax[
b];
182 matrixR(nBands, 0) = 0.0;
184 productAtR = boost::numeric::ublas::prod(
m_transposeA, matrixR);
186 matrixX = boost::numeric::ublas::prod(invertAtA, productAtR);
188 for (
unsigned b = 0;
b < nBands;
b++)
190 value = matrixX(
b + 1, 0);
191 tmpRaster->setValue(c, r, value,
b);
194 tmp_min[
b] = value < tmp_min[
b] ? value : tmp_min[
b];
195 tmp_max[
b] = value > tmp_max[
b] ? value : tmp_max[
b];
200 if (outputRaster.size() > 1)
203 matrixE = matrixR - productAX;
206 for (
unsigned b = 0, e = 0;
b < nBands;
b++, e++)
208 value = matrixE(e, 0);
209 outputRaster[1]->setValue(c, r, value,
b);
219 for (
unsigned r = 0; r < tmpRaster->getNumberOfRows(); r++)
221 for (
unsigned c = 0; c < tmpRaster->getNumberOfColumns(); c++)
223 for (
unsigned b = 0;
b < nComponents;
b++)
225 tmpRaster->getValue(c, r, value,
b);
229 value = 255 * (value - tmp_min[
b]) / (tmp_max[
b] - tmp_min[
b]);
231 value = (Qmax[
b] - Qmin[
b]) * (value - tmp_min[b]) / (tmp_max[
b] - tmp_min[
b]);
233 outputRaster[0]->setValue(c, r, value,
b);
244 boost::numeric::ublas::matrix<double>& matrix)
const 252 boost::numeric::ublas::matrix<double>& matrix) {
258 const std::map<std::string, std::vector<double> >& components)
260 const unsigned int nComponents = (
unsigned int)components.size();
261 const unsigned int nBands = (
unsigned int)inputRasterBands.size();
263 boost::numeric::ublas::matrix<double> transposeA = boost::numeric::ublas::matrix<double>(nComponents + 1, nBands + 1);
264 boost::numeric::ublas::matrix<double> matrixA(transposeA.size2(), transposeA.size1());
271 std::vector<double> Qmax;
272 for (
unsigned i = 0; i < inputSensorBands.size(); i++)
279 for (
unsigned j = 0; j < nBands; j++)
280 transposeA(row, j) = 1.0;
281 transposeA(row, nBands) = 0.0;
283 std::map<std::string, std::vector<double> >::const_iterator it;
284 for (row = 1, it = components.begin(); it != components.end(); row++, it++)
286 for (
unsigned j = 0; j < nBands; j++)
287 transposeA(row, j) = it->second[j] / Qmax[j];
288 transposeA(row, nBands) = 1.0;
292 matrixA = boost::numeric::ublas::trans(transposeA);
double GetDigitalNumberBandMax(std::string bandName)
Returns the maximum digital number of a given sensor/band.
Raster Mixture model strategy factory base class.
te::rp::MixtureModelStrategy * build()
Concrete factories (derived from this one) must implement this method in order to create objects...
Raster linear mixture model strategy factory.
bool generateTransformMatrix(const std::vector< unsigned int > &inputRasterBands, const std::vector< std::string > &inputSensorBands, const std::map< std::string, std::vector< double > > &components)
Generates the used transformation matrix (when applicable).
~MixtureModelLinearStrategy()
Index into a lookup table.
bool getMinMaxError(std::vector< double > &, std::vector< double > &) const
std::vector< double > m_maxerror
Maximum error value.
A raster band description.
Base exception class for plugin module.
This class can be used to inform the progress of a task.
boost::numeric::ublas::matrix< double > m_transposeA
Transformation matrix;.
bool getTransformMatrix(boost::numeric::ublas::matrix< double > &matrix) const
Returns the used transformation matrix (when applicable).
int m_type
The data type of the elements in the band ( See te::dt namespace basic data types for reference )...
#define TE_TR(message)
It marks a string in order to get translated.
double GetDigitalNumberBandMin(std::string bandName)
Returns the minimum digital number of a given sensor/band.
MixtureModelLinearStrategy()
#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...
std::vector< double > m_minerror
Minimun error value.
boost::numeric::ublas::matrix< double > m_transfMatrix
std::vector< double > m_min
Transpose of A (A is the set of known reflectances for each component);.
Raster mixture model strategy base class.
bool getMinMax(std::vector< double > &, std::vector< double > &) const
An abstract class for raster data strucutures.
bool execute(const te::rst::Raster &inputRaster, const std::vector< unsigned int > &inputRasterBands, const std::vector< std::string > &inputSensorBands, const std::map< std::string, std::vector< double > > &components, std::vector< te::rst::Raster * > &outputRaster, const bool normalize, const bool enableProgressInterface)
Executes the segmentation strategy.
Raster strategy parameters base class.
MixtureModelLinearStrategyFactory()
Raster linear strategy for mixture model classification.
AbstractParameters * clone() const
Create a clone copy of this instance.
Abstract parameters base interface.
~MixtureModelLinearStrategyFactory()
bool initialize(StrategyParameters const *const strategyParams)
Initialize the segmentation strategy.
MixtureModelLinearStrategy::Parameters m_parameters
Internal execution parameters.
static Raster * make()
It creates and returns an empty raster with default raster driver.
bool GetInverseMatrix(const boost::numeric::ublas::matrix< T > &inputMatrix, boost::numeric::ublas::matrix< T > &outputMatrix)
Matrix inversion.
void reset()
Clear all internal allocated resources and reset the parameters instance to its initial state...
Raster Processing functions.
std::vector< double > m_max
Maximum value.
A rectified grid is the spatial support for raster data.
const Parameters & operator=(const Parameters ¶ms)
ColorInterp m_colorInterp
The color interpretation.
bool m_isInitialized
True if this instance is initialized.
bool setTransformMatrix(boost::numeric::ublas::matrix< double > &matrix)
Sets the used transformation matrix.