Orfeo Toolbox  4.0
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>
43 ITK_EXPORT class SSDBlockMatching
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 };
63 
64 
74 template <class TInputImage, class TOutputMetricImage>
75 ITK_EXPORT class SSDDivMeanBlockMatching
76 {
77 public:
79  typedef typename TOutputMetricImage::ValueType MetricValueType;
80 
81  // Implement the SSD DivMean operator
83  {
84  MetricValueType ssd = 0;
85  MetricValueType meana = 0;
86  MetricValueType meanb = 0;
87 
88  for(unsigned int i = 0; i<a.Size(); ++i)
89  {
90  meana+=a.GetPixel(i);
91  meanb+=b.GetPixel(i);
92  }
93  meana/=a.Size();
94  meanb/=b.Size();
95 
96  // For some reason, iterators do not work on neighborhoods
97  for(unsigned int i = 0; i<a.Size(); ++i)
98  {
99  ssd += (a.GetPixel(i)/meana-b.GetPixel(i)/meanb)*(a.GetPixel(i)/meana-b.GetPixel(i)/meanb);
100 
101  }
102 
103  return ssd;
104  }
105 };
106 
107 
117 template <class TInputImage, class TOutputMetricImage>
118 ITK_EXPORT class NCCBlockMatching
119 {
120 public:
122  typedef typename TOutputMetricImage::ValueType MetricValueType;
123 
124  // Implement the NCC operator
126  {
127  //MetricValueType meanA(0),meanB(0), sigmaA(0), sigmaB(0), cov(0), ncc(0);
128  double meanA=0.0;
129  double meanB=0.0;
130  double sigmaA=0.0;
131  double sigmaB=0.0;
132  double cov=0.0;
133  double ncc=0.0;
134  double valueA;
135  double valueB;
136  double size=a.Size();
137  // For some reason, iterators do not work on neighborhoods
138  for(unsigned int i = 0; i<size; ++i)
139  {
140  meanA+=static_cast<double>(a.GetPixel(i));
141  meanB+=static_cast<double>(b.GetPixel(i));
142  }
143 
144  // Compute mean
145  meanA/=size;
146  meanB/=size;
147 
148  for(unsigned int i = 0; i<size; ++i)
149  {
150  valueA=static_cast<double>(a.GetPixel(i));
151  valueB=static_cast<double>(b.GetPixel(i));
152  cov+=(valueA-meanA)*(valueB-meanB);
153  sigmaA+=(valueA-meanA)*(valueA-meanA);
154  sigmaB+=(valueB-meanB)*(valueB-meanB);
155  }
156 
157  cov/=size-1;
158  sigmaA/=size-1;
159  sigmaB/=size-1;
160  sigmaA = vcl_sqrt(sigmaA);
161  sigmaB = vcl_sqrt(sigmaB);
162 
163  if(sigmaA > 1e-20 && sigmaB > 1e-20)
164  {
165  ncc = vcl_abs(cov)/(sigmaA*sigmaB);
166  }
167  else
168  {
169  ncc = 0;
170  }
171 
172  return static_cast<MetricValueType>(ncc);
173  }
174 };
175 
185 template <class TInputImage, class TOutputMetricImage>
186 ITK_EXPORT class LPBlockMatching
187 {
188 public:
190  typedef typename TOutputMetricImage::ValueType MetricValueType;
191 
193  {
194  }
195 
196  void SetP(double p)
197  {
198  if (p > 0.0)
199  {
200  m_P = p;
201  }
202  else
203  {
204  m_P = 1.0;
205  }
206  }
207 
208  // Implement the Lp metric
210  {
211  MetricValueType score(0);
212 
213  // For some reason, iterators do not work on neighborhoods
214  for(unsigned int i = 0; i<a.Size(); ++i)
215  {
216  score += vcl_pow( vcl_abs(static_cast<double>(a.GetPixel(i)-b.GetPixel(i))) , m_P);
217  }
218 
219  return score;
220  }
221 
222 private:
223 
224  double m_P;
225 };
226 
227 } // End Namespace Functor
228 
286 template <class TInputImage, class TOutputMetricImage, class TOutputDisparityImage = TOutputMetricImage, class TMaskImage = otb::Image<unsigned char>,
287  class TBlockMatchingFunctor = Functor::SSDBlockMatching<TInputImage,TOutputMetricImage> >
289  public itk::ImageToImageFilter<TInputImage,TOutputDisparityImage>
290 {
291 public:
294  typedef itk::ImageToImageFilter<TInputImage,
295  TOutputDisparityImage> Superclass;
298 
300  itkNewMacro(Self);
301 
303  itkTypeMacro(PixelWiseBlockMatchingImageFilter, ImageToImageFilter);
304 
306  typedef TInputImage InputImageType;
307  typedef TOutputMetricImage OutputMetricImageType;
308  typedef TOutputDisparityImage OutputDisparityImageType;
309  typedef TMaskImage InputMaskImageType;
310  typedef TBlockMatchingFunctor BlockMatchingFunctorType;
311 
312  typedef typename InputImageType::SizeType SizeType;
313  typedef typename InputImageType::IndexType IndexType;
314  typedef typename InputImageType::RegionType RegionType;
315  typedef typename InputImageType::SpacingType SpacingType;
316  typedef typename InputImageType::PointType PointType;
317 
318  typedef typename TOutputMetricImage::ValueType MetricValueType;
319 
320  typedef typename OutputDisparityImageType::PixelType DisparityPixelType;
321 
323 
325  void SetLeftInput( const TInputImage * image);
326 
328  void SetRightInput( const TInputImage * image);
329 
331  void SetLeftMaskInput(const TMaskImage * image);
332 
334  void SetRightMaskInput(const TMaskImage * image);
335 
337  const TInputImage * GetLeftInput() const;
338  const TInputImage * GetRightInput() const;
339  const TMaskImage * GetLeftMaskInput() const;
340  const TMaskImage * GetRightMaskInput() const;
341 
343  const TOutputMetricImage * GetMetricOutput() const;
344  TOutputMetricImage * GetMetricOutput();
345 
347  const TOutputDisparityImage * GetHorizontalDisparityOutput() const;
348  TOutputDisparityImage * GetHorizontalDisparityOutput();
349 
351  const TOutputDisparityImage * GetVerticalDisparityOutput() const;
352  TOutputDisparityImage * GetVerticalDisparityOutput();
353 
354 
356  void SetRadius(unsigned int radius)
357  {
358  m_Radius.Fill(radius);
359  }
360 
362  itkSetMacro(Radius, SizeType);
363  itkGetConstReferenceMacro(Radius, SizeType);
364 
365  /*** Set/Get the minimum disparity to explore */
366  itkSetMacro(MinimumHorizontalDisparity,int);
367  itkGetConstReferenceMacro(MinimumHorizontalDisparity,int);
368 
369  /*** Set/Get the maximum disparity to explore */
370  itkSetMacro(MaximumHorizontalDisparity,int);
371  itkGetConstReferenceMacro(MaximumHorizontalDisparity,int);
372 
373  /*** Set/Get the minimum disparity to explore */
374  itkSetMacro(MinimumVerticalDisparity,int);
375  itkGetConstReferenceMacro(MinimumVerticalDisparity,int);
376 
377  /*** Set/Get the maximum disparity to explore */
378  itkSetMacro(MaximumVerticalDisparity,int);
379  itkGetConstReferenceMacro(MaximumVerticalDisparity,int);
380 
381  itkSetMacro(Minimize, bool);
382  itkGetConstReferenceMacro(Minimize,bool);
383  itkBooleanMacro(Minimize);
384 
386  itkSetMacro(ExplorationRadius, SizeType);
387  itkGetConstReferenceMacro(ExplorationRadius, SizeType);
388 
390  itkSetMacro(InitHorizontalDisparity,int);
391  itkGetConstReferenceMacro(InitHorizontalDisparity,int);
392 
394  itkSetMacro(InitVerticalDisparity,int);
395  itkGetConstReferenceMacro(InitVerticalDisparity,int);
396 
399  {
400  return m_Functor;
401  }
402 
404  const BlockMatchingFunctorType & GetFunctor() const
405  {
406  return m_Functor;
407  }
408 
410  void SetHorizontalDisparityInput( const TOutputDisparityImage * hfield);
411 
413  void SetVerticalDisparityInput( const TOutputDisparityImage * vfield);
414 
416  const TOutputDisparityImage * GetHorizontalDisparityInput() const;
417  const TOutputDisparityImage * GetVerticalDisparityInput() const;
418 
420  itkSetMacro(Step, unsigned int);
421  itkGetMacro(Step, unsigned int);
422 
424  itkSetMacro(GridIndex, IndexType);
425  itkGetConstReferenceMacro(GridIndex, IndexType);
426 
428  static RegionType ConvertFullToSubsampledRegion(RegionType full, unsigned int step, IndexType index);
429 
431  static RegionType ConvertSubsampledToFullRegion(RegionType sub, unsigned int step, IndexType index);
432 
433 protected:
436 
439 
441  virtual void GenerateOutputInformation();
442 
444  virtual void GenerateInputRequestedRegion();
445 
447  virtual void BeforeThreadedGenerateData();
448 
450  virtual void ThreadedGenerateData(const RegionType & outputRegionForThread, itk::ThreadIdType threadId);
451 
452 private:
453  PixelWiseBlockMatchingImageFilter(const Self&); //purposely not implemented
454  void operator=(const Self&); //purposely not implemeFnted
455 
458 
461 
464 
467 
470 
473 
476 
479 
483 
487 
489  unsigned int m_Step;
490 
495 
496 };
497 } // end namespace otb
498 
499 #ifndef OTB_MANUAL_INSTANTIATION
501 #endif
502 
503 #endif

Generated at Sat Mar 8 2014 16:13:12 for Orfeo Toolbox with doxygen 1.8.3.1