File: otbFastNLMeansImageFilter.h

package info (click to toggle)
otb 7.2.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 1,005,476 kB
  • sloc: cpp: 270,143; xml: 128,722; ansic: 4,367; sh: 1,768; python: 1,084; perl: 92; makefile: 72
file content (164 lines) | stat: -rw-r--r-- 6,645 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/*
 * Copyright (C) 2005-2020 Centre National d'Etudes Spatiales (CNES)
 *
 * This file is part of Orfeo Toolbox
 *
 *     https://www.orfeo-toolbox.org/
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef otbFastNLMeansImageFilter_h
#define otbFastNLMeansImageFilter_h

#include "itkImageToImageFilter.h"

namespace otb
{

/** \class NLMeansFilter
 * \brief This class implements a fast approximated version of NL Means denoising
 * algorithm. This implementation is based on code in scikit module skimage.
 * This fast version of the NL Means algorithm
 * has been described in the following papers :
 *
 *  J. Darbon, A. Cunha, T.F. Chan, S. Osher, and G.J. Jensen,
 * Fast nonlocal filtering applied to electron cryomicroscopy,
 * in 5th IEEE International Symposium on Biomedical Imaging: From Nano to Macro,
 * 2008, pp. 1331-1334.
 *
 * Jacques Froment.
 * Parameter-Free Fast Pixelwise Non-Local Means Denoising.
 * Image Processing On Line, 2014, vol. 4, p. 300-326.
 *
 * \ingroup OTBSmoothing
 */

template <class TInputImage, class TOutputImage>
class ITK_EXPORT NLMeansFilter : public itk::ImageToImageFilter<TInputImage, TOutputImage>
{
public:
  /** Standard class typedefs */
  typedef NLMeansFilter                                      Self;
  typedef itk::ImageToImageFilter<TInputImage, TOutputImage> Superclass;
  typedef itk::SmartPointer<Self>                            Pointer;
  typedef itk::SmartPointer<const Self>                      ConstPointer;

  /** Typedef to image types */
  typedef TInputImage  InImageType;
  typedef TOutputImage OutImageType;

  /** Typedef to describe the image pointer types. */
  typedef typename InImageType::Pointer      InImagePointerType;
  typedef typename InImageType::ConstPointer InImageConstPointerType;
  typedef typename InImageType::RegionType   InRegionType;
  typedef typename InImageType::IndexType    InIndexType;
  typedef typename InImageType::SizeType     InSizeType;
  typedef typename InImageType::OffsetType   InOffsetType;
  typedef typename OutImageType::Pointer     OutImagePointerType;
  typedef typename OutImageType::RegionType  OutRegionType;
  typedef typename OutImageType::PixelType   OutPixelType;
  typedef typename OutImageType::SizeType    OutSizeType;
  typedef typename OutImageType::IndexType   OutIndexType;

  /** Method for creation through the object factory. */
  itkNewMacro(Self);
  itkTypeMacro(NLMeansFilter, ImageToImageFilter);

  void SetSigma(const float sigma)
  {
    m_Var = 2.0 * sigma * sigma;
  }

  void SetHalfWindowSize(const unsigned int hws)
  {
    m_HalfPatchSize.Fill(hws);
    // Update NormalizeDistance
    m_NormalizeDistance = (2 * hws + 1) * (2 * hws + 1) * m_CutoffDistance * m_CutoffDistance;
  }

  void SetHalfSearchSize(const unsigned int hss)
  {
    m_HalfSearchSize.Fill(hss);
  }
  void SetCutOffDistance(const float thresh)
  {
    m_CutoffDistance = thresh;
    // Update NormalizeDistance
    m_NormalizeDistance = (2 * m_HalfPatchSize[0] + 1) * (2 * m_HalfPatchSize[1] + 1) * m_CutoffDistance * m_CutoffDistance;
  }

protected:
  /** Constructor */
  NLMeansFilter();
  /** Destructor */
  ~NLMeansFilter() override = default;

  void ThreadedGenerateData(const OutRegionType& outputRegionForThread, itk::ThreadIdType itkNotUsed(threadId)) override;

  void GenerateInputRequestedRegion() override;

  /** Compute the requested input region, given an output region.
   * If the input requested region is outside the largest input region, a mirror padding
   * is necessary. The returned tuple is composed of the following parameters :
   *  * input requested region (always lie inside the largest input region)
   *  * top rows, left cols, bottom rows, right cols : numbers of rows/cols to add with a mirror padding
   *  * boolean : if true, a mirror padding (in at least one direction) has to be computed
   */
  std::tuple<InRegionType, int, int, int, int, bool> OutputRegionToInputRegion(const OutRegionType& outputRegion) const;

  /** PrintSelf method */
  void PrintSelf(std::ostream& os, itk::Indent indent) const override;

private:
  NLMeansFilter(const Self&) = delete;            // purposely not implemented
  NLMeansFilter& operator=(const Self&) = delete; // purposely not implemented

  /** For a given shift in rows and cols, this function computes
   * the squared difference between the image and its shifted version.
   * Results are added to form an integral image.
   */
  void ComputeIntegralImage(const std::vector<double>& dataInput,    /**< input data stored in a vector */
                            std::vector<double>&       imIntegral,   /**< output parameter. Contains the integral image of squared difference */
                            const OutIndexType         shift,        /**< Shift (dcol, drow) to apply to compute the difference */
                            const InSizeType           sizeIntegral, /**< Integral image size */
                            const InSizeType           sizeInput     /**< input data image size */
                            ) const;

  /** This function computes the squared euclidean distance
   * between a patch and its shifted version.
   * Computation relies on the integral image obtained before.
   */
  OutPixelType ComputeDistance(const unsigned int         row,        /**< Upper left corner row coordinate of patch*/
                               const unsigned int         col,        /**< Upper left corner col coordinate of patch*/
                               const std::vector<double>& imIntegral, /**< Integral image of squared difference*/
                               const unsigned int         nbCols      /**< Integral image number of columns */
                               ) const;

  // Define class attributes
  InSizeType m_HalfSearchSize{0,0};
  InSizeType m_HalfPatchSize{0,0};
  float      m_Var;
  float      m_CutoffDistance;
  float      m_NormalizeDistance; // cutoff**2 * windowSize**2

  static const int m_ROW = 1;
  static const int m_COL = 0;
};
} // end namespace otb

#ifndef OTB_MANUAL_INSTANTIATION
#include "otbFastNLMeansImageFilter.hxx"
#endif

#endif