Orfeo Toolbox  4.0
otbDisparityMapEstimationMethod.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 __otbDisparityMapEstimationMethod_txx
19 #define __otbDisparityMapEstimationMethod_txx
20 
23 #include "otbExtractROI.h"
24 #include "otbMacro.h"
25 
26 // #include "otbImageFileWriter.h"
27 //
28 
29 namespace otb
30 {
31 /*
32  * Constructor.
33  */
34 template <typename TFixedImage, typename TMovingImage, class TPointSet>
37 {
38  this->SetNumberOfRequiredInputs(3);
39  //this->SetNumberOfRequiredOutputs(1);
40  // this->SetReleaseDataFlag(false);
41  this->SetReleaseDataBeforeUpdateFlag(false);
42  m_Transform = 0; // has to be provided by the user.
43  m_Interpolator = 0; // has to be provided by the user.
44  m_Metric = 0; // has to be provided by the user.
45  m_Optimizer = 0; // has to be provided by the user.
46  m_WinSize.Fill(15);
47  m_ExploSize.Fill(10);
48  m_InitialTransformParameters = ParametersType(1);
49  m_InitialTransformParameters.Fill(0.0f);
50 }
51 /*
52  * Destructor.
53  */
54 template <class TFixedImage, class TMovingImage, class TPointSet>
57 {}
62 template <class TFixedImage, class TMovingImage, class TPointSet>
63 void
65 ::SetPointSet(const TPointSet * pointset)
66 {
67  this->itk::ProcessObject::SetNthInput(0, const_cast<PointSetType *>(pointset));
68 }
73 template <class TFixedImage, class TMovingImage, class TPointSet>
74 const TPointSet *
77 {
78  return static_cast<const PointSetType *>(this->itk::ProcessObject::GetInput(0));
79 }
84 template <class TFixedImage, class TMovingImage, class TPointSet>
85 void
87 ::SetFixedImage(const TFixedImage * image)
88 {
89  this->itk::ProcessObject::SetNthInput(1, const_cast<FixedImageType *>(image));
90 }
95 template <class TFixedImage, class TMovingImage, class TPointSet>
96 const TFixedImage *
99 {
100  return static_cast<const FixedImageType *>(this->itk::ProcessObject::GetInput(1));
101 }
106 template <class TFixedImage, class TMovingImage, class TPointSet>
107 void
109 ::SetMovingImage(const TMovingImage * image)
110 {
111  this->itk::ProcessObject::SetNthInput(2, const_cast<MovingImageType *>(image));
112 }
117 template <class TFixedImage, class TMovingImage, class TPointSet>
118 const TMovingImage *
121 {
122  return static_cast<const MovingImageType *>(this->itk::ProcessObject::GetInput(2));
123 }
124 
128 template <class TFixedImage, class TMovingImage, class TPointSet>
129 void
132 {
133  // inputs pointers
134  const FixedImageType * fixed = this->GetFixedImage();
135  const MovingImageType * moving = this->GetMovingImage();
136  const PointSetType * pointSet = this->GetPointSet();
137  PointSetType* output = this->GetOutput();
138 
139  // Typedefs
140  typedef typename PointSetType::PointsContainer PointsContainer;
141  typedef typename PointsContainer::ConstIterator PointsIterator;
143  typedef otb::ExtractROI<FixedPixelType, FixedPixelType> FixedExtractType;
144  typedef otb::ExtractROI<MovingPixelType, MovingPixelType> MovingExtractType;
145 
146  // points retrieving
147  typename PointsContainer::ConstPointer points = pointSet->GetPoints();
148 
149  // Iterator set up
150  PointsIterator pointIterator = points->Begin();
151  PointsIterator end = points->End();
152  unsigned int dataId = 0;
153 
154  otbMsgDevMacro(<< "Starting registration");
155 
157  while (pointIterator != end)
158  {
159  typename PointSetType::PointType p = pointIterator.Value(); // access the point
160 
161  // Extract the needed sub-images
162  typename FixedExtractType::Pointer fixedExtractor = FixedExtractType::New();
163  typename MovingExtractType::Pointer movingExtractor = MovingExtractType::New();
164  fixedExtractor->SetInput(fixed);
165  movingExtractor->SetInput(moving);
166 
167  // Fixed extractor setup
168  fixedExtractor->SetStartX(static_cast<unsigned long>(p[0] - m_ExploSize[0]));
169  fixedExtractor->SetStartY(static_cast<unsigned long>(p[1] - m_ExploSize[1]));
170  fixedExtractor->SetSizeX(static_cast<unsigned long>(2 * m_ExploSize[0] + 1));
171  fixedExtractor->SetSizeY(static_cast<unsigned long>(2 * m_ExploSize[1] + 1));
172  otbMsgDevMacro(<< "Point id: " << dataId);
174  << "Fixed region: origin(" << p[0] - m_ExploSize[0] << ", " << p[1] - m_ExploSize[1] << ") size(" << 2 *
175  m_ExploSize[0] + 1 << ", " << 2 * m_ExploSize[1] + 1 << ")");
176  // Moving extractor setup
177  movingExtractor->SetStartX(static_cast<unsigned long>(p[0] - m_WinSize[0]));
178  movingExtractor->SetStartY(static_cast<unsigned long>(p[1] - m_WinSize[1]));
179  movingExtractor->SetSizeX(static_cast<unsigned long>(2 * m_WinSize[0] + 1));
180  movingExtractor->SetSizeY(static_cast<unsigned long>(2 * m_WinSize[1] + 1));
182  << "Moving region: origin(" << p[0] - m_WinSize[0] << ", " << p[1] - m_WinSize[1] << ") size(" << 2 *
183  m_WinSize[0] + 1 << ", " << 2 * m_WinSize[1] + 1 << ")");
184  // update the extractors
185  fixedExtractor->Update();
186  movingExtractor->Update();
187 
188 // std::cout<<"Fixed extract origin: "<<fixedExtractor->GetOutput()->GetOrigin()<<std::endl;
189 // std::cout<<"Fixed extract spacing: "<<fixedExtractor->GetOutput()->GetSpacing()<<std::endl;
190 // std::cout<<"Moving extract origin: "<<movingExtractor->GetOutput()->GetOrigin()<<std::endl;
191 // std::cout<<"Moving extract spacing: "<<movingExtractor->GetOutput()->GetSpacing()<<std::endl;
192 
193 // typedef otb::ImageFileWriter<FixedImageType> FixedWriterType;
194 // typedef otb::ImageFileWriter<MovingImageType> MovingWriterType;
195 
196 // typename FixedWriterType::Pointer fwriter = FixedWriterType::New();
197 // typename MovingWriterType::Pointer mwriter = MovingWriterType::New();
198 
199 // std::ostringstream oss;
200 // oss.str("");
201 // oss<<"Fixed"<<dataId<<".tif";
202 // fwriter->SetInput(fixedExtractor->GetOutput());
203 // fwriter->SetFileName(oss.str());
204 // fwriter->Update();
205 
206 // oss.str("");
207 // oss<<"Moving"<<dataId<<".tif";
208 // mwriter->SetInput(movingExtractor->GetOutput());
209 // mwriter->SetFileName(oss.str());
210 // mwriter->Update();
211 
212  // Registration filter definition
213  typename RegistrationType::Pointer registration = RegistrationType::New();
214 
215  // Registration filter setup
216  registration->SetOptimizer(m_Optimizer);
217  registration->SetTransform(m_Transform);
218  registration->SetInterpolator(m_Interpolator);
219  registration->SetMetric(m_Metric);
220  registration->SetFixedImage(fixedExtractor->GetOutput());
221  registration->SetMovingImage(movingExtractor->GetOutput());
222 
223  // initial transform parameters setup
224  registration->SetInitialTransformParameters(m_InitialTransformParameters);
225  m_Interpolator->SetInputImage(movingExtractor->GetOutput());
226 
227  // Perform the registration
228  registration->Update();
229 
230  // Retrieve the final parameters
231  ParametersType finalParameters = registration->GetLastTransformParameters();
232  double value = m_Optimizer->GetValue(registration->GetLastTransformParameters());
233 
234  // Computing moving image point
235  typename FixedImageType::PointType inputPoint, outputPoint;
236  typename FixedImageType::IndexType inputIndex;
237 
238  // ensure that we have the right coord rep type
239  inputIndex[0] = static_cast<unsigned int>(p[0]);
240  inputIndex[1] = static_cast<unsigned int>(p[1]);
241 
242  fixed->TransformIndexToPhysicalPoint(inputIndex, inputPoint);
243 
244  m_Transform->SetParameters(finalParameters);
245  outputPoint = m_Transform->TransformPoint(inputPoint);
246 
247  otbMsgDevMacro(<< "Metric value: " << value);
248  otbMsgDevMacro(<< "Transform parameters: " << finalParameters);
250  << "Displacement: (" << outputPoint[0] - inputPoint[0] << ", " << outputPoint[1] - inputPoint[1] << ")");
251  otbMsgDevMacro(<< "Final parameters: " << finalParameters);
252 
253  ParametersType data(finalParameters.GetSize() + 3);
254 
255  data[0] = value;
256  data[1] = outputPoint[0] - inputPoint[0];
257  data[2] = outputPoint[1] - inputPoint[1];
258 
259  for (unsigned int i = 0; i < finalParameters.GetSize(); ++i)
260  {
261  data[i + 3] = finalParameters[i];
262  }
263 
264  // Set the parameters value in the point set data container.
265  output->SetPoint(dataId, p);
266  output->SetPointData(dataId, data);
267  // otbMsgDevMacro(<<"Point "<<dataId<<": "<<finalParameters);
268  ++pointIterator; // advance to next point
269  ++dataId;
270  }
271 }
272 template <class TFixedImage, class TMovingImage, class TPointSet>
273 void
275 ::PrintSelf(std::ostream& os, itk::Indent indent) const
276 {
277  Superclass::PrintSelf(os, indent);
278  os << indent << "Window size: " << m_WinSize << std::endl;
279  os << indent << "Exploration size: " << m_ExploSize << std::endl;
280 }
281 }
282 #endif

Generated at Sat Mar 8 2014 15:53:44 for Orfeo Toolbox with doxygen 1.8.3.1