PCL
PSFFit.h
Go to the documentation of this file.
1 // ____ ______ __
2 // / __ \ / ____// /
3 // / /_/ // / / /
4 // / ____// /___ / /___ PixInsight Class Library
5 // /_/ \____//_____/ PCL 2.7.0
6 // ----------------------------------------------------------------------------
7 // pcl/PSFFit.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_PSFFit_h
53 #define __PCL_PSFFit_h
54 
56 
57 #include <pcl/Defs.h>
58 #include <pcl/Diagnostics.h>
59 
60 #include <pcl/ImageVariant.h>
61 #include <pcl/Matrix.h>
62 #include <pcl/String.h>
63 
64 namespace pcl
65 {
66 
67 // ----------------------------------------------------------------------------
68 
87 namespace PSFunction
88 {
89  enum value_type
90  {
91  Invalid = -1, // Represents an invalid or unsupported PSF type
92  Gaussian = 0, // Gaussian PSF
93  Moffat, // Moffat PSF with a fitted beta parameter
94  MoffatA, // Moffat PSF with fixed beta=10
95  Moffat8, // Moffat PSF with fixed beta=8
96  Moffat6, // Moffat PSF with fixed beta=6
97  Moffat4, // Moffat PSF with fixed beta=4
98  Moffat25, // Moffat PSF with fixed beta=2.5
99  Moffat15, // Moffat PSF with fixed beta=1.5
100  Lorentzian, // Lorentzian PSF, equivalent to a Moffat PSF with fixed beta=1
101  VariableShape, // Variable shape PSF
102  NumberOfFunctions,
103  Auto = 999, // Automatic selection for optimization, process-dependent.
104  Default = Gaussian
105  };
106 }
107 
108 // ----------------------------------------------------------------------------
109 
125 namespace PSFFitStatus
126 {
127  enum value_type
128  {
129  Invalid = -1,
130  NotFitted = 0,
131  FittedOk,
132  BadParameters,
133  NoSolution,
134  NoConvergence,
135  InaccurateSolution,
136  UnknownError
137  };
138 }
139 
140 // ----------------------------------------------------------------------------
141 
146 struct PSFData
147 {
151  using psf_function = PSFunction::value_type;
152 
156  using psf_fit_status = PSFFitStatus::value_type;
157 
163 
164  psf_function function = PSFunction::Invalid;
165  bool circular = false;
166  psf_fit_status status = PSFFitStatus::NotFitted;
167  bool celestial = false;
168  double B = 0;
169  double A = 0;
170  DPoint b0 = 0.0;
171  DPoint c0 = 0.0;
172  DPoint q0 = 0.0;
173  double sx = 0;
174  double sy = 0;
175  double theta = 0;
176  double beta = 0;
177  double flux = 0;
178  double signal = 0;
179  unsigned signalCount = 0;
180  double mad = 0;
185  PSFData() = default;
186 
190  PSFData( const PSFData& ) = default;
191 
195  PSFData( PSFData&& ) = default;
196 
200  PSFData& operator =( const PSFData& ) = default;
201 
205  PSFData& operator =( PSFData&& ) = default;
206 
212  operator bool() const
213  {
214  return status == PSFFitStatus::FittedOk;
215  }
216 
222  operator double() const
223  {
224  return (signal != 0) ? signal : flux;
225  }
226 
233  double operator []( int i ) const noexcept
234  {
235  return (i == 0) ? c0.x : c0.y;
236  }
237 
243 
248 
257  double FWHMx() const
258  {
259  return FWHM( function, sx, beta );
260  }
261 
270  double FWHMy() const
271  {
272  return FWHM( function, sy, beta );
273  }
274 
283  double FWTMx() const
284  {
285  return FWTM( function, sx, beta );
286  }
287 
296  double FWTMy() const
297  {
298  return FWTM( function, sy, beta );
299  }
300 
305  double Volume() const
306  {
307  return A*Volume( function, sx, sy, beta );
308  }
309 
328  DRect Bounds( double k = 1.0 ) const
329  {
330  double dx = k*FWHMx()/2;
331  double dy = k*FWHMy()/2;
332  return DRect( c0.x-dx, c0.y-dy, c0.x+dx, c0.y+dy );
333  }
334 
354  DRect FWTMBounds( double k = 1.0 ) const
355  {
356  double dx = k*FWTMx()/2;
357  double dy = k*FWTMy()/2;
358  return DRect( c0.x-dx, c0.y-dy, c0.x+dx, c0.y+dy );
359  }
360 
367  {
368  return celestial;
369  }
370 
375  void ToImage( Image& ) const;
376 
392  static double FWHM( psf_function function, double sigma, double beta = 2 )
393  {
394  PCL_PRECONDITION( beta > 0 )
395  switch ( function )
396  {
397  case PSFunction::Gaussian: return 2.3548200450309493 * sigma;
398  case PSFunction::Moffat: return 2 * sigma * Sqrt( Pow2( 1/beta ) - 1 );
399  case PSFunction::MoffatA: return 0.5358113941912513 * sigma;
400  case PSFunction::Moffat8: return 0.6016900619596693 * sigma;
401  case PSFunction::Moffat6: return 0.6998915581984769 * sigma;
402  case PSFunction::Moffat4: return 0.8699588840921645 * sigma;
403  case PSFunction::Moffat25: return 1.1305006161394060 * sigma;
404  case PSFunction::Moffat15: return 1.5328418730817597 * sigma;
405  case PSFunction::Lorentzian: return 2 * sigma;
406  case PSFunction::VariableShape: return 2 * sigma * Pow( beta*0.6931471805599453, 1/beta );
407  default: return 0; // ?!
408  }
409  }
410 
426  static double FWTM( psf_function function, double sigma, double beta = 2 )
427  {
428  PCL_PRECONDITION( beta > 0 )
429  switch ( function )
430  {
431  case PSFunction::Gaussian: return 4.291932052578694 * sigma;
432  case PSFunction::Moffat: return 2 * sigma * Sqrt( Pow( 10.0, 1/beta ) - 1 );
433  case PSFunction::MoffatA: return 1.017694279819175 * sigma;
434  case PSFunction::Moffat8: return 1.155026289161115 * sigma;
435  case PSFunction::Moffat6: return 1.367917055412454 * sigma;
436  case PSFunction::Moffat4: return 1.764402913213331 * sigma;
437  case PSFunction::Moffat25: return 2.459175822514185 * sigma;
438  case PSFunction::Moffat15: return 3.816589489904712 * sigma;
439  case PSFunction::Lorentzian: return 6 * sigma;
440  case PSFunction::VariableShape: return 2 * sigma * Pow( beta*2.302585092994045, 1/beta );
441  default: return 0; // ?!
442  }
443  }
444 
466  static double Volume( psf_function function, double sigma_x, double sigma_y, double beta = 2 )
467  {
468  PCL_PRECONDITION( beta > 0 )
469  PCL_PRECONDITION( function != PSFunction::Lorentzian && (function != PSFunction::Moffat || beta > 1) )
470  switch ( function )
471  {
472  case PSFunction::Gaussian: return 6.2831853071795862 * sigma_x*sigma_y;
473  case PSFunction::Moffat: return 3.1415926535897931 * sigma_x*sigma_y/(beta - 1);
474  case PSFunction::MoffatA: return 0.3490658503988659 * sigma_x*sigma_y;
475  case PSFunction::Moffat8: return 0.4487989505128276 * sigma_x*sigma_y;
476  case PSFunction::Moffat6: return 0.6283185307179586 * sigma_x*sigma_y;
477  case PSFunction::Moffat4: return 1.0471975511965976 * sigma_x*sigma_y;
478  case PSFunction::Moffat25: return 2.0943951023931953 * sigma_x*sigma_y;
479  case PSFunction::Moffat15: return 6.2831853071795862 * sigma_x*sigma_y;
480  default: return 0; // ?!
481  }
482  }
483 };
484 
485 // ----------------------------------------------------------------------------
486 
492 class PSFFit
493 {
494 public:
495 
499  using psf_function = PSFData::psf_function;
500 
504  using psf_fit_status = PSFData::psf_fit_status;
505 
510 
575  PSFFit( const ImageVariant& image,
576  const DPoint& center, const DRect& rect,
577  psf_function function = PSFunction::Gaussian, bool circular = false,
578  float betaMin = 1.0F, float betaMax = 4.0F,
579  double tolerance = 1.0e-08, float bkgMaxVar = 0.1F, float growthForFlux = 1.0F );
580 
584  PSFFit( const PSFFit& ) = default;
585 
589  PSFFit( PSFFit&& ) = default;
590 
594  PSFFit& operator =( const PSFFit& x ) = default;
595 
599  PSFFit& operator =( PSFFit&& x ) = default;
600 
605  operator bool() const
606  {
607  return psf;
608  }
609 
610 private:
611 
612  Matrix S; // matrix of sampled pixel data
613  Vector P; // vector of function parameters
614 
615  // The initial local background measured on the sampling region and the
616  // maximum allowed relative difference, for stabilization of local
617  // background PSF parameters.
618  double m_bkg;
619  float m_bkgMaxVar;
620 
621  // Growing factor in units of the Full Width at Tenth Maximum (FWTM) for
622  // extension/contraction of the PSF flux measurement region.
623  float m_growthForFlux = 1.0F;
624 
625  // Keep track of successive beta values in L-M iterations for stabilization
626  // of shape parameters. This guarantees convergence for Moffat functions.
627  mutable float m_beta;
628 
629  Vector GoodnessOfFit( psf_function, bool circular, bool test = false ) const;
630 
631  friend class PSFFitEngine;
632 };
633 
634 // ----------------------------------------------------------------------------
635 
636 } // pcl
637 
638 #endif // __PCL_PSFFit_h
639 
640 // ----------------------------------------------------------------------------
641 // EOF pcl/PSFFit.h - Released 2024-06-18T15:48:54Z
Implements a generic, two-dimensional, shared or local image.
Definition: Image.h:278
Generic dynamic matrix of arbitrary dimensions.
Definition: Matrix.h:123
A generic point in the two-dimensional space.
Definition: Point.h:100
component x
Abscissa (horizontal, or X-axis coordinate).
Definition: Point.h:111
component y
Ordinate (vertical, or Y-axis coordinate).
Definition: Point.h:112
A generic rectangle in the two-dimensional space.
Definition: Rectangle.h:314
Generic vector of arbitrary length.
Definition: Vector.h:107
Acts like a union for all types of images in PCL, with optional class-wide ownership of transported i...
Definition: ImageVariant.h:322
Numerical Point Spread Function (PSF) fit to a source in an image.
Definition: PSFFit.h:493
PSFData psf
Definition: PSFFit.h:509
PSFData::psf_function psf_function
Definition: PSFFit.h:499
PSFFit(PSFFit &&)=default
PSFFit & operator=(const PSFFit &x)=default
PSFFit(const PSFFit &)=default
PSFFit(const ImageVariant &image, const DPoint &center, const DRect &rect, psf_function function=PSFunction::Gaussian, bool circular=false, float betaMin=1.0F, float betaMax=4.0F, double tolerance=1.0e-08, float bkgMaxVar=0.1F, float growthForFlux=1.0F)
Unicode (UTF-16) string.
Definition: String.h:8113
Complex< T1 > Pow(const Complex< T1 > &c, T2 x) noexcept
Definition: Complex.h:747
Complex< T > Sqrt(const Complex< T > &c) noexcept
Definition: Complex.h:674
constexpr T Pow2(T x) noexcept
Definition: Math.h:1717
PCL root namespace.
Definition: AbstractImage.h:77
PSF fit parameters.
Definition: PSFFit.h:147
double FWHMy() const
Definition: PSFFit.h:270
PSFData(const PSFData &)=default
bool circular
Circular or elliptical PSF.
Definition: PSFFit.h:165
unsigned signalCount
Number of pixels used for signal evaluation.
Definition: PSFFit.h:179
PSFFitStatus::value_type psf_fit_status
Definition: PSFFit.h:156
DPoint q0
Centroid equatorial coordinates, when celestial=true.
Definition: PSFFit.h:172
String StatusText() const
double signal
Total flux above the local background, measured over the elliptical PSF region.
Definition: PSFFit.h:178
PSFData(PSFData &&)=default
psf_fit_status status
Status code (PSFFitStatus namespace).
Definition: PSFFit.h:166
PSFunction::value_type psf_function
Definition: PSFFit.h:151
DPoint::component component
Definition: PSFFit.h:162
bool celestial
True iff equatorial coordinates are available.
Definition: PSFFit.h:167
static double FWHM(psf_function function, double sigma, double beta=2)
Definition: PSFFit.h:392
void ToImage(Image &) const
double B
Local background estimate in pixel value units.
Definition: PSFFit.h:168
double A
Function amplitude (or estimated maximum) in pixel value units.
Definition: PSFFit.h:169
PSFData()=default
String FunctionName() const
double sy
Function width in pixels on the Y axis, sx >= sy.
Definition: PSFFit.h:174
DPoint b0
Barycenter position (initial star detection position) in image coordinates.
Definition: PSFFit.h:170
double sx
Function width in pixels on the X axis, sx >= sy.
Definition: PSFFit.h:173
double Volume() const
Definition: PSFFit.h:305
double FWHMx() const
Definition: PSFFit.h:257
double operator[](int i) const noexcept
Definition: PSFFit.h:233
double FWTMy() const
Definition: PSFFit.h:296
DRect FWTMBounds(double k=1.0) const
Definition: PSFFit.h:354
bool HasCelestialCoordinates() const
Definition: PSFFit.h:366
double theta
Rotation angle of the sx axis in degrees, in the [0,180) range.
Definition: PSFFit.h:175
double FWTMx() const
Definition: PSFFit.h:283
double beta
Moffat beta or shape parameter (dimensionless).
Definition: PSFFit.h:176
DRect Bounds(double k=1.0) const
Definition: PSFFit.h:328
static double Volume(psf_function function, double sigma_x, double sigma_y, double beta=2)
Definition: PSFFit.h:466
double mad
Definition: PSFFit.h:180
PSFData & operator=(const PSFData &)=default
DPoint c0
Fitted centroid position in image coordinates.
Definition: PSFFit.h:171
double flux
Total flux above the local background, measured over the rectangular fitting region.
Definition: PSFFit.h:177
static double FWTM(psf_function function, double sigma, double beta=2)
Definition: PSFFit.h:426