27#ifndef fitGaussian_hpp
28#define fitGaussian_hpp
36#include "../constants.hpp"
56template <
typename _realT>
63template <
typename _realT>
68 static const int nparams = 4;
70 static void func( realT *p, realT *hx,
int m,
int n,
void *adata )
74 realT G0 = arr->G0( p );
75 realT G = arr->G( p );
76 realT x0 = arr->x0( p );
77 realT sigma = arr->sigma( p );
83 for(
int i = 0; i < arr->
m_nx; ++i )
92 for(
int i = 0; i < arr->
m_nx; ++i )
104 for(
int i = 0; i < arr->
m_nx; ++i )
111 for(
int i = 0; i < arr->
m_nx; ++i )
143template <
typename _realT>
148 typedef _realT realT;
192 arr.G0( this->
p,
G0 );
194 arr.x0( this->
p,
x0 );
195 arr.sigma( this->
p,
sigma );
208 void setArray( realT *data, realT *coords,
int nx )
211 arr.m_coords = coords;
231 return arr.G0( this->
p );
237 return arr.G( this->
p );
243 return arr.x0( this->
p );
251 return arr.sigma( this->
p );
255extern template class fitGaussian1D<float>;
256extern template class fitGaussian1D<double>;
265template <
typename _realT>
266struct gaussian2D_sym_fitter;
269template <
typename _realT>
270struct gaussian2D_gen_fitter;
272template <
typename _realT>
273struct gaussian2D_gen_fitter_bgfixed;
299template <
typename fitterT>
304 typedef typename fitterT::realT realT;
312 if( fitterT::maxNparams == 5 )
428 for(
int j = 0; j < nx; ++j )
430 for(
int i = 0; i < ny; ++i )
432 idx_mat = i + j * nx;
434 this->
n +=
arr.mask[idx_mat];
443 fitter.paramNormalizer( &
arr, this->
p, 1 );
447 fitter.paramNormalizer( &
arr, this->
p, -1 );
448 fitter.paramNormalizer(
460 return arr.G0( this->
p );
466 return arr.G( this->
p );
472 return arr.x0( this->
p );
478 return arr.y0( this->
p );
488 return arr.sigma( this->
p );
502 return arr.sigma_x( this->
p );
508 return arr.sigma_y( this->
p );
514 return arr.theta( this->
p );
523template <
typename _realT>
526 typedef _realT realT;
528 static const int maxNparams = 5;
530 static void func( realT *p, realT *hx,
int m,
int n,
void *adata )
534 size_t idx_mat, idx_dat;
538 for(
int j = 0; j < arr->
ny; j++ )
540 for(
int i = 0; i < arr->
nx; i++ )
542 idx_mat = i + j * arr->
nx;
562template <
typename _realT>
565 typedef _realT realT;
567 static const int maxNparams = 7;
569 static void func( realT *p, realT *hx,
int m,
int n,
void *adata )
573 size_t idx_mat, idx_dat;
575 realT G0 = arr->G0( p );
576 realT G = arr->G( p );
577 realT x0 = arr->x0( p );
578 realT y0 = arr->y0( p );
579 realT a = arr->a( p );
580 realT b = arr->b( p );
581 realT c = arr->c( p );
584 if( a * c - b * b <= 0 || a <= 0 || c <= 0 || a + c <= 2 * fabs( b ) )
588 if( arr->
mask ==
nullptr )
592 for(
int j = 0; j < arr->
ny; ++j )
594 for(
int i = 0; i < arr->
ny; ++i )
596 idx_mat = i + j * arr->
nx;
597 hx[idx_dat] = arr->
data[idx_mat];
604 for(
int j = 0; j < arr->
ny; ++j )
606 for(
int i = 0; i < arr->
ny; ++i )
608 idx_mat = i + j * arr->
nx;
609 hx[idx_dat] = arr->
data[idx_mat] * arr->
weights[idx_mat];
617 for(
int j = 0; j < arr->
ny; ++j )
619 for(
int i = 0; i < arr->
ny; ++i )
621 idx_mat = i + j * arr->
nx;
622 if( arr->
mask[idx_mat] == 0 )
626 hx[idx_dat] = arr->
data[idx_mat];
637 if( arr->
mask ==
nullptr )
641 for(
int j = 0; j < arr->
ny; ++j )
643 for(
int i = 0; i < arr->
nx; ++i )
645 idx_mat = i + j * arr->
nx;
655 for(
int j = 0; j < arr->
ny; ++j )
657 for(
int i = 0; i < arr->
nx; ++i )
659 idx_mat = i + j * arr->
nx;
670 for(
int j = 0; j < arr->
ny; ++j )
672 for(
int i = 0; i < arr->
nx; ++i )
674 idx_mat = i + j * arr->
nx;
676 if( arr->
mask[idx_mat] == 0 )
697 realT sx = arr->sigma_x( p );
698 realT sy = arr->sigma_y( p );
699 realT th = arr->theta( p );
712 realT na = arr->a( p );
713 realT nb = arr->b( p );
714 realT nc = arr->c( p );
717 arr->sigma_x( p, sx );
718 arr->sigma_y( p, sy );
733template <
typename realT>
739template <
typename realT>
747template <
typename realT>
764 Ag = im( (
int)xg, (
int)yg );
765 for(
int i = 0; i < 2 * maxWidth + 1; ++i )
767 for(
int j = 0; j < 2 * maxWidth + 1; ++j )
769 if( im( (
int)( xg0 - maxWidth + i ), (
int)( yg0 - maxWidth + j ) ) > Ag )
771 Ag = im( (
int)( xg0 - maxWidth + i ), (
int)( yg0 - maxWidth + j ) );
772 xg = xg0 - maxWidth + i;
773 yg = yg0 - maxWidth + j;
785 realT minD = widthWidth;
788 for(
int i = 0; i < nAngs; ++i )
793 for(
int j = 0; j < widthWidth; ++j )
795 if( im( (
int)( xg + j * c ), (
int)( yg + j * s ) ) <= 0.5 * Ag )
823 angG = 0.5 * ( minang + maxang );
831extern template int guessGauss2D_ang<float>(
float &Ag,
844extern template int guessGauss2D_ang<double>(
double &Ag,
Wrapper for a native array to pass to mx::math::fit::levmarInterface, with 1D Gaussian details.
Wrapper for a native array to pass to mx::math::fit::levmarInterface, with 2D Gaussian details.
realT G()
Get the peak scaling.
realT sigma()
Return sigma.
void setArray(realT *data, realT *coords, int nx)
Set the data aray.
realT G0()
Get the current value of G0, the constant.
void setGuess(realT G0, realT G, realT x0, realT sigma)
Set the initial guess for a symmetric Gaussian.
void setFixed(bool G0, bool G, bool x0, bool sigma)
Set whether each parameter is fixed.
realT x0()
Get the center x-coordinate.
void setArray(realT *data, int nx)
Set the data aray.
Class to manage fitting a 2D Gaussian to data via the levmarInterface.
void setGuess(realT G0, realT G, realT x0, realT y0, realT sigma)
Set the initial guess for a symmetric Gaussian.
void setFixed(bool G0, bool G, bool x0, bool y0, bool sigma_x, bool sigma_y, bool theta)
Set whether each parameter is fixed.
array2FitGaussian2D< realT > arr
void setArray(realT *data, int nx, int ny)
Set the data aray.
void setGuess(realT G0, realT G, realT x0, realT y0, realT sigma_x, realT sigma_y, realT theta)
Set the initial guess for the general Gaussian.
realT fwhm()
Return the full-width at half maximum.
void setFixed(bool G0, bool G, bool x0, bool y0, bool sigma)
Set whether each parameter is fixed.
void setArray(realT *data, int nx, int ny, realT *mask)
Set the data aray, with a mask.
int fit()
Perform the fit.
realT * init_p
Parameter array on input, saved for comparison.
Tools for using the eigen library for image processing.
Declarations for utilities related to the Gaussian function.
Eigen::Array< scalarT, -1, -1 > eigenImage
Definition of the eigenImage type, which is an alias for Eigen::Array.
int guessGauss2D_ang(realT &Ag, realT &xg, realT &yg, realT &xFWHM, realT &yFWHM, realT &angG, mx::improc::eigenImage< realT > &im, realT maxWidth, realT widthWidth, realT nAngs, realT xg0, realT yg0)
Form an estimate of the parameters of an elliptical Gaussian from a 2D image.
fitGaussian2D< mx::math::fit::gaussian2D_sym_fitter< realT > > fitGaussian2Dsym
Alias for the fitGaussian2D type fitting the symmetric gaussian.
fitGaussian2D< mx::math::fit::gaussian2D_gen_fitter< realT > > fitGaussian2Dgen
Alias for the fitGaussian2D type fitting the general elliptical gaussian.
realT gaussian2D(const realT x, const realT y, const realT G0, const realT G, const realT x0, const realT y0, const realT sigma)
Find value at position (x,y) of the 2D arbitrarily-centered symmetric Gaussian.
floatT sigma2fwhm(floatT sig)
Convert from Gaussian width parameter to FWHM.
realT gaussian(const realT x, const realT G0, const realT G, const realT x0, const realT sigma)
Find value at position (x) of the 1D arbitrarily-centered symmetric Gaussian.
void gaussian2D_gen2rot(realT &sigma_x, realT &sigma_y, realT &theta, const realT a, const realT b, const realT c)
Convert from (a,b,c) to ( , , ) for the elliptical Gaussian.
void gaussian2D_rot2gen(realT &a, realT &b, realT &c, const realT sigma_x, const realT sigma_y, const realT theta)
Convert from ( , , ) to (a,b,c) for the elliptical Gaussian.
constexpr T pi()
Get the value of pi.
constexpr T two_pi()
Get the value of 2pi.
A c++ interface to the templatized levmar minimization routines..
Declarations of some libarary wide utilities.
Wrapper for a native array to pass to levmarInterface, with !D Gaussian details.
size_t m_nx
X dimension of the array.
realT * m_mask
Pointer to the (optional) mask array. Any 0 pixels are excluded from the fit.
realT * m_coords
Pointer to the array of x values (optional).
realT * m_data
///< Pointer to the array of y values
Wrapper for a native array to pass to levmarInterface, with 2D Gaussian details.
size_t ny
Y dimension of the array.
size_t nx
X dimension of the array.
realT * data
Pointer to the array.
realT * mask
Pointer to the (optional) mask array. Any 0 pixels are excluded from the fit.
Wrapper for a native array to pass to levmarInterface.
size_t ny
X dimension of the array.
size_t nx
Pointer to the array.
levmarInterface fitter structure for the symmetric Gaussian.
levmarInterface fitter structure for the general elliptical Gaussian.
Alias for the fitGaussian1D type fitting the gaussian.
void paramNormalizer(array2FitGaussian2D< realT > *arr, realT *p, int dir)
Does nothing in this case.