OTB  9.0.0
Orfeo Toolbox
otbPixelWiseBlockMatchingImageFilter.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2005-2022 Centre National d'Etudes Spatiales (CNES)
3  *
4  * This file is part of Orfeo Toolbox
5  *
6  * https://www.orfeo-toolbox.org/
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  */
20 
21 #ifndef otbPixelWiseBlockMatchingImageFilter_h
22 #define otbPixelWiseBlockMatchingImageFilter_h
23 
24 
25 #include "itkImageToImageFilter.h"
26 #include "itkConstNeighborhoodIterator.h"
27 #include "itkImageRegionIterator.h"
28 #include "otbImage.h"
29 
30 namespace otb
31 {
32 
33 namespace Functor
34 {
46 template <class TInputImage, class TOutputMetricImage>
47 ITK_EXPORT class SSDBlockMatching
48 {
49 public:
50  typedef itk::ConstNeighborhoodIterator<TInputImage> ConstNeighborhoodIteratorType;
51  typedef typename TOutputMetricImage::ValueType MetricValueType;
52 
53  // Implement the SSD operator
55  {
56  MetricValueType ssd = 0;
57 
58  // For some reason, iterators do not work on neighborhoods
59  for (unsigned int i = 0; i < a.Size(); ++i)
60  {
61  ssd += (a.GetPixel(i) - b.GetPixel(i)) * (a.GetPixel(i) - b.GetPixel(i));
62  }
63 
64  return ssd;
65  }
66 };
67 
68 
80 template <class TInputImage, class TOutputMetricImage>
81 ITK_EXPORT class SSDDivMeanBlockMatching
82 {
83 public:
84  typedef itk::ConstNeighborhoodIterator<TInputImage> ConstNeighborhoodIteratorType;
85  typedef typename TOutputMetricImage::ValueType MetricValueType;
86 
87  // Implement the SSD DivMean operator
89  {
90  MetricValueType ssd = 0;
91  MetricValueType meana = 0;
92  MetricValueType meanb = 0;
93 
94  for (unsigned int i = 0; i < a.Size(); ++i)
95  {
96  meana += a.GetPixel(i);
97  meanb += b.GetPixel(i);
98  }
99  meana /= a.Size();
100  meanb /= b.Size();
101 
102  // For some reason, iterators do not work on neighborhoods
103  for (unsigned int i = 0; i < a.Size(); ++i)
104  {
105  ssd += (a.GetPixel(i) / meana - b.GetPixel(i) / meanb) * (a.GetPixel(i) / meana - b.GetPixel(i) / meanb);
106  }
107 
108  return ssd;
109  }
110 };
111 
112 
124 template <class TInputImage, class TOutputMetricImage>
125 ITK_EXPORT class NCCBlockMatching
126 {
127 public:
128  typedef itk::ConstNeighborhoodIterator<TInputImage> ConstNeighborhoodIteratorType;
129  typedef typename TOutputMetricImage::ValueType MetricValueType;
130 
131  // Implement the NCC operator
133  {
134  // MetricValueType meanA(0),meanB(0), sigmaA(0), sigmaB(0), cov(0), ncc(0);
135  double meanA = 0.0;
136  double meanB = 0.0;
137  double sigmaA = 0.0;
138  double sigmaB = 0.0;
139  double cov = 0.0;
140  double ncc = 0.0;
141  double valueA;
142  double valueB;
143  double size = a.Size();
144  // For some reason, iterators do not work on neighborhoods
145  for (unsigned int i = 0; i < size; ++i)
146  {
147  meanA += static_cast<double>(a.GetPixel(i));
148  meanB += static_cast<double>(b.GetPixel(i));
149  }
150 
151  // Compute mean
152  meanA /= size;
153  meanB /= size;
154 
155  for (unsigned int i = 0; i < size; ++i)
156  {
157  valueA = static_cast<double>(a.GetPixel(i));
158  valueB = static_cast<double>(b.GetPixel(i));
159  cov += (valueA - meanA) * (valueB - meanB);
160  sigmaA += (valueA - meanA) * (valueA - meanA);
161  sigmaB += (valueB - meanB) * (valueB - meanB);
162  }
163 
164  cov /= size - 1;
165  sigmaA /= size - 1;
166  sigmaB /= size - 1;
167  sigmaA = std::sqrt(sigmaA);
168  sigmaB = std::sqrt(sigmaB);
169 
170  if (sigmaA > 1e-20 && sigmaB > 1e-20)
171  {
172  ncc = std::abs(cov) / (sigmaA * sigmaB);
173  }
174  else
175  {
176  ncc = 0;
177  }
178 
179  return static_cast<MetricValueType>(ncc);
180  }
181 };
182 
194 template <class TInputImage, class TOutputMetricImage>
195 ITK_EXPORT class LPBlockMatching
196 {
197 public:
198  typedef itk::ConstNeighborhoodIterator<TInputImage> ConstNeighborhoodIteratorType;
199  typedef typename TOutputMetricImage::ValueType MetricValueType;
200 
202  {
203  }
204 
205  void SetP(double p)
206  {
207  if (p > 0.0)
208  {
209  m_P = p;
210  }
211  else
212  {
213  m_P = 1.0;
214  }
215  }
216 
217  // Implement the Lp metric
219  {
220  MetricValueType score(0);
221 
222  // For some reason, iterators do not work on neighborhoods
223  for (unsigned int i = 0; i < a.Size(); ++i)
224  {
225  score += std::pow(std::abs(static_cast<double>(a.GetPixel(i) - b.GetPixel(i))), m_P);
226  }
227 
228  return score;
229  }
230 
231 private:
232  double m_P;
233 };
234 
235 } // End Namespace Functor
236 
296 template <class TInputImage, class TOutputMetricImage, class TOutputDisparityImage = TOutputMetricImage, class TMaskImage = otb::Image<unsigned char>,
297  class TBlockMatchingFunctor = Functor::SSDBlockMatching<TInputImage, TOutputMetricImage>>
298 class ITK_EXPORT PixelWiseBlockMatchingImageFilter : public itk::ImageToImageFilter<TInputImage, TOutputDisparityImage>
299 {
300 public:
303  typedef itk::ImageToImageFilter<TInputImage, TOutputDisparityImage> Superclass;
304  typedef itk::SmartPointer<Self> Pointer;
305  typedef itk::SmartPointer<const Self> ConstPointer;
306 
308  itkNewMacro(Self);
309 
311  itkTypeMacro(PixelWiseBlockMatchingImageFilter, ImageToImageFilter);
312 
314  typedef TInputImage InputImageType;
315  typedef TOutputMetricImage OutputMetricImageType;
316  typedef TOutputDisparityImage OutputDisparityImageType;
317  typedef TMaskImage InputMaskImageType;
318  typedef TBlockMatchingFunctor BlockMatchingFunctorType;
319 
320  typedef typename InputImageType::SizeType SizeType;
321  typedef typename InputImageType::IndexType IndexType;
322  typedef typename InputImageType::RegionType RegionType;
323  typedef typename InputImageType::SpacingType SpacingType;
324  typedef typename InputImageType::PointType PointType;
325 
326  typedef typename TOutputMetricImage::ValueType MetricValueType;
327 
328  typedef typename OutputDisparityImageType::PixelType DisparityPixelType;
329 
330  typedef itk::ConstNeighborhoodIterator<TInputImage> ConstNeighborhoodIteratorType;
331 
333  void SetLeftInput(const TInputImage* image);
334 
336  void SetRightInput(const TInputImage* image);
337 
339  void SetLeftMaskInput(const TMaskImage* image);
340 
342  void SetRightMaskInput(const TMaskImage* image);
343 
345  const TInputImage* GetLeftInput() const;
346  const TInputImage* GetRightInput() const;
347  const TMaskImage* GetLeftMaskInput() const;
348  const TMaskImage* GetRightMaskInput() const;
350 
352  const TOutputMetricImage* GetMetricOutput() const;
353  TOutputMetricImage* GetMetricOutput();
355 
357  const TOutputDisparityImage* GetHorizontalDisparityOutput() const;
358  TOutputDisparityImage* GetHorizontalDisparityOutput();
360 
362  const TOutputDisparityImage* GetVerticalDisparityOutput() const;
363  TOutputDisparityImage* GetVerticalDisparityOutput();
365 
366 
368  void SetRadius(unsigned int radius)
369  {
370  m_Radius.Fill(radius);
371  }
372 
374  itkSetMacro(Radius, SizeType);
375  itkGetConstReferenceMacro(Radius, SizeType);
377 
379  itkSetMacro(MinimumHorizontalDisparity, int);
380  itkGetConstReferenceMacro(MinimumHorizontalDisparity, int);
382 
384  itkSetMacro(MaximumHorizontalDisparity, int);
385  itkGetConstReferenceMacro(MaximumHorizontalDisparity, int);
387 
389  itkSetMacro(MinimumVerticalDisparity, int);
390  itkGetConstReferenceMacro(MinimumVerticalDisparity, int);
392 
394  itkSetMacro(MaximumVerticalDisparity, int);
395  itkGetConstReferenceMacro(MaximumVerticalDisparity, int);
397 
398  itkSetMacro(Minimize, bool);
399  itkGetConstReferenceMacro(Minimize, bool);
400  itkBooleanMacro(Minimize);
401 
403  itkSetMacro(ExplorationRadius, SizeType);
404  itkGetConstReferenceMacro(ExplorationRadius, SizeType);
406 
408  itkSetMacro(InitHorizontalDisparity, int);
409  itkGetConstReferenceMacro(InitHorizontalDisparity, int);
411 
413  itkSetMacro(InitVerticalDisparity, int);
414  itkGetConstReferenceMacro(InitVerticalDisparity, int);
416 
419  {
420  return m_Functor;
421  }
422 
425  {
426  return m_Functor;
427  }
428 
430  void SetHorizontalDisparityInput(const TOutputDisparityImage* hfield);
431 
433  void SetVerticalDisparityInput(const TOutputDisparityImage* vfield);
434 
436  const TOutputDisparityImage* GetHorizontalDisparityInput() const;
437  const TOutputDisparityImage* GetVerticalDisparityInput() const;
439 
441  itkSetMacro(Step, unsigned int);
442  itkGetMacro(Step, unsigned int);
444 
446  itkSetMacro(GridIndex, IndexType);
447  itkGetConstReferenceMacro(GridIndex, IndexType);
449 
451  static RegionType ConvertFullToSubsampledRegion(RegionType full, unsigned int step, IndexType index);
452 
454  static RegionType ConvertSubsampledToFullRegion(RegionType sub, unsigned int step, IndexType index);
455 
456 protected:
459 
462 
464  void GenerateOutputInformation() override;
465 
467  void GenerateInputRequestedRegion() override;
468 
470  void BeforeThreadedGenerateData() override;
471 
473  void ThreadedGenerateData(const RegionType& outputRegionForThread, itk::ThreadIdType threadId) override;
474 
475 private:
476  PixelWiseBlockMatchingImageFilter(const Self&) = delete;
477  void operator =(const Self&); // purposely not implemeFnted
478 
481 
484 
487 
490 
493 
496 
499 
502 
506 
510 
512  unsigned int m_Step;
513 
518 };
519 } // end namespace otb
520 
521 #ifndef OTB_MANUAL_INSTANTIATION
523 #endif
524 
525 #endif
otb::Functor::SSDDivMeanBlockMatching::operator()
MetricValueType operator()(ConstNeighborhoodIteratorType &a, ConstNeighborhoodIteratorType &b) const
Definition: otbPixelWiseBlockMatchingImageFilter.h:88
otb::Functor::LPBlockMatching::SetP
void SetP(double p)
Definition: otbPixelWiseBlockMatchingImageFilter.h:205
otb::PixelWiseBlockMatchingImageFilter::InputMaskImageType
TMaskImage InputMaskImageType
Definition: otbPixelWiseBlockMatchingImageFilter.h:317
otb::PixelWiseBlockMatchingImageFilter::m_MaximumHorizontalDisparity
int m_MaximumHorizontalDisparity
Definition: otbPixelWiseBlockMatchingImageFilter.h:486
otb::PixelWiseBlockMatchingImageFilter::PointType
InputImageType::PointType PointType
Definition: otbPixelWiseBlockMatchingImageFilter.h:324
otbImage.h
otb::PixelWiseBlockMatchingImageFilter::m_MinimumHorizontalDisparity
int m_MinimumHorizontalDisparity
Definition: otbPixelWiseBlockMatchingImageFilter.h:483
otb::PixelWiseBlockMatchingImageFilter::Self
PixelWiseBlockMatchingImageFilter Self
Definition: otbPixelWiseBlockMatchingImageFilter.h:302
otb::Functor::SSDDivMeanBlockMatching::MetricValueType
TOutputMetricImage::ValueType MetricValueType
Definition: otbPixelWiseBlockMatchingImageFilter.h:85
otb::Functor::NCCBlockMatching::MetricValueType
TOutputMetricImage::ValueType MetricValueType
Definition: otbPixelWiseBlockMatchingImageFilter.h:129
otb::Functor::LPBlockMatching::LPBlockMatching
LPBlockMatching()
Definition: otbPixelWiseBlockMatchingImageFilter.h:201
otb
The "otb" namespace contains all Orfeo Toolbox (OTB) classes.
Definition: otbJoinContainer.h:32
otb::PixelWiseBlockMatchingImageFilter::m_Step
unsigned int m_Step
Definition: otbPixelWiseBlockMatchingImageFilter.h:512
otb::PixelWiseBlockMatchingImageFilter::BlockMatchingFunctorType
TBlockMatchingFunctor BlockMatchingFunctorType
Definition: otbPixelWiseBlockMatchingImageFilter.h:318
otb::PixelWiseBlockMatchingImageFilter::DisparityPixelType
OutputDisparityImageType::PixelType DisparityPixelType
Definition: otbPixelWiseBlockMatchingImageFilter.h:328
otb::Functor::LPBlockMatching::operator()
MetricValueType operator()(ConstNeighborhoodIteratorType &a, ConstNeighborhoodIteratorType &b) const
Definition: otbPixelWiseBlockMatchingImageFilter.h:218
otb::PixelWiseBlockMatchingImageFilter::m_MinimumVerticalDisparity
int m_MinimumVerticalDisparity
Definition: otbPixelWiseBlockMatchingImageFilter.h:489
otbPixelWiseBlockMatchingImageFilter.hxx
otb::PixelWiseBlockMatchingImageFilter::IndexType
InputImageType::IndexType IndexType
Definition: otbPixelWiseBlockMatchingImageFilter.h:321
otb::PixelWiseBlockMatchingImageFilter::GetFunctor
BlockMatchingFunctorType & GetFunctor()
Definition: otbPixelWiseBlockMatchingImageFilter.h:418
otb::PixelWiseBlockMatchingImageFilter::Superclass
itk::ImageToImageFilter< TInputImage, TOutputDisparityImage > Superclass
Definition: otbPixelWiseBlockMatchingImageFilter.h:303
otb::PixelWiseBlockMatchingImageFilter::m_MaximumVerticalDisparity
int m_MaximumVerticalDisparity
Definition: otbPixelWiseBlockMatchingImageFilter.h:492
otb::Functor::SSDDivMeanBlockMatching::ConstNeighborhoodIteratorType
itk::ConstNeighborhoodIterator< TInputImage > ConstNeighborhoodIteratorType
Definition: otbPixelWiseBlockMatchingImageFilter.h:84
otb::PixelWiseBlockMatchingImageFilter::m_GridIndex
IndexType m_GridIndex
Definition: otbPixelWiseBlockMatchingImageFilter.h:517
otb::PixelWiseBlockMatchingImageFilter::GetFunctor
const BlockMatchingFunctorType & GetFunctor() const
Definition: otbPixelWiseBlockMatchingImageFilter.h:424
otb::PixelWiseBlockMatchingImageFilter::m_Minimize
bool m_Minimize
Definition: otbPixelWiseBlockMatchingImageFilter.h:495
otb::Functor::NCCBlockMatching::ConstNeighborhoodIteratorType
itk::ConstNeighborhoodIterator< TInputImage > ConstNeighborhoodIteratorType
Definition: otbPixelWiseBlockMatchingImageFilter.h:128
otb::PixelWiseBlockMatchingImageFilter::m_InitHorizontalDisparity
int m_InitHorizontalDisparity
Definition: otbPixelWiseBlockMatchingImageFilter.h:505
otb::PixelWiseBlockMatchingImageFilter
Perform 2D block matching between two images.
Definition: otbPixelWiseBlockMatchingImageFilter.h:298
otb::PixelWiseBlockMatchingImageFilter::OutputDisparityImageType
TOutputDisparityImage OutputDisparityImageType
Definition: otbPixelWiseBlockMatchingImageFilter.h:316
otb::PixelWiseBlockMatchingImageFilter::OutputMetricImageType
TOutputMetricImage OutputMetricImageType
Definition: otbPixelWiseBlockMatchingImageFilter.h:315
otb::PixelWiseBlockMatchingImageFilter::RegionType
InputImageType::RegionType RegionType
Definition: otbPixelWiseBlockMatchingImageFilter.h:322
otb::PixelWiseBlockMatchingImageFilter::SizeType
InputImageType::SizeType SizeType
Definition: otbPixelWiseBlockMatchingImageFilter.h:320
otb::Functor::LPBlockMatching::m_P
double m_P
Definition: otbPixelWiseBlockMatchingImageFilter.h:232
otb::PixelWiseBlockMatchingImageFilter::SetRadius
void SetRadius(unsigned int radius)
Definition: otbPixelWiseBlockMatchingImageFilter.h:368
otb::Functor::LPBlockMatching::ConstNeighborhoodIteratorType
itk::ConstNeighborhoodIterator< TInputImage > ConstNeighborhoodIteratorType
Definition: otbPixelWiseBlockMatchingImageFilter.h:198
otb::Functor::SSDBlockMatching::ConstNeighborhoodIteratorType
itk::ConstNeighborhoodIterator< TInputImage > ConstNeighborhoodIteratorType
Definition: otbPixelWiseBlockMatchingImageFilter.h:50
otb::Functor::NCCBlockMatching
Functor to perform simple NCC block-matching.
Definition: otbPixelWiseBlockMatchingImageFilter.h:125
otb::Functor::LPBlockMatching
Functor to perform block-matching based on the L^p pseudo-norm.
Definition: otbPixelWiseBlockMatchingImageFilter.h:195
otb::PixelWiseBlockMatchingImageFilter::InputImageType
TInputImage InputImageType
Definition: otbPixelWiseBlockMatchingImageFilter.h:311
otb::PixelWiseBlockMatchingImageFilter::SpacingType
InputImageType::SpacingType SpacingType
Definition: otbPixelWiseBlockMatchingImageFilter.h:323
otb::Functor::SSDBlockMatching::MetricValueType
TOutputMetricImage::ValueType MetricValueType
Definition: otbPixelWiseBlockMatchingImageFilter.h:51
otb::Functor::SSDBlockMatching
Functor to perform simple SSD block-matching.
Definition: otbPixelWiseBlockMatchingImageFilter.h:47
otb::Functor::SSDBlockMatching::operator()
MetricValueType operator()(ConstNeighborhoodIteratorType &a, ConstNeighborhoodIteratorType &b) const
Definition: otbPixelWiseBlockMatchingImageFilter.h:54
otb::PixelWiseBlockMatchingImageFilter::m_Functor
BlockMatchingFunctorType m_Functor
Definition: otbPixelWiseBlockMatchingImageFilter.h:501
otb::PixelWiseBlockMatchingImageFilter::m_Radius
SizeType m_Radius
Definition: otbPixelWiseBlockMatchingImageFilter.h:480
otb::PixelWiseBlockMatchingImageFilter::ConstPointer
itk::SmartPointer< const Self > ConstPointer
Definition: otbPixelWiseBlockMatchingImageFilter.h:305
otb::Functor::SSDDivMeanBlockMatching
Functor to perform derived SSD block-matching (SSD divided by mean)
Definition: otbPixelWiseBlockMatchingImageFilter.h:81
otb::PixelWiseBlockMatchingImageFilter::MetricValueType
TOutputMetricImage::ValueType MetricValueType
Definition: otbPixelWiseBlockMatchingImageFilter.h:326
otb::Functor::NCCBlockMatching::operator()
MetricValueType operator()(ConstNeighborhoodIteratorType &a, ConstNeighborhoodIteratorType &b) const
Definition: otbPixelWiseBlockMatchingImageFilter.h:132
otb::PixelWiseBlockMatchingImageFilter::ConstNeighborhoodIteratorType
itk::ConstNeighborhoodIterator< TInputImage > ConstNeighborhoodIteratorType
Definition: otbPixelWiseBlockMatchingImageFilter.h:330
otb::PixelWiseBlockMatchingImageFilter::m_InitVerticalDisparity
int m_InitVerticalDisparity
Definition: otbPixelWiseBlockMatchingImageFilter.h:509
otb::Functor::LPBlockMatching::MetricValueType
TOutputMetricImage::ValueType MetricValueType
Definition: otbPixelWiseBlockMatchingImageFilter.h:199
otb::PixelWiseBlockMatchingImageFilter::m_ExplorationRadius
SizeType m_ExplorationRadius
Definition: otbPixelWiseBlockMatchingImageFilter.h:498
otb::PixelWiseBlockMatchingImageFilter::Pointer
itk::SmartPointer< Self > Pointer
Definition: otbPixelWiseBlockMatchingImageFilter.h:304