Orfeo Toolbox  3.16
otbPixelWiseBlockMatchingImageFilter.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 __otbPixelWiseBlockMatchingImageFilter_h
19 #define __otbPixelWiseBlockMatchingImageFilter_h
20 
21 
22 #include "itkImageToImageFilter.h"
25 #include "itkImageRegionIterator.h"
26 #include "otbImage.h"
27 
28 namespace otb
29 {
30 
31 namespace Functor
32 {
42 template <class TInputImage, class TOutputMetricImage>
44 {
45 public:
47  typedef typename TOutputMetricImage::ValueType MetricValueType;
48 
49  // Implement the SSD operator
51  {
52  MetricValueType ssd = 0;
53 
54  // For some reason, iterators do not work on neighborhoods
55  for(unsigned int i = 0; i<a.Size(); ++i)
56  {
57  ssd += (a.GetPixel(i)-b.GetPixel(i))*(a.GetPixel(i)-b.GetPixel(i));
58  }
59 
60  return ssd;
61  }
62 };
72 template <class TInputImage, class TOutputMetricImage>
74 {
75 public:
77  typedef typename TOutputMetricImage::ValueType MetricValueType;
78 
79  // Implement the NCC operator
81  {
82  //MetricValueType meanA(0),meanB(0), sigmaA(0), sigmaB(0), cov(0), ncc(0);
83  double meanA=0.0;
84  double meanB=0.0;
85  double sigmaA=0.0;
86  double sigmaB=0.0;
87  double cov=0.0;
88  double ncc=0.0;
89  double valueA;
90  double valueB;
91  double size=a.Size();
92  // For some reason, iterators do not work on neighborhoods
93  for(unsigned int i = 0; i<size; ++i)
94  {
95  meanA+=static_cast<double>(a.GetPixel(i));
96  meanB+=static_cast<double>(b.GetPixel(i));
97  }
98 
99  // Compute mean
100  meanA/=size;
101  meanB/=size;
102 
103  for(unsigned int i = 0; i<size; ++i)
104  {
105  valueA=static_cast<double>(a.GetPixel(i));
106  valueB=static_cast<double>(b.GetPixel(i));
107  cov+=(valueA-meanA)*(valueB-meanB);
108  sigmaA+=(valueA-meanA)*(valueA-meanA);
109  sigmaB+=(valueB-meanB)*(valueB-meanB);
110  }
111 
112  cov/=size-1;
113  sigmaA/=size-1;
114  sigmaB/=size-1;
115  sigmaA = vcl_sqrt(sigmaA);
116  sigmaB = vcl_sqrt(sigmaB);
117 
118  if(sigmaA > 1e-20 && sigmaB > 1e-20)
119  {
120  ncc = vcl_abs(cov)/(sigmaA*sigmaB);
121  }
122  else
123  {
124  ncc = 0;
125  }
126 
127  return static_cast<MetricValueType>(ncc);
128  }
129 };
130 
140 template <class TInputImage, class TOutputMetricImage>
142 {
143 public:
145  typedef typename TOutputMetricImage::ValueType MetricValueType;
146 
148  {
149  }
150 
151  void SetP(double p)
152  {
153  if (p > 0.0)
154  {
155  m_P = p;
156  }
157  else
158  {
159  m_P = 1.0;
160  }
161  }
162 
163  // Implement the Lp metric
165  {
166  MetricValueType score(0);
167 
168  // For some reason, iterators do not work on neighborhoods
169  for(unsigned int i = 0; i<a.Size(); ++i)
170  {
171  score += vcl_pow( vcl_abs(static_cast<double>(a.GetPixel(i)-b.GetPixel(i))) , m_P);
172  }
173 
174  return score;
175  }
176 
177 private:
178 
179  double m_P;
180 };
181 
182 } // End Namespace Functor
183 
241 template <class TInputImage, class TOutputMetricImage, class TOutputDisparityImage = TOutputMetricImage, class TMaskImage = otb::Image<unsigned char>,
242  class TBlockMatchingFunctor = Functor::SSDBlockMatching<TInputImage,TOutputMetricImage> >
244  public itk::ImageToImageFilter<TInputImage,TOutputDisparityImage>
245 {
246 public:
249  typedef itk::ImageToImageFilter<TInputImage,
250  TOutputDisparityImage> Superclass;
253 
255  itkNewMacro(Self);
256 
258  itkTypeMacro(PixelWiseBlockMatchingImageFilter, ImageToImageFilter);
259 
261  typedef TInputImage InputImageType;
262  typedef TOutputMetricImage OutputMetricImageType;
263  typedef TOutputDisparityImage OutputDisparityImageType;
264  typedef TMaskImage InputMaskImageType;
265  typedef TBlockMatchingFunctor BlockMatchingFunctorType;
266 
267  typedef typename InputImageType::SizeType SizeType;
268  typedef typename InputImageType::IndexType IndexType;
269  typedef typename InputImageType::RegionType RegionType;
270 
271  typedef typename TOutputMetricImage::ValueType MetricValueType;
272 
274 
276  void SetLeftInput( const TInputImage * image);
277 
279  void SetRightInput( const TInputImage * image);
280 
282  void SetLeftMaskInput(const TMaskImage * image);
283 
285  void SetRightMaskInput(const TMaskImage * image);
286 
288  const TInputImage * GetLeftInput() const;
289  const TInputImage * GetRightInput() const;
290  const TMaskImage * GetLeftMaskInput() const;
291  const TMaskImage * GetRightMaskInput() const;
292 
294  const TOutputMetricImage * GetMetricOutput() const;
295  TOutputMetricImage * GetMetricOutput();
296 
298  const TOutputDisparityImage * GetHorizontalDisparityOutput() const;
299  TOutputDisparityImage * GetHorizontalDisparityOutput();
300 
302  const TOutputDisparityImage * GetVerticalDisparityOutput() const;
303  TOutputDisparityImage * GetVerticalDisparityOutput();
304 
305 
307  void SetRadius(unsigned int radius)
308  {
309  m_Radius.Fill(radius);
310  }
311 
313  itkSetMacro(Radius, SizeType);
314  itkGetConstReferenceMacro(Radius, SizeType);
315 
316  /*** Set/Get the minimum disparity to explore */
317  itkSetMacro(MinimumHorizontalDisparity,int);
318  itkGetConstReferenceMacro(MinimumHorizontalDisparity,int);
319 
320  /*** Set/Get the maximum disparity to explore */
321  itkSetMacro(MaximumHorizontalDisparity,int);
322  itkGetConstReferenceMacro(MaximumHorizontalDisparity,int);
323 
324  /*** Set/Get the minimum disparity to explore */
325  itkSetMacro(MinimumVerticalDisparity,int);
326  itkGetConstReferenceMacro(MinimumVerticalDisparity,int);
327 
328  /*** Set/Get the maximum disparity to explore */
329  itkSetMacro(MaximumVerticalDisparity,int);
330  itkGetConstReferenceMacro(MaximumVerticalDisparity,int);
331 
332  itkSetMacro(Minimize, bool);
333  itkGetConstReferenceMacro(Minimize,bool);
334  itkBooleanMacro(Minimize);
335 
337  itkSetMacro(ExplorationRadius, SizeType);
338  itkGetConstReferenceMacro(ExplorationRadius, SizeType);
339 
341  itkSetMacro(InitHorizontalDisparity,int);
342  itkGetConstReferenceMacro(InitHorizontalDisparity,int);
343 
345  itkSetMacro(InitVerticalDisparity,int);
346  itkGetConstReferenceMacro(InitVerticalDisparity,int);
347 
350  {
351  return m_Functor;
352  }
353 
355  const BlockMatchingFunctorType & GetFunctor() const
356  {
357  return m_Functor;
358  }
359 
361  void SetHorizontalDisparityInput( const TOutputDisparityImage * hfield);
362 
364  void SetVerticalDisparityInput( const TOutputDisparityImage * vfield);
365 
367  const TOutputDisparityImage * GetHorizontalDisparityInput() const;
368  const TOutputDisparityImage * GetVerticalDisparityInput() const;
369 
370 protected:
373 
376 
378  virtual void GenerateInputRequestedRegion();
379 
381  virtual void BeforeThreadedGenerateData();
382 
384  virtual void ThreadedGenerateData(const RegionType & outputRegionForThread, int threadId);
385 
386 private:
387  PixelWiseBlockMatchingImageFilter(const Self&); //purposely not implemented
388  void operator=(const Self&); //purposely not implemeFnted
389 
392 
395 
398 
401 
404 
407 
410 
413 
417 
421 };
422 } // end namespace otb
423 
424 #ifndef OTB_MANUAL_INSTANTIATION
426 #endif
427 
428 #endif

Generated at Sun Feb 3 2013 00:41:58 for Orfeo Toolbox with doxygen 1.8.1.1