Orfeo Toolbox  3.16
otbLocalRxDetectorNonThreadFilter.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  Copyright (c) Centre National d'Etudes Spatiales. All rights reserved.
9  See OTBCopyright.txt for details.
10 
11 
12  This software is distributed WITHOUT ANY WARRANTY; without even
13  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14  PURPOSE. See the above copyright notices for more information.
15 
16 =========================================================================*/
17 
18 #ifndef __otbLocalRxDetectorNonThreadFilter_txx
19 #define __otbLocalRxDetectorNonThreadFilter_txx
20 
22 
23 namespace otb
24 {
25 
29 template <class TInputImage, class TOutputImage>
32 {
33  this->m_ExternalRadius = 0;
34  this->m_InternalRadius = 0;
35 }
36 
40 template <class TInputImage, class TOutputImage>
41 void
43 ::PrintSelf(std::ostream& os, itk::Indent indent) const
44 {
45  Superclass::PrintSelf(os, indent);
46 
47  os << indent << "Internal Radius: " << m_InternalRadius << std::endl;
48  os << indent << "External Radius: " << m_ExternalRadius << std::endl;
49 }
50 
54 template <class TInputImage, class TOutputImage>
55 void
58 {
59 
60  this->AllocateOutputs();
61 
62  // Get the input and output pointers
63  InputPointerType inputPtr =
64  const_cast< InputImageType * >( this->GetInput());
65  OutputPointerType outputPtr = this->GetOutput();
66 
67  inputPtr->Update();
68 
69  // Fill the output buffer with black pixels
70  outputPtr->FillBuffer(0);
71 
72  // Iterator on input region
74  radius.Fill(m_ExternalRadius);
75 
76  VectorFaceCalculatorType vectorFaceCalculator;
77  typename VectorFaceCalculatorType::FaceListType vectorFaceList;
78  typename VectorFaceCalculatorType::FaceListType::iterator vectorFit;
79 
80  vectorFaceList = vectorFaceCalculator(inputPtr, inputPtr->GetRequestedRegion(), radius);
81  vectorFit = vectorFaceList.begin(); // Only the first face is used
82  ConstShapedNeighborhoodIteratorType inputIt(radius, inputPtr, *vectorFit);
83 
84  // Neighborhood Configuration
86 
87  for (int y = -m_ExternalRadius; y <= m_ExternalRadius; y++)
88  {
89  off[1] = y;
90  for (int x = -m_ExternalRadius; x <= m_ExternalRadius; x++)
91  {
92  off[0] = x;
93  if ((abs(x) > m_InternalRadius) || (abs(y) > m_InternalRadius))
94  {
95  inputIt.ActivateOffset(off);
96  }
97  }
98  }
99 
100  // Iterator on output region
101  FaceCalculatorType faceCalculator;
102  typename FaceCalculatorType::FaceListType faceList;
103  typename FaceCalculatorType::FaceListType::iterator fit;
104 
105  faceList = faceCalculator(outputPtr, inputPtr->GetRequestedRegion(), radius);
106  fit = faceList.begin(); // Only the first face is used
107 
108 
109  ImageRegionIteratorType outputIt(outputPtr, *fit);
110 
111  //std::cout << "Region de la face list : " << *vectorFit << std::endl;
112  //std::cout << "Region dans le buffer : " << inputPtr->GetBufferedRegion() << std::endl;
113 
114  // Run Input Image
115  for (inputIt.GoToBegin(), outputIt.GoToBegin(); !inputIt.IsAtEnd(); ++inputIt, ++outputIt)
116  {
117  // Create ListSample
118  typename ListSampleType::Pointer listSample = ListSampleType::New();
119  listSample->SetMeasurementVectorSize(inputPtr->GetNumberOfComponentsPerPixel());
120 
121  // Run neighborhood
123  for (ci = inputIt.Begin(); !ci.IsAtEnd(); ++ci)
124  {
125  // Pushback element in listSample
126  //std::cout << "pixel of shaped iteror : " << ci.Get() << std::endl;
127  listSample->PushBack(ci.Get());
128  }
129 
130  // Compute Mean vector
131  typename MeanCalculatorType::Pointer meanCalculator = MeanCalculatorType::New();
132  meanCalculator->SetInputSample(listSample);
133  meanCalculator->Update();
134 
135  typename MeanCalculatorType::OutputType *meanVector;
136  meanVector = meanCalculator->GetOutput();
137 
138  // Compute covariance matrix
139  typename CovarianceCalculatorType::Pointer covarianceCalculator = CovarianceCalculatorType::New();
140  covarianceCalculator->SetInputSample(listSample);
141  covarianceCalculator->SetMean(meanVector);
142  covarianceCalculator->Update();
143 
144  const typename CovarianceCalculatorType::OutputType *covarianceMatrix = covarianceCalculator->GetOutput();
145 
146  // Compute RX value
147  MatrixType invCovMat;
148  invCovMat = covarianceMatrix->GetInverse();
149 
150  VectorMeasurementType testPixVec;
151  testPixVec = inputPtr->GetPixel(inputIt.GetIndex());
152 
153  VectorMeasurementType meanVec(meanVector->GetNumberOfElements());
154  for(unsigned int i = 0; i < meanVector->GetNumberOfElements(); ++i)
155  {
156  meanVec.SetElement(i, meanVector->GetElement(i));
157  }
158 
159  typename MatrixType::InternalMatrixType centeredTestPixMat(meanVector->GetNumberOfElements(), 1);
160  for (unsigned int i = 0; i < centeredTestPixMat.rows(); ++i)
161  {
162  centeredTestPixMat.put(i, 0, (testPixVec.GetElement(i) - meanVector->GetElement(i)));
163  }
164 
165  typename MatrixType::InternalMatrixType rxValue = centeredTestPixMat.transpose() * invCovMat.GetVnlMatrix() * centeredTestPixMat;
166 
167  outputIt.Set(rxValue.get(0, 0));
168  }
169 }
170 
174 template <class TInputImage, class TOutputImage>
175 void
178 {
179  // Call the superclass' implementation of this method
180  Superclass::GenerateInputRequestedRegion();
181 
182  // Get pointers to the input and output
183  InputPointerType inputPtr =
184  const_cast< InputImageType * >( this->GetInput());
185  OutputPointerType outputPtr = this->GetOutput();
186 
187  if ( !inputPtr || !outputPtr )
188  {
189  return;
190  }
191 
192  // Get a copy of the input requested region (should equal the output
193  // requested region)
194  typename TInputImage::RegionType inputRequestedRegion;
195  inputRequestedRegion = inputPtr->GetRequestedRegion();
196 
197  // Pad the input requested region by the operator radius
198  inputRequestedRegion.PadByRadius( m_ExternalRadius );
199 
200  // Crop the input requested region at the input's largest possible region
201  if ( inputRequestedRegion.Crop(inputPtr->GetLargestPossibleRegion()) )
202  {
203  inputPtr->SetRequestedRegion( inputRequestedRegion );
204  return;
205  }
206  else
207  {
208  // Couldn't crop the region (requested region is outside the largest
209  // possible region). Throw an exception.
210 
211  // Store what we tried to request (prior to trying to crop)
212  inputPtr->SetRequestedRegion( inputRequestedRegion );
213 
214  // Build an exception
215  itk::InvalidRequestedRegionError e(__FILE__, __LINE__);
216  e.SetLocation(ITK_LOCATION);
217  e.SetDescription("Requested region is (at least partially) outside the largest possible region.");
218  e.SetDataObject(inputPtr);
219  throw e;
220  }
221 }
222 
223 } // end namespace otb
224 
225 #endif

Generated at Sun Feb 3 2013 00:35:26 for Orfeo Toolbox with doxygen 1.8.1.1