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
|
/*
* 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_hxx
#define __StreamingStatisticsMosaicFilter_hxx
#include "otbStreamingStatisticsMosaicFilter.h"
namespace otb
{
template <class TInputImage, class TOutputImage, class TInternalValueType>
PersistentStatisticsMosaicFilter<TInputImage, TOutputImage, TInternalValueType>::PersistentStatisticsMosaicFilter()
{
// allocate the data objects for the outputs which are
// just decorators around pixel types
// 1: means
// 2: stdevs
// 3: means of products
// 4: mins
// 5: maxs
for (int i = 1; i < 6; ++i)
{
typename RealMatrixListObjectType::Pointer output = static_cast<RealMatrixListObjectType*>(this->MakeOutput(i).GetPointer());
this->itk::ProcessObject::SetNthOutput(i, output.GetPointer());
}
// allocate the data objects for the outputs which are
// just decorators around real matrices
//
// 6: count
typename RealMatrixObjectType::Pointer output = static_cast<RealMatrixObjectType*>(this->MakeOutput(6).GetPointer());
this->itk::ProcessObject::SetNthOutput(6, output.GetPointer());
}
/*
* Make output
*/
template <class TInputImage, class TOutputImage, class TInternalValueType>
typename itk::DataObject::Pointer
PersistentStatisticsMosaicFilter<TInputImage, TOutputImage, TInternalValueType>::MakeOutput(DataObjectPointerArraySizeType output)
{
itkDebugMacro(<< "Entering MakeOutput(" << output << ")");
if (output == 0)
{
return static_cast<itk::DataObject*>(TOutputImage::New().GetPointer());
}
else if (output == 6)
{
return static_cast<itk::DataObject*>(RealMatrixObjectType::New().GetPointer());
}
return static_cast<itk::DataObject*>(RealMatrixListObjectType::New().GetPointer());
}
template <class TInputImage, class TOutputImage, class TInternalValueType>
typename PersistentStatisticsMosaicFilter<TInputImage, TOutputImage, TInternalValueType>::RealMatrixListObjectType*
PersistentStatisticsMosaicFilter<TInputImage, TOutputImage, TInternalValueType>::GetMeansOutput()
{
return static_cast<RealMatrixListObjectType*>(this->itk::ProcessObject::GetOutput(1));
}
template <class TInputImage, class TOutputImage, class TInternalValueType>
typename PersistentStatisticsMosaicFilter<TInputImage, TOutputImage, TInternalValueType>::RealMatrixListObjectType*
PersistentStatisticsMosaicFilter<TInputImage, TOutputImage, TInternalValueType>::GetStdsOutput()
{
return static_cast<RealMatrixListObjectType*>(this->itk::ProcessObject::GetOutput(2));
}
template <class TInputImage, class TOutputImage, class TInternalValueType>
typename PersistentStatisticsMosaicFilter<TInputImage, TOutputImage, TInternalValueType>::RealMatrixListObjectType*
PersistentStatisticsMosaicFilter<TInputImage, TOutputImage, TInternalValueType>::GetMeansOfProductsOutput()
{
return static_cast<RealMatrixListObjectType*>(this->itk::ProcessObject::GetOutput(3));
}
template <class TInputImage, class TOutputImage, class TInternalValueType>
typename PersistentStatisticsMosaicFilter<TInputImage, TOutputImage, TInternalValueType>::RealMatrixListObjectType*
PersistentStatisticsMosaicFilter<TInputImage, TOutputImage, TInternalValueType>::GetMinsOutput()
{
return static_cast<RealMatrixListObjectType*>(this->itk::ProcessObject::GetOutput(4));
}
template <class TInputImage, class TOutputImage, class TInternalValueType>
typename PersistentStatisticsMosaicFilter<TInputImage, TOutputImage, TInternalValueType>::RealMatrixListObjectType*
PersistentStatisticsMosaicFilter<TInputImage, TOutputImage, TInternalValueType>::GetMaxsOutput()
{
return static_cast<RealMatrixListObjectType*>(this->itk::ProcessObject::GetOutput(5));
}
template <class TInputImage, class TOutputImage, class TInternalValueType>
typename PersistentStatisticsMosaicFilter<TInputImage, TOutputImage, TInternalValueType>::RealMatrixObjectType*
PersistentStatisticsMosaicFilter<TInputImage, TOutputImage, TInternalValueType>::GetAreasOutput()
{
return static_cast<RealMatrixObjectType*>(this->itk::ProcessObject::GetOutput(6));
}
template <class TInputImage, class TOutputImage, class TInternalValueType>
const typename PersistentStatisticsMosaicFilter<TInputImage, TOutputImage, TInternalValueType>::RealMatrixListObjectType*
PersistentStatisticsMosaicFilter<TInputImage, TOutputImage, TInternalValueType>::GetMeansOutput() const
{
return static_cast<const RealMatrixListObjectType*>(this->itk::ProcessObject::GetOutput(1));
}
template <class TInputImage, class TOutputImage, class TInternalValueType>
const typename PersistentStatisticsMosaicFilter<TInputImage, TOutputImage, TInternalValueType>::RealMatrixListObjectType*
PersistentStatisticsMosaicFilter<TInputImage, TOutputImage, TInternalValueType>::GetStdsOutput() const
{
return static_cast<const RealMatrixListObjectType*>(this->itk::ProcessObject::GetOutput(2));
}
template <class TInputImage, class TOutputImage, class TInternalValueType>
const typename PersistentStatisticsMosaicFilter<TInputImage, TOutputImage, TInternalValueType>::RealMatrixListObjectType*
PersistentStatisticsMosaicFilter<TInputImage, TOutputImage, TInternalValueType>::GetMeansOfProductsOutput() const
{
return static_cast<const RealMatrixListObjectType*>(this->itk::ProcessObject::GetOutput(3));
}
template <class TInputImage, class TOutputImage, class TInternalValueType>
const typename PersistentStatisticsMosaicFilter<TInputImage, TOutputImage, TInternalValueType>::RealMatrixListObjectType*
PersistentStatisticsMosaicFilter<TInputImage, TOutputImage, TInternalValueType>::GetMinsOutput() const
{
return static_cast<const RealMatrixListObjectType*>(this->itk::ProcessObject::GetOutput(4));
}
template <class TInputImage, class TOutputImage, class TInternalValueType>
const typename PersistentStatisticsMosaicFilter<TInputImage, TOutputImage, TInternalValueType>::RealMatrixListObjectType*
PersistentStatisticsMosaicFilter<TInputImage, TOutputImage, TInternalValueType>::GetMaxsOutput() const
{
return static_cast<const RealMatrixListObjectType*>(this->itk::ProcessObject::GetOutput(5));
}
template <class TInputImage, class TOutputImage, class TInternalValueType>
const typename PersistentStatisticsMosaicFilter<TInputImage, TOutputImage, TInternalValueType>::RealMatrixObjectType*
PersistentStatisticsMosaicFilter<TInputImage, TOutputImage, TInternalValueType>::GetAreasOutput() const
{
return static_cast<const RealMatrixObjectType*>(this->itk::ProcessObject::GetOutput(6));
}
/**
* Reset() implementation
*/
template <class TInputImage, class TOutputImage, class TInternalValueType>
void PersistentStatisticsMosaicFilter<TInputImage, TOutputImage, TInternalValueType>::Reset()
{
itkDebugMacro(<< "Entering Reset()");
Superclass::GenerateOutputInformation();
// Prepare threads result
const unsigned int numberOfThreads = this->GetNumberOfThreads();
const unsigned int nBands = this->GetNumberOfBands();
const unsigned int nbImages = this->GetNumberOfInputImages();
itkDebugMacro(<< "\nN threads: " << numberOfThreads << "\nN bands: " << nBands << "\nN images: " << nbImages);
m_InternalThreadResults.clear();
for (unsigned int threadId = 0; threadId < numberOfThreads; threadId++)
{
// Create a clean empty container for each thread
ThreadResultsContainer threadResult(nBands, nbImages * nbImages);
m_InternalThreadResults.push_back(threadResult);
}
}
template <class TInputImage, class TOutputImage, class TInternalValueType>
void PersistentStatisticsMosaicFilter<TInputImage, TOutputImage, TInternalValueType>::AllocateOutputs()
{
itkDebugMacro(<< "Entering AllocateOutputs()");
// This is commented to prevent the streaming of the whole image for the first stream strip
// It shall not cause any problem because the output image of this filter is not intended to be used.
// InputImagePointer image = const_cast< TInputImage * >( this->GetInput() );
// this->GraftOutput( image );
// Nothing that needs to be allocated for the remaining outputs
}
/*
* Synthetize() implementation
*/
template <class TInputImage, class TOutputImage, class TInternalValueType>
void PersistentStatisticsMosaicFilter<TInputImage, TOutputImage, TInternalValueType>::Synthetize()
{
itkDebugMacro(<< "Entering Synthetize()");
const unsigned int nBands = Superclass::GetNumberOfBands();
const unsigned int nbImages = this->GetNumberOfInputImages();
// Merge threads result
ThreadResultsContainer finalResults = ThreadResultsContainer(nBands, nbImages * nbImages);
for (const auto& res : m_InternalThreadResults)
{
finalResults.Update(res);
}
// Compute final outputs
m_Means.clear();
m_Stds.clear();
m_ProdMeans.clear();
m_Mins.clear();
m_Maxs.clear();
m_Area = RealMatrixType(nbImages, nbImages, 0);
for (unsigned int band = 0; band < nBands; band++)
{
RealMatrixType mean(nbImages, nbImages, 0);
RealMatrixType prodmean(nbImages, nbImages, 0);
RealMatrixType stdev(nbImages, nbImages, 0);
RealMatrixType min(nbImages, nbImages, itk::NumericTraits<InputImageInternalPixelType>::max());
RealMatrixType max(nbImages, nbImages, itk::NumericTraits<InputImageInternalPixelType>::NonpositiveMin());
for (unsigned int i = 0; i < nbImages; i++)
{
for (unsigned int j = 0; j < nbImages; j++)
{
const InternalValueType count = finalResults.m_count[i * nbImages + j];
const InternalValueType sum = finalResults.m_sum[band][i * nbImages + j];
const InternalValueType cosum = finalResults.m_cosum[band][i * nbImages + j];
const InternalValueType sqSum = finalResults.m_sqSum[band][i * nbImages + j];
const InternalValueType minVal = finalResults.m_min[band][i * nbImages + j];
const InternalValueType maxVal = finalResults.m_max[band][i * nbImages + j];
// Update area
m_Area[i][j] = count;
// Update Min and Max
if (minVal < min[i][j])
min[i][j] = minVal;
if (maxVal > max[i][j])
max[i][j] = maxVal;
// Update Mean, Std and Mean of products
if (count > 0)
{
mean[i][j] = sum / (static_cast<InternalValueType>(count));
prodmean[i][j] = cosum / (static_cast<InternalValueType>(count));
// Unbiased estimate
InternalValueType variance = (sqSum - (sum * sum / static_cast<InternalValueType>(count))) / (static_cast<InternalValueType>(count) - 1);
if (variance > 0)
{
stdev[i][j] = vcl_sqrt(variance);
}
}
}
}
m_Means.push_back(mean);
m_Stds.push_back(stdev);
m_ProdMeans.push_back(prodmean);
m_Mins.push_back(min);
m_Maxs.push_back(max);
} // next band
this->GetMeansOutput()->Set(m_Means);
this->GetStdsOutput()->Set(m_Stds);
this->GetMeansOfProductsOutput()->Set(m_ProdMeans);
this->GetMinsOutput()->Set(m_Mins);
this->GetMaxsOutput()->Set(m_Maxs);
this->GetAreasOutput()->Set(m_Area);
}
/**
* Processing
*/
template <class TInputImage, class TOutputImage, class TInternalValueType>
void PersistentStatisticsMosaicFilter<TInputImage, TOutputImage, TInternalValueType>::ThreadedGenerateData(const OutputImageRegionType& outputRegionForThread,
itk::ThreadIdType threadId)
{
// Debug info
itkDebugMacro(<< "Actually executing thread " << threadId << " in region " << outputRegionForThread);
// Support progress methods/callbacks
itk::ProgressReporter progress(this, threadId, outputRegionForThread.GetNumberOfPixels());
// Get number of input images
const unsigned int nbOfInputImages = this->GetNumberOfInputImages();
// Get number of used inputs
const unsigned int nbOfUsedInputImages = Superclass::GetNumberOfUsedInputImages();
// Iterate through the thread region
IteratorType outputIt(this->GetOutput(), outputRegionForThread);
// Prepare input pointers, interpolators, and valid regions (input images)
typename std::vector<InputImageType*> currentImage;
typename std::vector<InterpolatorPointerType> interp;
Superclass::PrepareImageAccessors(currentImage, interp);
// temporary variables
OutputImagePointType geoPoint;
for (outputIt.GoToBegin(); !outputIt.IsAtEnd(); ++outputIt)
{
// Update progress
progress.CompletedPixel();
// Overlap descriptor for the current pixel (yes/no + value)
std::vector<unsigned int> overlapImagesIndices;
std::vector<InputImagePixelType> overlapPixelValue;
// Current pixel --> Geographical point
this->GetOutput()->TransformIndexToPhysicalPoint(outputIt.GetIndex(), geoPoint);
// Loop on used input images
for (unsigned int i = 0; i < nbOfUsedInputImages; i++)
{
// Check if the point is inside the transformed thread region
// (i.e. the region in the current input image which match the thread
// region)
if (interp[i]->IsInsideBuffer(geoPoint))
{
// Compute the interpolated pixel value
InputImagePixelType interpolatedPixel = interp[i]->Evaluate(geoPoint);
// Put image index + image pixel value into memory
if (Superclass::IsPixelNotEmpty(interpolatedPixel))
{
overlapImagesIndices.push_back(Superclass::GetUsedInputImageIndice(i));
overlapPixelValue.push_back(interpolatedPixel);
}
} // point inside buffer
} // next image
// Update thread result
// Nb of overlaps at the current pixel
unsigned int nbOfOverlappingPixels = overlapImagesIndices.size();
// Loop on overlapping pixels
for (unsigned int i = 0; i < nbOfOverlappingPixels; i++)
{
// Index of the image whose comes the current overlapping pixel
unsigned int imageIndex = overlapImagesIndices.at(i);
// We need to sum this pixel to all overlaps ij
InputImagePixelType pixel = overlapPixelValue.at(i);
for (unsigned int j = 0; j < nbOfOverlappingPixels; j++)
{
// if (i!=j)
{
// Index of the other image which share this overlapping pixel
unsigned int otherImageIndex = overlapImagesIndices.at(j);
// Pixel value of the other image which share this overlapping pixel
InputImagePixelType otherPixel = overlapPixelValue.at(j);
// Update
m_InternalThreadResults.at(threadId).Update(pixel, otherPixel, imageIndex * nbOfInputImages + otherImageIndex);
}
}
} // loop on overlapping pixels
} // next output pixel
}
} // end namespace gtb
#endif
|