PCL
StarDetector.h
Go to the documentation of this file.
1 // ____ ______ __
2 // / __ \ / ____// /
3 // / /_/ // / / /
4 // / ____// /___ / /___ PixInsight Class Library
5 // /_/ \____//_____/ PCL 2.7.0
6 // ----------------------------------------------------------------------------
7 // pcl/StarDetector.h - Released 2024-06-18T15:48:54Z
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-2024 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_StarDetector_h
53 #define __PCL_StarDetector_h
54 
56 
57 #include <pcl/Defs.h>
58 
59 #include <pcl/ImageVariant.h>
60 #include <pcl/ParallelProcess.h>
61 #include <pcl/PSFFit.h>
62 
63 namespace pcl
64 {
65 
66 // ----------------------------------------------------------------------------
67 
73 class PCL_CLASS StarDetector : public ParallelProcess
74 {
75 public:
76 
80  using psf_function = PSFFit::psf_function;
81 
86  struct Star
87  {
93 
94  DPoint pos = 0.0;
97  Rect rect = 0;
98  Rect srect = 0;
99  float area = 0;
100  float flux = 0;
101  float signal = 0;
103  float mad = 0;
105  Star* ref = nullptr;
106 
110  Star() = default;
111 
117  Star( const DPoint& p, const Rect& r, const Rect& sr = 0, float a = 0, float f = 0, float s = 0, float m = 0 )
118  : pos( p ), rect( r ), srect( sr ), area( a ), flux( f ), signal( s ), mad( m )
119  {
120  }
121 
126  Star( const DPoint& p, float f = 0 )
127  : pos( p ), flux( f )
128  {
129  }
130 
135  Star( double x, double y, float f = 0 )
136  : pos( x, y ), flux( f )
137  {
138  }
139 
143  Star( const Star& ) = default;
144 
148  Star& operator =( const Star& ) = default;
149 
154  bool operator ==( const Star& s ) const
155  {
156  return pos == s.pos;
157  }
158 
164  bool operator <( const Star& s ) const
165  {
166  return flux > s.flux;
167  }
168 
173  component operator []( int i ) const
174  {
175  return pos[i];
176  }
177  };
178 
183 
187  StarDetector() = default;
188 
192  StarDetector( const StarDetector& ) = default;
193 
197  StarDetector& operator =( const StarDetector& ) = default;
198 
202  ~StarDetector() override
203  {
204  }
205 
219  int StructureLayers() const
220  {
221  return m_structureLayers;
222  }
223 
228  void SetStructureLayers( int n )
229  {
230  PCL_PRECONDITION( n > 0 && n <= 8 )
231  m_structureLayers = Range( n, 1, 8 );
232  }
233 
249  int NoiseLayers() const
250  {
251  return m_noiseLayers;
252  }
253 
258  void SetNoiseLayers( int n )
259  {
260  PCL_PRECONDITION( n >= 0 && n <= 4 )
261  m_noiseLayers = Range( n, 0, 4 );
262  }
263 
273  {
274  return m_hotPixelFilterRadius;
275  }
276 
282  {
283  PCL_PRECONDITION( n >= 0 && n <= 3 )
284  m_hotPixelFilterRadius = Range( n, 0, 3 );
285  }
286 
303  {
304  return m_noiseReductionFilterRadius;
305  }
306 
312  {
313  PCL_PRECONDITION( n >= 0 && n <= 64 )
314  m_noiseReductionFilterRadius = Range( n, 0, 64 );
315  }
316 
339  int MinStructureSize() const
340  {
341  return m_minStructureSize;
342  }
343 
349  void SetMinStructureSize( int n )
350  {
351  PCL_PRECONDITION( n >= 0 )
352  m_minStructureSize = Max( 0, n );
353  }
354 
363  int MinStarSize() const
364  {
365  return m_minStarSize;
366  }
367 
387  float Sensitivity() const
388  {
389  return m_sensitivity;
390  }
391 
396  void SetSensitivity( float s )
397  {
398  PCL_PRECONDITION( s >= 0 && s <= 1 )
399  m_sensitivity = Range( s, 0.0F, 1.0F );
400  }
401 
424  float PeakResponse() const
425  {
426  return m_peakResponse;
427  }
428 
433  void SetPeakResponse( float r )
434  {
435  PCL_PRECONDITION( r >= 0 && r <= 1 )
436  m_peakResponse = Range( r, 0.0F, 1.0F );
437  }
438 
451  float MinSNR() const
452  {
453  return m_minSNR;
454  }
455 
460  void SetMinSNR( float snr )
461  {
462  PCL_PRECONDITION( snr >= 0 )
463  m_minSNR = Max( 0.0F, snr );
464  }
465 
475  float BrightThreshold() const
476  {
477  return m_brightThreshold;
478  }
479 
484  void SetBrightThreshold( float r )
485  {
486  PCL_PRECONDITION( r >= 1 && r <= 100 )
487  m_brightThreshold = Range( r, 1.0F, 100.0F );
488  }
489 
508  float MaxDistortion() const
509  {
510  return m_maxDistortion;
511  }
512 
517  void SetMaxDistortion( float d )
518  {
519  PCL_PRECONDITION( d >= 0 && d <= 1 )
520  m_maxDistortion = Range( d, 0.0F, 1.0F );
521  }
522 
535  {
536  return m_allowClusteredSources;
537  }
538 
543  void EnableClusteredSources( bool enable = true )
544  {
545  m_allowClusteredSources = enable;
546  }
547 
552  void DisableClusteredSources( bool disable = true )
553  {
554  EnableClusteredSources( !disable );
555  }
556 
565  {
566  return m_localDetectionFilterRadius;
567  }
568 
574  {
575  PCL_PRECONDITION( n >= 1 && n <= 5 )
576  m_localDetectionFilterRadius = Range( n, 1, 5 );
577  }
578 
587  {
588  return m_localMaximaDetectionLimit;
589  }
590 
596  {
597  PCL_PRECONDITION( v >= 0 && v <= 1 )
598  m_localMaximaDetectionLimit = Range( v, 0.1F, 1.0F );
599  }
600 
616  {
617  return !m_noLocalMaximaDetection;
618  }
619 
624  void EnableLocalMaximaDetection( bool enable = true )
625  {
626  m_noLocalMaximaDetection = !enable;
627  }
628 
633  void DisableLocalMaximaDetection( bool disable = true )
634  {
635  EnableLocalMaximaDetection( !disable );
636  }
637 
645  float UpperLimit() const
646  {
647  return m_upperLimit;
648  }
649 
654  void SetUpperLimit( float u )
655  {
656  PCL_PRECONDITION( u >= 0 && u <= 1 )
657  m_upperLimit = Range( u, 0.0F, 1.0F );
658  }
659 
667  {
668  return m_invert;
669  }
670 
675  void EnableImageInversion( bool enabled = true )
676  {
677  m_invert = enabled;
678  }
679 
684  void DisableImageInversion( bool disable = true )
685  {
686  EnableImageInversion( !disable );
687  }
688 
707  bool IsPSFFittingEnabled() const
708  {
709  return m_fitPSF;
710  }
711 
716  void EnablePSFFitting( bool enable = true )
717  {
718  m_fitPSF = enable;
719  }
720 
725  void DisablePSFFitting( bool disable = true )
726  {
727  EnablePSFFitting( !disable );
728  }
729 
735  psf_function PSFType() const
736  {
737  return m_psfType;
738  }
739 
744  void SetPSFType( psf_function type )
745  {
746  m_psfType = type;
747  }
748 
754  bool IsEllipticPSF() const
755  {
756  return m_psfElliptic;
757  }
758 
762  void EnableEllipticPSF( bool enable = true )
763  {
764  m_psfElliptic = enable;
765  }
766 
770  void DisableEllipticPSF( bool disable = true )
771  {
772  EnableEllipticPSF( !disable );
773  }
774 
788  float PSFCentroidTolerance() const
789  {
790  return m_psfCentroidTolerance;
791  }
792 
797  void SetPSFCentroidTolerance( float t )
798  {
799  PCL_PRECONDITION( t >= 0 )
800  m_psfCentroidTolerance = Max( 0.0F, t );
801  }
802 
812  const UInt8Image* DetectionMask() const
813  {
814  return m_mask;
815  }
816 
827  void SetDetectionMask( const UInt8Image* mask )
828  {
829  if ( mask != nullptr )
830  if ( mask->IsEmpty() )
831  mask = nullptr;
832  m_mask = mask;
833  }
834 
841  star_list DetectStars( const ImageVariant& image ) const;
842 
852  star_list operator()( const ImageVariant& image ) const
853  {
854  return DetectStars( image );
855  }
856 
863  Image StructureMap( const ImageVariant& image ) const;
864 
872  Image Structures( const ImageVariant& image ) const;
873 
874 protected:
875 
876  int m_structureLayers = 5;
877  int m_noiseLayers = 0;
878  int m_hotPixelFilterRadius = 1;
879  int m_noiseReductionFilterRadius = 0;
880  int m_minStructureSize = 0; // automatic mode by default
881  mutable int m_minStarSize = 0; // when minStructureSize=0
882  float m_sensitivity = 0.5F;
883  float m_peakResponse = 0.5F;
884  float m_minSNR = 0.0F;
885  float m_brightThreshold = 3.0F;
886  float m_maxDistortion = 0.6F;
887  bool m_allowClusteredSources = false;
888  int m_localDetectionFilterRadius = 2;
889  float m_localMaximaDetectionLimit = 0.75F;
890  bool m_noLocalMaximaDetection = false;
891  float m_upperLimit = 1.0F;
892  bool m_invert = false;
893  bool m_fitPSF = false;
894  psf_function m_psfType = PSFunction::Gaussian;
895  bool m_psfElliptic = false;
896  float m_psfCentroidTolerance = 1.5F;
897  const UInt8Image* m_mask = nullptr;
898 
899 private:
900 
901  star_list DetectStars( Image& image ) const;
902 };
903 
904 // ----------------------------------------------------------------------------
905 
906 } // pcl
907 
908 #endif // __PCL_StarDetector_h
909 
910 // ----------------------------------------------------------------------------
911 // EOF pcl/StarDetector.h - Released 2024-06-18T15:48:54Z
Generic dynamic array.
Definition: Array.h:100
A generic point in the two-dimensional space.
Definition: Point.h:100
A generic rectangle in the two-dimensional space.
Definition: Rectangle.h:314
bool IsEmpty() const noexcept
Acts like a union for all types of images in PCL, with optional class-wide ownership of transported i...
Definition: ImageVariant.h:322
PSFData::psf_function psf_function
Definition: PSFFit.h:499
A process using multiple concurrent execution threads.
Automatic star detection.
Definition: StarDetector.h:74
void SetNoiseLayers(int n)
Definition: StarDetector.h:258
void SetPeakResponse(float r)
Definition: StarDetector.h:433
void SetPSFType(psf_function type)
Definition: StarDetector.h:744
StarDetector(const StarDetector &)=default
void SetBrightThreshold(float r)
Definition: StarDetector.h:484
void EnableLocalMaximaDetection(bool enable=true)
Definition: StarDetector.h:624
Image StructureMap(const ImageVariant &image) const
const UInt8Image * DetectionMask() const
Definition: StarDetector.h:812
float Sensitivity() const
Definition: StarDetector.h:387
void SetMinStructureSize(int n)
Definition: StarDetector.h:349
void SetMaxDistortion(float d)
Definition: StarDetector.h:517
void EnablePSFFitting(bool enable=true)
Definition: StarDetector.h:716
void SetUpperLimit(float u)
Definition: StarDetector.h:654
bool IsPSFFittingEnabled() const
Definition: StarDetector.h:707
void SetStructureLayers(int n)
Definition: StarDetector.h:228
void DisableLocalMaximaDetection(bool disable=true)
Definition: StarDetector.h:633
void SetSensitivity(float s)
Definition: StarDetector.h:396
int StructureLayers() const
Definition: StarDetector.h:219
void SetLocalDetectionFilterRadius(int n)
Definition: StarDetector.h:573
int NoiseReductionFilterRadius() const
Definition: StarDetector.h:302
void DisablePSFFitting(bool disable=true)
Definition: StarDetector.h:725
int MinStructureSize() const
Definition: StarDetector.h:339
bool IsEllipticPSF() const
Definition: StarDetector.h:754
bool IsImageInversionEnabled() const
Definition: StarDetector.h:666
Image Structures(const ImageVariant &image) const
bool IsLocalMaximaDetectionEnabled() const
Definition: StarDetector.h:615
void DisableImageInversion(bool disable=true)
Definition: StarDetector.h:684
void SetDetectionMask(const UInt8Image *mask)
Definition: StarDetector.h:827
void SetMinSNR(float snr)
Definition: StarDetector.h:460
star_list operator()(const ImageVariant &image) const
Definition: StarDetector.h:852
StarDetector()=default
void EnableImageInversion(bool enabled=true)
Definition: StarDetector.h:675
float PeakResponse() const
Definition: StarDetector.h:424
~StarDetector() override
Definition: StarDetector.h:202
void DisableEllipticPSF(bool disable=true)
Definition: StarDetector.h:770
int MinStarSize() const
Definition: StarDetector.h:363
void DisableClusteredSources(bool disable=true)
Definition: StarDetector.h:552
void EnableClusteredSources(bool enable=true)
Definition: StarDetector.h:543
float MaxDistortion() const
Definition: StarDetector.h:508
float MinSNR() const
Definition: StarDetector.h:451
float UpperLimit() const
Definition: StarDetector.h:645
void SetLocalMaximaDetectionLimit(float v)
Definition: StarDetector.h:595
bool IsClusteredSourcesEnabled() const
Definition: StarDetector.h:534
int HotPixelFilterRadius() const
Definition: StarDetector.h:272
void EnableEllipticPSF(bool enable=true)
Definition: StarDetector.h:762
void SetNoiseReductionFilterRadius(int n)
Definition: StarDetector.h:311
float PSFCentroidTolerance() const
Definition: StarDetector.h:788
psf_function PSFType() const
Definition: StarDetector.h:735
star_list DetectStars(const ImageVariant &image) const
void SetPSFCentroidTolerance(float t)
Definition: StarDetector.h:797
int LocalDetectionFilterRadius() const
Definition: StarDetector.h:564
void SetHotPixelFilterRadius(int n)
Definition: StarDetector.h:281
float BrightThreshold() const
Definition: StarDetector.h:475
float LocalMaximaDetectionLimit() const
Definition: StarDetector.h:586
int NoiseLayers() const
Definition: StarDetector.h:249
bool operator==(const Array< T, A > &x1, const Array< T, A > &x2) noexcept
Definition: Array.h:2267
bool operator<(const Array< T, A > &x1, const Array< T, A > &x2) noexcept
Definition: Array.h:2278
constexpr const T & Range(const T &x, const T &a, const T &b) noexcept
Definition: Utility.h:190
constexpr const T & Max(const T &a, const T &b) noexcept
Definition: Utility.h:119
PCL root namespace.
Definition: AbstractImage.h:77
Structure to hold the data of a detected star.
Definition: StarDetector.h:87
Star(const DPoint &p, float f=0)
Definition: StarDetector.h:126
Star(const Star &)=default
Star(const DPoint &p, const Rect &r, const Rect &sr=0, float a=0, float f=0, float s=0, float m=0)
Definition: StarDetector.h:117
float flux
Total flux above local background.
Definition: StarDetector.h:100
Star(double x, double y, float f=0)
Definition: StarDetector.h:135
DPoint::component component
Definition: StarDetector.h:92