Orfeo Toolbox  3.16
otbGaussianRenderingFunction.h
Go to the documentation of this file.
1 /*=========================================================================
2 
3  Program: ORFEO Toolbox
4  Language: C++
5  Date: $Date$
6  Version: $Revision$
7 
8 
9  Copyright (c) Centre National d'Etudes Spatiales. All rights reserved.
10  See OTBCopyright.txt for details.
11 
12 
13  This software is distributed WITHOUT ANY WARRANTY; without even
14  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15  PURPOSE. See the above copyright notices for more information.
16 
17  =========================================================================*/
18 #ifndef __otbGaussianRenderingFunction_h
19 #define __otbGaussianRenderingFunction_h
20 
21 #include <cassert>
22 #include <iomanip>
23 #include <vector>
24 #include <algorithm>
25 
26 #include "otbMacro.h"
27 #include "otbI18n.h"
30 #include "otbViewerConst.h"
32 
33 
34 namespace otb
35 {
36  namespace Function
37  {
45  template <class TPixel, class TRGBPixel,
46  class TPixelRepresentationFunction = ChannelSelectorFunctor<TPixel>,
47  class TTransferFunction = Identity<
48  typename itk::NumericTraits<typename itk::NumericTraits<TPixel>::ValueType>::RealType,
49  typename itk::NumericTraits<typename itk::NumericTraits<TPixel>::ValueType>::RealType
50  > >
52  : public StandardRenderingFunction<TPixel, TRGBPixel, TPixelRepresentationFunction, TTransferFunction>
53  {
54  public:
60 
62 
63 
66 
68  itkNewMacro(Self);
69 
71  typedef TRGBPixel OutputPixelType;
72  typedef typename OutputPixelType::ValueType OutputValueType;
73  typedef TPixel PixelType;
74  typedef typename itk::NumericTraits<PixelType>::ValueType ScalarType;
75  typedef typename itk::NumericTraits<ScalarType>::RealType RealScalarType;
77 
78 
80 
84  virtual void Initialize(const MetaDataDictionaryType &metadatadictionary)
85  {
86  Superclass::Initialize(metadatadictionary);
88  }
89 
101  {
102  if ((spixel.Size() != 1) && (spixel.Size() != 3) && (spixel.Size() != 4))
103  {
104  itkExceptionMacro( << "the PixelRepresentation function should give an output of "
105  << "size 1, 3 or 4 otherwise I don't know how to make an RGB of it !" );
106  }
107 
108  if (spixel.Size() != this->m_TransferedMinimum.size())
109  {
110  itkExceptionMacro( << " m_TransferedMinimum and pixel size do not correspond"
111  << " spixel.Size(): " << spixel.Size()
112  << " m_TransferedMinimum.size(): " << this->m_TransferedMinimum.size());
113  }
114 
116 
117  if (spixel.Size() == 1)
118  {
119  OutputValueType value = ClampRescale(this->m_TransferFunction(spixel[0]), 0);
120  output[0] = value;
121  output[1] = value;
122  output[2] = value;
123  }
124  else
125  {
126  output[0] = ClampRescale(this->m_TransferFunction(spixel[0]), 0);
127  output[1] = ClampRescale(this->m_TransferFunction(spixel[1]), 1);
128  output[2] = ClampRescale(this->m_TransferFunction(spixel[2]), 2);
129  }
130 
131  if ((spixel.Size() == 4) && (output.Size() == 4))
132  {
133  assert((spixel[3] >= SCREEN_COLOR_MIN_VALUE) && (spixel[3] <= SCREEN_COLOR_MAX_VALUE));
134  output[3] = static_cast<OutputValueType>(spixel[3]); //just copy the alpha channel
135  }
136 
137  return output;
138  }
139 
147  virtual void SetParameters(const ParametersType & p)
148  {
149  m_Mean = p[0];
150  m_StandardDeviation = p[1];
151 
152  ParametersType psup;
153  psup.SetSize(p.GetSize() - 2);
154  for (unsigned int i = 0; i < psup.GetSize(); ++i)
155  {
156  psup[i] = p[i + 2];
157  }
158 
160 
162 
163  otbMsgDevMacro(<< "GaussianRenderingFunction::SetParameters: Gaussian curve mean = "
164  << m_Mean << "; standard deviation = " << m_StandardDeviation);
165 
166  this->Modified();
167  }
168 
177  {
178  ParametersType p;
180 
181  p.SetSize(psup.GetSize() + 2);
182  p[0] = m_Mean;
183  p[1] = m_StandardDeviation;
184 
185  for (unsigned int i = 0; i < psup.GetSize(); ++i)
186  {
187  p[i + 2] = psup[i];
188  }
189 
190  return p;
191  }
192 
193  protected:
200  {}
201 
206 
207 
222  {
223  m_BinLowerBounds.clear();
224 
225  // MDE : Correction
226  unsigned int nbHist = this->m_PixelRepresentationFunction->GetOutputSize();
227 
228  for (unsigned int histIndex = 0; histIndex < nbHist; histIndex++)
229  {
230  std::vector<double> blb;
231 
232  double xMin = m_Mean - 3 * m_StandardDeviation;
233  double xMax = m_Mean + 3 * m_StandardDeviation;
234 
235  for (unsigned int binIndex = 0; binIndex <= SCREEN_COLOR_MAX_VALUE; binIndex++)
236  {
237  double x = ( xMax - xMin ) * binIndex / ( SCREEN_COLOR_MAX_VALUE + 1 ) + xMin;
238  double cdfValue = itk::Statistics::GaussianDistribution::CDF(x, m_Mean, m_StandardDeviation);
239 
240  double binMinValue = this->GetHistogramList()->GetNthElement(histIndex)->Quantile(0, cdfValue);
241 
242  blb.push_back(binMinValue);
243  }
244 
245  m_BinLowerBounds.push_back(blb);
246  }
247  }
248 
253  const OutputValueType ClampRescale(RealScalarType input, unsigned int channel) const
254  {
255  const std::vector<double> & blb = m_BinLowerBounds[channel];
256  std::vector<double>::const_iterator it;
257  double output;
258 
259  it = std::lower_bound(blb.begin(), blb.end(), static_cast<double>(input));
260 
261  if (it == blb.end())
262  {
263  output = SCREEN_COLOR_MAX_VALUE;
264  }
265  else if (it != blb.begin())
266  {
267  output = int(it - blb.begin()) - 1;
268  }
269  else
270  {
271  output = SCREEN_COLOR_MIN_VALUE;
272  }
273 
274  return static_cast<OutputValueType>(output);
275  }
276 
280  double m_Mean;
281 
287 
292  std::vector< std::vector<double> > m_BinLowerBounds;
293 
294 
295  private:
296 
297  GaussianRenderingFunction(const Self&); //purposely not implemented
298  void operator=(const Self&); //purposely not implemented
299 
300  };
301 
302 } // end namespace Functor
303 
304 } // end namespace otb
305 
306 #endif /* __otbGaussianRenderingFunction_h */

Generated at Sun Feb 3 2013 00:24:12 for Orfeo Toolbox with doxygen 1.8.1.1