1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450
|
/*
* Copyright (C) 1999-2011 Insight Software Consortium
* Copyright (C) 2005-2020 Centre National d'Etudes Spatiales (CNES)
* Copyright (C) 2016-2019 IRSTEA
*
* This file is part of Orfeo Toolbox
*
* https://www.orfeo-toolbox.org/
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef __StreamingStatisticsMosaicFilter_H
#define __StreamingStatisticsMosaicFilter_H
#include "otbStreamingMosaicFilterBase.h"
#include "otbPersistentMosaicFilter.h"
#include "otbPersistentFilterStreamingDecorator.h"
#include "itkArray.h"
#include "itkSimpleDataObjectDecorator.h"
#include "itkImageRegionConstIteratorWithOnlyIndex.h"
namespace otb
{
/** \class PersistentStatisticsMosaicFilter
* \brief Computes the statistics of a mosaic from an input images set.
* The output pixel value is equal to the number of overlaps
*
* Support streaming
*
* The pixels must support the operator ==, +, /, etc.
* The "no data value", output spacing, interpolator can be chosen.
* The behavior of the filter is to compute input images statistics
* as they were in a layered fashion: interpolators are used to
* compute pixels values of all input images for a given output pixel.
* This is used to compute the following matrices: mean, standard
* deviation, min, max, and means of pixels products. Let's denote
* X one of these matrices, then X\{ij} is the statistic of the image
* i in the overlapping area with the image j.
*
* \ingroup OTBMosaic
* \ingroup OTBStatistics
*/
template <class TInputImage, class TOutputImage = TInputImage, class TInternalValueType = double>
class ITK_EXPORT PersistentStatisticsMosaicFilter : public otb::PersistentMosaicFilter<TInputImage, TOutputImage, TInternalValueType>
{
public:
/** Standard Self typedef */
typedef PersistentStatisticsMosaicFilter Self;
typedef PersistentMosaicFilter<TInputImage, TOutputImage, TInternalValueType> Superclass;
typedef itk::SmartPointer<Self> Pointer;
typedef itk::SmartPointer<const Self> ConstPointer;
/** Method for creation through the object factory. */
itkNewMacro(Self);
/** Runtime information support. */
itkTypeMacro(PersistentStatisticsMosaicFilter, PersistentMosaicFilter);
/** Input image typedefs. */
typedef typename Superclass::InputImageType InputImageType;
typedef typename Superclass::InputImagePointer InputImagePointer;
typedef typename Superclass::InputImagePointType InputImagePointType;
typedef typename Superclass::InputImagePixelType InputImagePixelType;
typedef typename Superclass::InputImageInternalPixelType InputImageInternalPixelType;
/** Output image typedefs. */
typedef typename Superclass::OutputImageType OutputImageType;
typedef typename Superclass::OutputImagePointType OutputImagePointType;
typedef typename Superclass::OutputImageRegionType OutputImageRegionType;
/** Internal computing typedef support. */
typedef typename Superclass::InternalValueType InternalValueType;
typedef typename Superclass::InterpolatorPointerType InterpolatorPointerType;
typedef itk::ImageRegionConstIteratorWithOnlyIndex<OutputImageType> IteratorType;
/** Typedefs for statistics */
typedef vnl_vector<InternalValueType> RealVectorType;
typedef vnl_matrix<InternalValueType> RealMatrixType;
typedef itk::SimpleDataObjectDecorator<RealMatrixType> RealMatrixObjectType;
typedef std::vector<RealMatrixType> RealMatrixListType;
typedef itk::SimpleDataObjectDecorator<RealMatrixListType> RealMatrixListObjectType;
/** Smart Pointer type to a DataObject. */
typedef typename itk::DataObject::Pointer DataObjectPointer;
typedef itk::ProcessObject::DataObjectPointerArraySizeType DataObjectPointerArraySizeType;
/** Overrided methods */
void AllocateOutputs() override;
void ThreadedGenerateData(const OutputImageRegionType& outputRegionForThread, itk::ThreadIdType threadId) override;
void Reset() override;
void Synthetize() override;
/** Make a DataObject of the correct type to be used as the specified output. */
DataObjectPointer MakeOutput(DataObjectPointerArraySizeType idx) override;
using Superclass::MakeOutput;
/** Return the computed Mean. */
RealMatrixListType GetMeans() const
{
return this->GetMeansOutput()->Get();
}
RealMatrixListObjectType* GetMeansOutput();
const RealMatrixListObjectType* GetMeansOutput() const;
/** Return the computed Std. */
RealMatrixListType GetStds() const
{
return this->GetStdsOutput()->Get();
}
RealMatrixListObjectType* GetStdsOutput();
const RealMatrixListObjectType* GetStdsOutput() const;
/** Return the computed Min. */
RealMatrixListType GetMins() const
{
return this->GetMinsOutput()->Get();
}
RealMatrixListObjectType* GetMinsOutput();
const RealMatrixListObjectType* GetMinsOutput() const;
/** Return the computed Max. */
RealMatrixListType GetMaxs() const
{
return this->GetMaxsOutput()->Get();
}
RealMatrixListObjectType* GetMaxsOutput();
const RealMatrixListObjectType* GetMaxsOutput() const;
/** Return the computed MeansOfProduct. */
RealMatrixListType GetMeansOfProducts() const
{
return this->GetMeansOfProductsOutput()->Get();
}
RealMatrixListObjectType* GetMeansOfProductsOutput();
const RealMatrixListObjectType* GetMeansOfProductsOutput() const;
/** Return the computed areas. */
RealMatrixType GetAreas() const
{
return this->GetAreasOutput()->Get();
}
RealMatrixObjectType* GetAreasOutput();
const RealMatrixObjectType* GetAreasOutput() const;
protected:
PersistentStatisticsMosaicFilter();
virtual ~PersistentStatisticsMosaicFilter()
{
}
/** Class for storing thread results:
* -sum of values
* -sum of squared values
* -min value
* -max value
* -count
*/
class ThreadResultsContainer
{
public:
/** Default constructor */
ThreadResultsContainer()
{
}
/* Constructor with size */
ThreadResultsContainer(unsigned int nbOfBands, unsigned int nbOfSamples)
{
Clear(nbOfBands, nbOfSamples);
}
/* Copy constructor */
ThreadResultsContainer(const ThreadResultsContainer& other)
{
m_count = RealVectorType(other.m_count);
m_sum = RealMatrixType(other.m_sum);
m_cosum = RealMatrixType(other.m_cosum);
m_sqSum = RealMatrixType(other.m_sqSum);
m_min = RealMatrixType(other.m_min);
m_max = RealMatrixType(other.m_max);
}
/* Clear routine: Resize at the specified dimension and clear values */
void Clear(unsigned int nbOfBands, unsigned int nbOfSamples)
{
const InternalValueType zeroValue = itk::NumericTraits<InternalValueType>::Zero;
const InternalValueType supValue = itk::NumericTraits<InternalValueType>::max();
const InternalValueType infValue = itk::NumericTraits<InternalValueType>::NonpositiveMin();
m_count = RealVectorType(nbOfSamples, 0);
m_sum = RealMatrixType(nbOfBands, nbOfSamples, zeroValue);
m_cosum = RealMatrixType(nbOfBands, nbOfSamples, zeroValue);
m_sqSum = RealMatrixType(nbOfBands, nbOfSamples, zeroValue);
m_min = RealMatrixType(nbOfBands, nbOfSamples, supValue);
m_max = RealMatrixType(nbOfBands, nbOfSamples, infValue);
}
/* 1-Pixel update */
void Update(const InputImagePixelType& pixel, unsigned int sampleId)
{
unsigned int nbOfBands = pixel.Size();
m_count[sampleId]++;
for (unsigned int band = 0; band < nbOfBands; band++)
{
// Cast
InternalValueType pixelValue = static_cast<InternalValueType>(pixel[band]);
// Update Min & max
if (pixelValue < m_min[band][sampleId])
m_min[band][sampleId] = pixelValue;
if (pixelValue > m_max[band][sampleId])
m_max[band][sampleId] = pixelValue;
// Update Sums
m_sum[band][sampleId] += pixelValue;
m_sqSum[band][sampleId] += pixelValue * pixelValue;
}
}
/* 2-Pixels update */
void Update(const InputImagePixelType& pixel_i, const InputImagePixelType& pixel_j, unsigned int sampleId)
{
Update(pixel_i, sampleId);
unsigned int nbOfBands = pixel_i.Size();
for (unsigned int band = 0; band < nbOfBands; band++)
{
// Cast
InternalValueType pixelValue_i = static_cast<InternalValueType>(pixel_i[band]);
InternalValueType pixelValue_j = static_cast<InternalValueType>(pixel_j[band]);
m_cosum[band][sampleId] += pixelValue_i * pixelValue_j;
}
}
/* Self update */
void Update(const ThreadResultsContainer& other)
{
unsigned int nbOfBands = other.m_sum.rows();
unsigned int nbOfSamples = other.m_sum.cols();
for (unsigned int sampleId = 0; sampleId < nbOfSamples; sampleId++)
{
m_count[sampleId] += other.m_count[sampleId];
for (unsigned int band = 0; band < nbOfBands; band++)
{
m_sum[band][sampleId] += other.m_sum[band][sampleId];
m_cosum[band][sampleId] += other.m_cosum[band][sampleId];
m_sqSum[band][sampleId] += other.m_sqSum[band][sampleId];
if (other.m_min[band][sampleId] < m_min[band][sampleId])
m_min[band][sampleId] = other.m_min[band][sampleId];
if (other.m_max[band][sampleId] > m_max[band][sampleId])
m_max[band][sampleId] = other.m_max[band][sampleId];
}
}
}
RealMatrixType m_sum;
RealMatrixType m_sqSum;
RealMatrixType m_cosum;
RealMatrixType m_min;
RealMatrixType m_max;
RealVectorType m_count;
};
// Internal threads count
std::vector<ThreadResultsContainer> m_InternalThreadResults;
// Results
RealMatrixListType m_Means;
RealMatrixListType m_Stds;
RealMatrixListType m_ProdMeans;
RealMatrixListType m_Mins;
RealMatrixListType m_Maxs;
RealMatrixType m_Area;
private:
PersistentStatisticsMosaicFilter(const Self&); // purposely not implemented
void operator=(const Self&); // purposely not implemented
}; // end of class
/** \class StreamingStatisticsMosaicFilter
* \brief This class streams the whole input image through the PersistentStatisticsMosaicFilter.
*
* This way, it allows computing the stats of the input images. It calls the
* Reset() method of the PersistentStatisticsMosaicFilter before streaming the image and the
* Synthetize() method of the PersistentStatisticsMosaicFilter after having streamed the image
* to compute the statistics. The accessor on the results are wrapping the accessors of the
* internal PersistentStatisticsMosaicFilter.
*
* \sa PersistentStatisticsMosaicFilter
* \sa PersistentImageFilter
* \sa PersistentFilterStreamingDecorator
* \sa StreamingImageVirtualWriter
* \ingroup Streamed
* \ingroup Multithreaded
* \ingroup Mosaic
*
* \ingroup OTBStatistics
*/
template <class TInputImage, class TOutputImage, class TInternalValueType>
class ITK_EXPORT StreamingStatisticsMosaicFilter
: public PersistentFilterStreamingDecorator<PersistentStatisticsMosaicFilter<TInputImage, TOutputImage, TInternalValueType>>
{
public:
/** Standard Self typedef */
typedef StreamingStatisticsMosaicFilter Self;
typedef PersistentFilterStreamingDecorator<PersistentStatisticsMosaicFilter<TInputImage, TOutputImage, TInternalValueType>> Superclass;
typedef itk::SmartPointer<Self> Pointer;
typedef itk::SmartPointer<const Self> ConstPointer;
/** Type macro */
itkNewMacro(Self);
/** Creation through object factory macro */
itkTypeMacro(StreamingStatisticsMosaicFilter, PersistentFilterStreamingDecorator);
/** Typedefs for statistics */
typedef typename Superclass::FilterType::RealMatrixType RealMatrixType;
typedef typename Superclass::FilterType::RealMatrixObjectType RealMatrixObjectType;
typedef typename Superclass::FilterType::RealMatrixListType RealMatrixListType;
typedef typename Superclass::FilterType::RealMatrixListObjectType RealMatrixListObjectType;
/** Input image typedefs. */
typedef typename Superclass::FilterType::InputImageType InputImageType;
using Superclass::PushBackInput;
void PushBackInput(InputImageType* input)
{
this->GetFilter()->PushBackInput(input);
}
/** Return the computed Means. */
RealMatrixListType GetMeans() const
{
return this->GetFilter()->GetMeansOutput()->Get();
}
RealMatrixListObjectType* GetMeansOutput()
{
return this->GetFilter()->GetMeansOutput();
}
const RealMatrixListObjectType* GetMeansOutput() const
{
return this->GetFilter()->GetMeansOutput();
}
/** Return the computed Stds. */
RealMatrixListType GetStds() const
{
return this->GetFilter()->GetStdsOutput()->Get();
}
RealMatrixListObjectType* GetStdsOutput()
{
return this->GetFilter()->GetStdsOutput();
}
const RealMatrixListObjectType* GetStdsOutput() const
{
return this->GetFilter()->GetStdsOutput();
}
/** Return the computed MeansOfProducts. */
RealMatrixListType GetMeansOfProducts() const
{
return this->GetFilter()->GetMeansOfProductsOutput()->Get();
}
RealMatrixListObjectType* GetMeansOfProductsOutput()
{
return this->GetFilter()->GetMeansOfProductsOutput();
}
const RealMatrixListObjectType* GetMeansOfProductsOutput() const
{
return this->GetFilter()->GetMeansOfProductsOutput();
}
/** Return the computed Mins. */
RealMatrixListType GetMins() const
{
return this->GetFilter()->GetMinsOutput()->Get();
}
RealMatrixListObjectType* GetMinsOutput()
{
return this->GetFilter()->GetMinsOutput();
}
const RealMatrixListObjectType* GetMinsOutput() const
{
return this->GetFilter()->GetMinsOutput();
}
/** Return the computed Maxs. */
RealMatrixListType GetMaxs() const
{
return this->GetFilter()->GetMaxsOutput()->Get();
}
RealMatrixListObjectType* GetMaxsOutput()
{
return this->GetFilter()->GetMaxsOutput();
}
const RealMatrixListObjectType* GetMaxsOutput() const
{
return this->GetFilter()->GetMaxsOutput();
}
/** Return the computed Areas. */
RealMatrixType GetAreas() const
{
return this->GetFilter()->GetAreasOutput()->Get();
}
RealMatrixObjectType* GetAreasOutput()
{
return this->GetFilter()->GetAreasOutput();
}
const RealMatrixObjectType* GetAreasOutput() const
{
return this->GetFilter()->GetAreasOutput();
}
protected:
/** Constructor */
StreamingStatisticsMosaicFilter(){};
/** Destructor */
~StreamingStatisticsMosaicFilter() override
{
}
private:
StreamingStatisticsMosaicFilter(const Self&); // purposely not implemented
void operator=(const Self&); // purposely not implemented
};
} // end namespace otb
#ifndef OTB_MANUAL_INSTANTIATION
#include "otbStreamingStatisticsMosaicFilter.hxx"
#endif
#endif
|