PCL
Convolution.h
Go to the documentation of this file.
1 // ____ ______ __
2 // / __ \ / ____// /
3 // / /_/ // / / /
4 // / ____// /___ / /___ PixInsight Class Library
5 // /_/ \____//_____/ PCL 2.1.19
6 // ----------------------------------------------------------------------------
7 // pcl/Convolution.h - Released 2019-11-07T10:59:34Z
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-2019 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 (http://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 
82 class PCL_CLASS Convolution : public InterlacedTransformation,
84  public ParallelProcess
85 { // NB: ImageTransformation is a virtual base class
86 public:
87 
94  Convolution() = default;
95 
105  Convolution( const KernelFilter& filter )
106  {
107  SetFilter( filter );
108  }
109 
113  Convolution( const Convolution& x ) :
116  ParallelProcess( x ),
117  m_weight( x.m_weight ),
118  m_highPass( x.m_highPass ), m_rawHighPass( x.m_rawHighPass ), m_rescaleHighPass( x.m_rescaleHighPass )
119  {
120  if ( !x.m_filter.IsNull() )
121  m_filter = x.m_filter->Clone();
122  }
123 
127  Convolution( Convolution&& ) = default;
128 
132  virtual ~Convolution()
133  {
134  }
135 
139  Convolution& operator =( const Convolution& x )
140  {
141  if ( &x != this )
142  {
143  (void)InterlacedTransformation::operator =( x );
144  (void)ThresholdedTransformation::operator =( x );
145  (void)ParallelProcess::operator =( x );
146  if ( x.m_filter.IsNull() )
147  m_filter.Destroy();
148  else
149  m_filter = x.m_filter->Clone();
150  m_weight = x.m_weight;
151  m_highPass = x.m_highPass;
152  m_rawHighPass = x.m_rawHighPass;
153  m_rescaleHighPass = x.m_rescaleHighPass;
154  }
155  return *this;
156  }
157 
161  Convolution& operator =( Convolution&& ) = default;
162 
170  const KernelFilter& Filter() const
171  {
172  PCL_PRECONDITION( !m_filter.IsNull() )
173  return *m_filter;
174  }
175 
179  void SetFilter( const KernelFilter& filter )
180  {
181  m_filter = filter.Clone();
182  CacheFilterProperties();
183  }
184 
194  double FilterWeight() const
195  {
196  return m_weight;
197  }
198 
210  bool IsHighPassFilter() const
211  {
212  return m_highPass;
213  }
214 
234  {
235  return m_rescaleHighPass;
236  }
237 
243  void EnableHighPassRescaling( bool enable = true )
244  {
245  m_rescaleHighPass = enable;
246  }
247 
253  void DisableHighPassRescaling( bool disable = true )
254  {
255  EnableHighPassRescaling( !disable );
256  }
257 
268  bool IsRawHighPassEnabled() const
269  {
270  return m_rawHighPass;
271  }
272 
277  void EnableRawHighPass( bool enable = true )
278  {
279  m_rawHighPass = enable;
280  }
281 
286  void DisableRawHighPass( bool disable = true )
287  {
288  EnableRawHighPass( !disable );
289  }
290 
297  {
298  PCL_PRECONDITION( !m_filter.IsNull() )
299  return m_filter->Size() + (m_filter->Size() - 1)*(InterlacingDistance() - 1);
300  }
301 
302 protected:
303 
304  /*
305  * The response function for convolution is defined as a kernel filter.
306  */
307  AutoPointer<KernelFilter> m_filter;
308 
309  /*
310  * Cached filter properties.
311  */
312  double m_weight = 0; // filter weight for low-pass normalization
313  bool m_highPass = false; // true if this is a high-pass filter
314 
315  /*
316  * User-selectable options
317  */
318  bool m_rawHighPass = false; // neither truncate nor normalize out-of-range values
319  bool m_rescaleHighPass = false; // truncate out-of-range values instead of normalize
320 
321  /*
322  * In-place 2-D nonseparable convolution algorithm in the spatial domain.
323  */
324  void Apply( pcl::Image& ) const override;
325  void Apply( pcl::DImage& ) const override;
326  void Apply( pcl::UInt8Image& ) const override;
327  void Apply( pcl::UInt16Image& ) const override;
328  void Apply( pcl::UInt32Image& ) const override;
329 
330 private:
331 
332  void CacheFilterProperties()
333  {
334  PCL_PRECONDITION( !m_filter.IsNull() )
335  PCL_PRECONDITION( !m_filter->IsEmpty() )
336  ValidateFilter();
337  m_highPass = m_filter->IsHighPassFilter();
338  m_weight = m_filter->Weight();
339  if ( pcl::Abs( m_weight ) < __PCL_CONVOLUTION_TINY_WEIGHT )
340  m_weight = 1;
341  }
342 
343  void ValidateFilter() const;
344 };
345 
346 // ----------------------------------------------------------------------------
347 
348 } // pcl
349 
350 #endif // __PCL_Convolution_h
351 
352 // ----------------------------------------------------------------------------
353 // EOF pcl/Convolution.h - Released 2019-11-07T10:59:34Z
bool IsNull() const
Definition: AutoPointer.h:454
Thresholded image transformation.
bool IsRawHighPassEnabled() const
Definition: Convolution.h:268
bool IsHighPassRescalingEnabled() const
Definition: Convolution.h:233
Interlaced image transformation in the spatial domain.
void EnableRawHighPass(bool enable=true)
Definition: Convolution.h:277
virtual ~Convolution()
Definition: Convolution.h:132
PCL root namespace.
Definition: AbstractImage.h:76
Convolution(const Convolution &x)
Definition: Convolution.h:113
virtual KernelFilter * Clone() const
Definition: KernelFilter.h:188
A smart pointer with sole object ownership and optional automatic object destruction.
Definition: AutoPointer.h:241
const KernelFilter & Filter() const
Definition: Convolution.h:170
16-bit unsigned integer image.
Discrete two-dimensional, nonseparable convolution in the spatial domain.
Definition: Convolution.h:82
T Abs(const Complex< T > &c)
Definition: Complex.h:420
void EnableHighPassRescaling(bool enable=true)
Definition: Convolution.h:243
void Apply(FI i, FI j, F f)
Definition: Utility.h:249
void DisableRawHighPass(bool disable=true)
Definition: Convolution.h:286
64-bit floating point real image.
32-bit unsigned integer image.
Convolution(const KernelFilter &filter)
Definition: Convolution.h:105
8-bit unsigned integer image.
Kernel filter in two dimensions.
Definition: KernelFilter.h:89
void SetFilter(const KernelFilter &filter)
Definition: Convolution.h:179
int OverlappingDistance() const
Definition: Convolution.h:296
void DisableHighPassRescaling(bool disable=true)
Definition: Convolution.h:253
32-bit floating point real image.
bool IsHighPassFilter() const
Definition: Convolution.h:210
double FilterWeight() const
Definition: Convolution.h:194
A process using multiple concurrent execution threads.