PCL
Convolution.h
Go to the documentation of this file.
1 // ____ ______ __
2 // / __ \ / ____// /
3 // / /_/ // / / /
4 // / ____// /___ / /___ PixInsight Class Library
5 // /_/ \____//_____/ PCL 2.4.7
6 // ----------------------------------------------------------------------------
7 // pcl/Convolution.h - Released 2020-12-17T15:46:29Z
8 // ----------------------------------------------------------------------------
9 // This file is part of the PixInsight Class Library (PCL).
10 // PCL is a multiplatform C++ framework for development of PixInsight modules.
11 //
12 // Copyright (c) 2003-2020 Pleiades Astrophoto S.L. All Rights Reserved.
13 //
14 // Redistribution and use in both source and binary forms, with or without
15 // modification, is permitted provided that the following conditions are met:
16 //
17 // 1. All redistributions of source code must retain the above copyright
18 // notice, this list of conditions and the following disclaimer.
19 //
20 // 2. All redistributions in binary form must reproduce the above copyright
21 // notice, this list of conditions and the following disclaimer in the
22 // documentation and/or other materials provided with the distribution.
23 //
24 // 3. Neither the names "PixInsight" and "Pleiades Astrophoto", nor the names
25 // of their contributors, may be used to endorse or promote products derived
26 // from this software without specific prior written permission. For written
27 // permission, please contact info@pixinsight.com.
28 //
29 // 4. All products derived from this software, in any form whatsoever, must
30 // reproduce the following acknowledgment in the end-user documentation
31 // and/or other materials provided with the product:
32 //
33 // "This product is based on software from the PixInsight project, developed
34 // by Pleiades Astrophoto and its contributors (https://pixinsight.com/)."
35 //
36 // Alternatively, if that is where third-party acknowledgments normally
37 // appear, this acknowledgment must be reproduced in the product itself.
38 //
39 // THIS SOFTWARE IS PROVIDED BY PLEIADES ASTROPHOTO AND ITS CONTRIBUTORS
40 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
41 // TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL PLEIADES ASTROPHOTO OR ITS
43 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
44 // EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, BUSINESS
45 // INTERRUPTION; PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; AND LOSS OF USE,
46 // DATA OR PROFITS) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
47 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
48 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
49 // POSSIBILITY OF SUCH DAMAGE.
50 // ----------------------------------------------------------------------------
51 
52 #ifndef __PCL_Convolution_h
53 #define __PCL_Convolution_h
54 
56 
57 #include <pcl/Defs.h>
58 #include <pcl/Diagnostics.h>
59 
60 #include <pcl/AutoPointer.h>
62 #include <pcl/KernelFilter.h>
63 #include <pcl/ParallelProcess.h>
65 
66 #define __PCL_CONVOLUTION_TINY_WEIGHT 1.0e-20
67 
68 namespace pcl
69 {
70 
71 // ----------------------------------------------------------------------------
72 
86 class PCL_CLASS Convolution : public InterlacedTransformation,
88  public ParallelProcess
89 { // NB: ImageTransformation is a virtual base class
90 public:
91 
98  Convolution() = default;
99 
109  Convolution( const KernelFilter& filter )
110  {
111  SetFilter( filter );
112  }
113 
120  , ParallelProcess( x )
121  , m_weight( x.m_weight )
122  , m_highPass( x.m_highPass )
123  , m_rawHighPass( x.m_rawHighPass )
124  , m_rescaleHighPass( x.m_rescaleHighPass )
125  {
126  if ( !x.m_filter.IsNull() )
127  m_filter = x.m_filter->Clone();
128  }
129 
133  Convolution( Convolution&& ) = default;
134 
138  virtual ~Convolution()
139  {
140  }
141 
145  Convolution& operator =( const Convolution& x )
146  {
147  if ( &x != this )
148  {
149  (void)InterlacedTransformation::operator =( x );
150  (void)ThresholdedTransformation::operator =( x );
151  (void)ParallelProcess::operator =( x );
152  if ( x.m_filter.IsNull() )
153  m_filter.Destroy();
154  else
155  m_filter = x.m_filter->Clone();
156  m_weight = x.m_weight;
157  m_highPass = x.m_highPass;
158  m_rawHighPass = x.m_rawHighPass;
159  m_rescaleHighPass = x.m_rescaleHighPass;
160  }
161  return *this;
162  }
163 
167  Convolution& operator =( Convolution&& ) = default;
168 
176  const KernelFilter& Filter() const
177  {
178  PCL_PRECONDITION( !m_filter.IsNull() )
179  return *m_filter;
180  }
181 
185  void SetFilter( const KernelFilter& filter )
186  {
187  m_filter = filter.Clone();
188  CacheFilterProperties();
189  }
190 
200  double FilterWeight() const
201  {
202  return m_weight;
203  }
204 
216  bool IsHighPassFilter() const
217  {
218  return m_highPass;
219  }
220 
240  {
241  return m_rescaleHighPass;
242  }
243 
249  void EnableHighPassRescaling( bool enable = true )
250  {
251  m_rescaleHighPass = enable;
252  }
253 
259  void DisableHighPassRescaling( bool disable = true )
260  {
261  EnableHighPassRescaling( !disable );
262  }
263 
274  bool IsRawHighPassEnabled() const
275  {
276  return m_rawHighPass;
277  }
278 
283  void EnableRawHighPass( bool enable = true )
284  {
285  m_rawHighPass = enable;
286  }
287 
292  void DisableRawHighPass( bool disable = true )
293  {
294  EnableRawHighPass( !disable );
295  }
296 
303  {
304  PCL_PRECONDITION( !m_filter.IsNull() )
305  return m_filter->Size() + (m_filter->Size() - 1)*(InterlacingDistance() - 1);
306  }
307 
308 protected:
309 
310  /*
311  * The response function for convolution is defined as a kernel filter.
312  */
313  AutoPointer<KernelFilter> m_filter;
314 
315  /*
316  * Cached filter properties.
317  */
318  double m_weight = 0; // filter weight for low-pass normalization
319  bool m_highPass = false; // true if this is a high-pass filter
320 
321  /*
322  * User-selectable options
323  */
324  bool m_rawHighPass = false; // neither truncate nor normalize out-of-range values
325  bool m_rescaleHighPass = false; // truncate out-of-range values instead of normalize
326 
327  /*
328  * In-place 2-D nonseparable convolution algorithm in the spatial domain.
329  */
330  void Apply( pcl::Image& ) const override;
331  void Apply( pcl::DImage& ) const override;
332  void Apply( pcl::UInt8Image& ) const override;
333  void Apply( pcl::UInt16Image& ) const override;
334  void Apply( pcl::UInt32Image& ) const override;
335 
336 private:
337 
338  void CacheFilterProperties()
339  {
340  PCL_PRECONDITION( !m_filter.IsNull() )
341  PCL_PRECONDITION( !m_filter->IsEmpty() )
342  ValidateFilter();
343  m_highPass = m_filter->IsHighPassFilter();
344  m_weight = m_filter->Weight();
345  if ( pcl::Abs( m_weight ) < __PCL_CONVOLUTION_TINY_WEIGHT )
346  m_weight = 1;
347  }
348 
349  void ValidateFilter() const;
350 };
351 
352 // ----------------------------------------------------------------------------
353 
354 } // pcl
355 
356 #endif // __PCL_Convolution_h
357 
358 // ----------------------------------------------------------------------------
359 // EOF pcl/Convolution.h - Released 2020-12-17T15:46:29Z
bool IsNull() const
Definition: AutoPointer.h:454
Thresholded image transformation.
bool IsRawHighPassEnabled() const
Definition: Convolution.h:274
bool IsHighPassRescalingEnabled() const
Definition: Convolution.h:239
Interlaced image transformation in the spatial domain.
void EnableRawHighPass(bool enable=true)
Definition: Convolution.h:283
virtual ~Convolution()
Definition: Convolution.h:138
PCL root namespace.
Definition: AbstractImage.h:76
Convolution(const Convolution &x)
Definition: Convolution.h:117
virtual KernelFilter * Clone() const
Definition: KernelFilter.h:190
A smart pointer with exclusive object ownership and optional automatic object destruction.
Definition: AutoPointer.h:241
const KernelFilter & Filter() const
Definition: Convolution.h:176
16-bit unsigned integer image.
Discrete two-dimensional nonseparable convolution in the spatial domain.
Definition: Convolution.h:86
void EnableHighPassRescaling(bool enable=true)
Definition: Convolution.h:249
void DisableRawHighPass(bool disable=true)
Definition: Convolution.h:292
void Apply(FI i, FI j, F f) noexcept(noexcept(f))
Definition: Utility.h:249
64-bit floating point real image.
32-bit unsigned integer image.
Convolution(const KernelFilter &filter)
Definition: Convolution.h:109
8-bit unsigned integer image.
T Abs(const Complex< T > &c) noexcept
Definition: Complex.h:429
Kernel filter in two dimensions.
Definition: KernelFilter.h:89
void SetFilter(const KernelFilter &filter)
Definition: Convolution.h:185
int OverlappingDistance() const
Definition: Convolution.h:302
void DisableHighPassRescaling(bool disable=true)
Definition: Convolution.h:259
32-bit floating point real image.
bool IsHighPassFilter() const
Definition: Convolution.h:216
double FilterWeight() const
Definition: Convolution.h:200
A process using multiple concurrent execution threads.