Orfeo Toolbox  3.16
otbScalarImageToPanTexTextureFilter.txx
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 __otbScalarImageToPanTexTextureFilter_txx
19 #define __otbScalarImageToPanTexTextureFilter_txx
20 
23 #include "itkImageRegionIterator.h"
24 #include "itkProgressReporter.h"
25 
26 namespace otb
27 {
28 template <class TInputImage, class TOutputImage>
31  m_NumberOfBinsPerAxis(8),
32  m_InputImageMinimum(0),
33  m_InputImageMaximum(256)
34 {
35  // There are 1 output corresponding to the Pan Tex texture indice
36  this->SetNumberOfOutputs(1);
37 
38  //Fill the offset list for contrast computation
39  OffsetType off;
40  off[0] = 0;
41  off[1] = 1;
42  m_OffsetList.push_back(off); //(0, 1)
43  off[1] = 2;
44  m_OffsetList.push_back(off); //(0, 2)
45  off[0] = 1;
46  off[1] = -2;
47  m_OffsetList.push_back(off); //(1, -2)
48  off[1] = -1;
49  m_OffsetList.push_back(off); //(1, -1)
50  off[1] = 0;
51  m_OffsetList.push_back(off); //(1, 0)
52  off[1] = 1;
53  m_OffsetList.push_back(off); //(1, 1)
54  off[1] = 2;
55  m_OffsetList.push_back(off); //(1, 2)
56  off[0] = 2;
57  off[1] = -1;
58  m_OffsetList.push_back(off); //(2, -1)
59  off[1] = 0;
60  m_OffsetList.push_back(off); //(2, 0)
61  off[1] = 1;
62  m_OffsetList.push_back(off); //(2, 1)
63 }
64 
65 template <class TInputImage, class TOutputImage>
68 {}
69 
70 template <class TInputImage, class TOutputImage>
71 void
74 {
75  // First, call superclass implementation
76  Superclass::GenerateInputRequestedRegion();
77 
78  // Retrieve the input and output pointers
79  InputImagePointerType inputPtr = const_cast<InputImageType *>(this->GetInput());
80  OutputImagePointerType outputPtr = this->GetOutput();
81 
82  if (!inputPtr || !outputPtr)
83  {
84  return;
85  }
86 
87  // Retrieve the output requested region
88  // We use only the first output since requested regions for all outputs are enforced to be equal
89  // by the default GenerateOutputRequestedRegiont() implementation
90  OutputRegionType outputRequestedRegion = outputPtr->GetRequestedRegion();
91 
92  // Build the input requested region
93  InputRegionType inputRequestedRegion = outputRequestedRegion;
94 
95  // Apply the radius
96  SizeType maxOffsetSize;
97  maxOffsetSize[0] = 2;
98  maxOffsetSize[1] = 2;
99  inputRequestedRegion.PadByRadius(m_Radius + maxOffsetSize);
100 
101  // Try to apply the requested region to the input image
102  if (inputRequestedRegion.Crop(inputPtr->GetLargestPossibleRegion()))
103  {
104  inputPtr->SetRequestedRegion(inputRequestedRegion);
105  }
106  else
107  {
108  // Build an exception
109  itk::InvalidRequestedRegionError e(__FILE__, __LINE__);
110  e.SetLocation(ITK_LOCATION);
111  e.SetDescription("Requested region is (at least partially) outside the largest possible region.");
112  e.SetDataObject(inputPtr);
113  throw e;
114  }
115 }
116 
117 template <class TInputImage, class TOutputImage>
118 void
120 ::ThreadedGenerateData(const OutputRegionType& outputRegionForThread, int threadId)
121 {
122  // Retrieve the input and output pointers
123  InputImagePointerType inputPtr = const_cast<InputImageType *>(this->GetInput());
124  OutputImagePointerType outputPtr = this->GetOutput();
125 
126  itk::ImageRegionIteratorWithIndex<OutputImageType> outputIt(outputPtr, outputRegionForThread);
127 
128  // Go to begin
129  outputIt.GoToBegin();
130 
131  // Set-up progress reporting
132  itk::ProgressReporter progress(this, threadId, outputRegionForThread.GetNumberOfPixels());
133 
134  // Iterate on outputs to compute textures
135  while (!outputIt.IsAtEnd())
136  {
137  // Find the input region on which texture will be computed
138  InputRegionType currentRegion;
139  typename InputRegionType::IndexType currentIndex = outputIt.GetIndex() - m_Radius;
140  typename InputRegionType::SizeType currentSize;
141 
142  for (unsigned int dim = 0; dim < InputImageType::ImageDimension; ++dim)
143  {
144  // Compute current size before applying offset
145  currentSize[dim] = 2 * m_Radius[dim] + 1;
146  }
147 
148  // Fill current region
149  currentRegion.SetIndex(currentIndex);
150  currentRegion.SetSize(currentSize);
151 
152  // Initialise output value
153  double out = itk::NumericTraits<double>::max();
154 
155  // For each offset
156  typename OffsetListType::const_iterator offIt;
157  for (offIt = m_OffsetList.begin(); offIt != m_OffsetList.end(); ++offIt)
158  {
159  // Build the co-occurence matrix generator
160  CoocurrenceMatrixGeneratorPointerType coOccurenceMatrixGenerator = CoocurrenceMatrixGeneratorType::New();
161  coOccurenceMatrixGenerator->SetInput(inputPtr);
162  coOccurenceMatrixGenerator->SetOffset((*offIt));
163  coOccurenceMatrixGenerator->SetNumberOfBinsPerAxis(m_NumberOfBinsPerAxis);
164  coOccurenceMatrixGenerator->SetPixelValueMinMax(m_InputImageMinimum, m_InputImageMaximum);
165 
166  // Compute the co-occurence matrix
167  coOccurenceMatrixGenerator->SetRegion(currentRegion);
168  coOccurenceMatrixGenerator->SetNormalize(true);
169  coOccurenceMatrixGenerator->Compute();
170 
171  typename HistogramType::Pointer histo = coOccurenceMatrixGenerator->GetOutput();
172 
173  double inertia = 0;
174  for (HistogramIterator hit = histo->Begin(); hit != histo->End(); ++hit)
175  {
176  MeasurementType frequency = hit.GetFrequency();
177  if (frequency == 0)
178  {
179  continue; // no use doing these calculations if we're just multiplying by zero.
180  }
181  typename InputRegionType::IndexType index = histo->GetIndex(hit.GetInstanceIdentifier());
182  inertia += (index[0] - index[1]) * (index[0] - index[1]) * frequency;
183  }
184 
185  if (inertia < out) out = inertia;
186  }
187 
188  outputIt.Set(out);
189  ++outputIt;
190  progress.CompletedPixel();
191  }
192 }
193 
194 } // End namespace otb
195 
196 #endif

Generated at Sun Feb 3 2013 00:46:43 for Orfeo Toolbox with doxygen 1.8.1.1