PCL
SeparableFilter.h
Go to the documentation of this file.
1 // ____ ______ __
2 // / __ \ / ____// /
3 // / /_/ // / / /
4 // / ____// /___ / /___ PixInsight Class Library
5 // /_/ \____//_____/ PCL 2.1.19
6 // ----------------------------------------------------------------------------
7 // pcl/SeparableFilter.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_SeparableFilter_h
53 #define __PCL_SeparableFilter_h
54 
56 
57 #include <pcl/Defs.h>
58 #include <pcl/Diagnostics.h>
59 
60 #include <pcl/Matrix.h>
61 #include <pcl/Vector.h>
62 
63 namespace pcl
64 {
65 
66 // ----------------------------------------------------------------------------
67 
89 class PCL_CLASS SeparableFilter
90 {
91 public:
92 
96  typedef float coefficient;
97 
102 
107 
111  SeparableFilter( const String& name = String() ) :
112  filterName( name )
113  {
114  }
115 
124  SeparableFilter( int n, const String& name = String() ) :
125  rowFilter( PCL_VALID_KERNEL_SIZE( n ) ),
126  colFilter( PCL_VALID_KERNEL_SIZE( n ) ), filterName( name )
127  {
128  PCL_PRECONDITION( n == 0 || n >= 3 )
129  PCL_PRECONDITION( n == 0 || (n & 1) )
130  }
131 
139  template <typename T>
140  SeparableFilter( int n, const T& x, const String& name = String() ) :
141  rowFilter( x, PCL_VALID_KERNEL_SIZE( n ) ),
142  colFilter( x, PCL_VALID_KERNEL_SIZE( n ) ), filterName( name )
143  {
144  PCL_PRECONDITION( n == 0 || n >= 3 )
145  PCL_PRECONDITION( n == 0 || (n & 1) )
146  }
147 
153  SeparableFilter( const coefficient_vector& h, const coefficient_vector& v, const String& name = String() ) :
154  rowFilter( h ), colFilter( v ), filterName( name )
155  {
156  PCL_PRECONDITION( v.Length() == h.Length() )
157  PCL_PRECONDITION( v.IsEmpty() || v.Length() >= 3 )
158  PCL_PRECONDITION( v.IsEmpty() || (v.Length() & 1) )
159  }
160 
167  template <typename T>
168  SeparableFilter( const T* h, const T* v, int n, const String& name = String() ) :
169  rowFilter( h, PCL_VALID_KERNEL_SIZE( n ) ),
170  colFilter( v, PCL_VALID_KERNEL_SIZE( n ) ), filterName( name )
171  {
172  PCL_PRECONDITION( n == 0 || n >= 3 )
173  PCL_PRECONDITION( n == 0 || (n & 1) )
174  }
175 
179  SeparableFilter( const SeparableFilter& ) = default;
180 
184  SeparableFilter( SeparableFilter&& ) = default;
185 
190  {
191  }
192 
200  virtual SeparableFilter* Clone() const
201  {
202  return new SeparableFilter( *this );
203  }
204 
208  SeparableFilter& operator =( const SeparableFilter& ) = default;
209 
213  SeparableFilter& operator =( SeparableFilter&& ) = default;
214 
219  SeparableFilter& operator =( const coefficient& x )
220  {
221  rowFilter = x;
222  colFilter = x;
223  return *this;
224  }
225 
231  bool operator ==( const SeparableFilter& f ) const
232  {
233  return Name() == f.Name() && SameCoefficients( f );
234  }
235 
239  String Name() const
240  {
241  return filterName;
242  }
243 
247  virtual void Rename( const String& newName )
248  {
249  filterName = newName.Trimmed();
250  }
251 
256  int Size() const
257  {
258  return rowFilter.Length(); // both 1D filters have the same length
259  }
260 
266  virtual void Resize( int n )
267  {
268  PCL_PRECONDITION( n == 0 || n >= 3 )
269  PCL_PRECONDITION( n == 0 || (n & 1) )
270  if ( n == 0 )
271  colFilter = rowFilter = coefficient_vector();
272  else
273  colFilter = rowFilter = coefficient_vector( PCL_VALID_KERNEL_SIZE( n ) );
274  }
275 
279  bool IsEmpty() const
280  {
281  return rowFilter.IsEmpty(); // both 1D filters have the same length
282  }
283 
287  coefficient_vector RowFilter() const
288  {
289  return rowFilter;
290  }
291 
295  coefficient_vector ColumnFilter() const
296  {
297  return colFilter;
298  }
299 
303  coefficient_vector ColFilter() const
304  {
305  return colFilter;
306  }
307 
313  coefficient_vector Filter( int phase ) const
314  {
315  return phase ? colFilter : rowFilter;
316  }
317 
322  double Weight() const
323  {
324  return rowFilter.Sum() * colFilter.Sum();
325  }
326 
338  bool IsHighPassFilter() const
339  {
340  int s = -1;
341  for ( coefficient_vector::const_iterator i = rowFilter.Begin(); i < rowFilter.End(); ++i )
342  if ( *i != 0 )
343  for ( s = *i < 0; ++i < rowFilter.End(); )
344  if ( *i != 0 && (*i < 0) != s )
345  return true;
346  for ( coefficient_vector::const_iterator i = colFilter.Begin(); i < colFilter.End(); ++i )
347  if ( *i != 0 )
348  for ( s = (s < 0) ? *i++ < 0 : s; i < colFilter.End(); ++i )
349  if ( *i != 0 && (*i < 0) != s )
350  return true;
351  return false;
352  }
353 
358  bool SameCoefficients( const SeparableFilter& f ) const
359  {
360  return colFilter == f.colFilter && rowFilter == f.rowFilter;
361  }
362 
367  virtual void Clear()
368  {
369  colFilter = rowFilter = coefficient_vector();
370  }
371 
372 protected:
373 
374  /*
375  * Horizontal and vertical one-dimensional filters. The 2D filter matrix is
376  * equal to the product of these two vectors.
377  */
378  coefficient_vector rowFilter;
379  coefficient_vector colFilter;
380 
381  /*
382  * Identifying name.
383  */
384  String filterName;
385 };
386 
387 // ----------------------------------------------------------------------------
388 
389 } // pcl
390 
391 #endif // __PCL_SeparableFilter_h
392 
393 // ----------------------------------------------------------------------------
394 // EOF pcl/SeparableFilter.h - Released 2019-11-07T10:59:34Z
SeparableFilter(int n, const String &name=String())
virtual SeparableFilter * Clone() const
PCL root namespace.
Definition: AbstractImage.h:76
GenericVector< coefficient > coefficient_vector
int Length() const
Definition: Vector.h:1502
Separable filter in two dimensions.
coefficient_vector Filter(int phase) const
virtual void Resize(int n)
coefficient_vector ColumnFilter() const
GenericMatrix< coefficient > coefficient_matrix
double Weight() const
bool SameCoefficients(const SeparableFilter &f) const
Unicode (UTF-16) string.
Definition: String.h:7911
virtual void Clear()
SeparableFilter(const T *h, const T *v, int n, const String &name=String())
SeparableFilter(const coefficient_vector &h, const coefficient_vector &v, const String &name=String())
SeparableFilter(int n, const T &x, const String &name=String())
virtual void Rename(const String &newName)
const coefficient * const_iterator
Definition: Vector.h:128
String Name(int colorSpace)
bool IsHighPassFilter() const
bool operator==(const Array< T, A > &x1, const Array< T, A > &x2)
Definition: Array.h:2075
coefficient_vector RowFilter() const
bool IsEmpty() const
Definition: Vector.h:1539
SeparableFilter(const String &name=String())
coefficient_vector ColFilter() const