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
|
/*=========================================================================
Program: ORFEO Toolbox
Language: C++
Date: $Date$
Version: $Revision$
Copyright (c) Centre National d'Etudes Spatiales. All rights reserved.
See OTBCopyright.txt for details.
Some parts of this code are derived from ITK. See ITKCopyright.txt
for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notices for more information.
=========================================================================*/
#ifndef otbStreamingStatisticsMapFromLabelImageFilter_txx
#define otbStreamingStatisticsMapFromLabelImageFilter_txx
#include "otbStreamingStatisticsMapFromLabelImageFilter.h"
#include "itkInputDataObjectIterator.h"
#include "itkImageRegionIterator.h"
#include "itkProgressReporter.h"
#include "otbMacro.h"
namespace otb
{
template<class TInputVectorImage, class TLabelImage>
PersistentStreamingStatisticsMapFromLabelImageFilter<TInputVectorImage, TLabelImage>
::PersistentStreamingStatisticsMapFromLabelImageFilter()
{
// first output is a copy of the image, DataObject created by
// superclass
//
// allocate the data objects for the outputs which are
// just decorators around pixel types
typename MeanValueMapObjectType::Pointer output
= static_cast<MeanValueMapObjectType*>(this->MakeOutput(1).GetPointer());
this->itk::ProcessObject::SetNthOutput(1, output.GetPointer());
this->Reset();
}
template<class TInputVectorImage, class TLabelImage>
typename itk::DataObject::Pointer
PersistentStreamingStatisticsMapFromLabelImageFilter<TInputVectorImage, TLabelImage>
::MakeOutput(DataObjectPointerArraySizeType output)
{
switch (output)
{
case 0:
return static_cast<itk::DataObject*>(TInputVectorImage::New().GetPointer());
break;
case 1:
return static_cast<itk::DataObject*>(MeanValueMapObjectType::New().GetPointer());
break;
default:
// might as well make an image
return static_cast<itk::DataObject*>(TInputVectorImage::New().GetPointer());
break;
}
}
template<class TInputVectorImage, class TLabelImage>
void
PersistentStreamingStatisticsMapFromLabelImageFilter<TInputVectorImage, TLabelImage>
::SetInputLabelImage(const LabelImageType *input)
{
// Process object is not const-correct so the const_cast is required here
this->itk::ProcessObject::SetNthInput(1,
const_cast< LabelImageType * >( input ) );
}
template<class TInputVectorImage, class TLabelImage>
const typename PersistentStreamingStatisticsMapFromLabelImageFilter<TInputVectorImage, TLabelImage>::LabelImageType*
PersistentStreamingStatisticsMapFromLabelImageFilter<TInputVectorImage, TLabelImage>
::GetInputLabelImage()
{
return static_cast< const TLabelImage * >
(this->itk::ProcessObject::GetInput(1));
}
template<class TInputVectorImage, class TLabelImage>
typename PersistentStreamingStatisticsMapFromLabelImageFilter<TInputVectorImage, TLabelImage>::MeanValueMapType
PersistentStreamingStatisticsMapFromLabelImageFilter<TInputVectorImage, TLabelImage>
::GetMeanValueMap() const
{
return m_RadiometricValueAccumulator;
}
template<class TInputVectorImage, class TLabelImage>
typename PersistentStreamingStatisticsMapFromLabelImageFilter<TInputVectorImage, TLabelImage>::LabelPopulationMapType
PersistentStreamingStatisticsMapFromLabelImageFilter<TInputVectorImage, TLabelImage>
::GetLabelPopulationMap() const
{
return m_LabelPopulation;
}
template<class TInputVectorImage, class TLabelImage>
void
PersistentStreamingStatisticsMapFromLabelImageFilter<TInputVectorImage, TLabelImage>
::GenerateOutputInformation()
{
Superclass::GenerateOutputInformation();
if (this->GetInput())
{
this->GetOutput()->CopyInformation(this->GetInput());
this->GetOutput()->SetLargestPossibleRegion(this->GetInput()->GetLargestPossibleRegion());
if (this->GetOutput()->GetRequestedRegion().GetNumberOfPixels() == 0)
{
this->GetOutput()->SetRequestedRegion(this->GetOutput()->GetLargestPossibleRegion());
}
}
}
template<class TInputVectorImage, class TLabelImage>
void
PersistentStreamingStatisticsMapFromLabelImageFilter<TInputVectorImage, TLabelImage>
::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
}
template<class TInputVectorImage, class TLabelImage>
void
PersistentStreamingStatisticsMapFromLabelImageFilter<TInputVectorImage, TLabelImage>
::Synthetize()
{
typename MeanValueMapType::iterator it;
for(it = m_RadiometricValueAccumulator.begin(); it != m_RadiometricValueAccumulator.end(); it++)
{
it->second = it->second / m_LabelPopulation[it->first];
}
}
template<class TInputVectorImage, class TLabelImage>
void
PersistentStreamingStatisticsMapFromLabelImageFilter<TInputVectorImage, TLabelImage>
::Reset()
{
m_RadiometricValueAccumulator.clear();
}
template<class TInputVectorImage, class TLabelImage>
void
PersistentStreamingStatisticsMapFromLabelImageFilter<TInputVectorImage, TLabelImage>
::GenerateInputRequestedRegion()
{
// The Requested Regions of all the inputs are set to their Largest Possible Regions
this->itk::ProcessObject::GenerateInputRequestedRegion();
// Iteration over all the inputs of the current filter (this)
for( itk::InputDataObjectIterator it( this ); !it.IsAtEnd(); it++ )
{
// Check whether the input is an image of the appropriate dimension
// dynamic_cast of all the input images as itk::ImageBase objects
// in order to pass the if ( input ) test whatever the inputImageType (vectorImage or labelImage)
ImageBaseType * input = dynamic_cast< ImageBaseType *>( it.GetInput() );
if ( input )
{
// Use the function object RegionCopier to copy the output region
// to the input. The default region copier has default implementations
// to handle the cases where the input and output are the same
// dimension, the input a higher dimension than the output, and the
// input a lower dimension than the output.
InputImageRegionType inputRegion;
this->CallCopyOutputRegionToInputRegion( inputRegion, this->GetOutput()->GetRequestedRegion() );
input->SetRequestedRegion(inputRegion);
}
}
}
template<class TInputVectorImage, class TLabelImage>
void
PersistentStreamingStatisticsMapFromLabelImageFilter<TInputVectorImage, TLabelImage>
::GenerateData()
{
/**
* Grab the input
*/
InputVectorImagePointer inputPtr = const_cast<TInputVectorImage *>(this->GetInput());
LabelImagePointer labelInputPtr = const_cast<TLabelImage *>(this->GetInputLabelImage());
itk::ImageRegionConstIterator<TInputVectorImage> inIt(inputPtr, inputPtr->GetRequestedRegion());
itk::ImageRegionConstIterator<TLabelImage> labelIt(labelInputPtr, labelInputPtr->GetRequestedRegion());
itk::VariableLengthVector<double> zeroValue(inputPtr->GetNumberOfComponentsPerPixel());
zeroValue.Fill(0.0);
typename VectorImageType::PixelType value;
typename LabelImageType::PixelType label;
// do the work
for (inIt.GoToBegin(), labelIt.GoToBegin();
!inIt.IsAtEnd() && !labelIt.IsAtEnd();
++inIt, ++labelIt)
{
value = inIt.Get();
label = labelIt.Get();
if (m_RadiometricValueAccumulator.count(label)<=0) //add new element to the map
{
// use a zero pixel with the right number of components to support scalar images
m_RadiometricValueAccumulator[label] = (zeroValue + value);
m_LabelPopulation[label] = 1;
}
else
{
m_RadiometricValueAccumulator[label] += value;
m_LabelPopulation[label] ++;
}
}
}
template<class TInputVectorImage, class TLabelImage>
void
PersistentStreamingStatisticsMapFromLabelImageFilter<TInputVectorImage, TLabelImage>
::PrintSelf(std::ostream& os, itk::Indent indent) const
{
Superclass::PrintSelf(os, indent);
}
} // end namespace otb
#endif
|