Author Topic: noise stddev estimation in PCL  (Read 5214 times)

Offline zvrastil

  • PixInsight Addict
  • ***
  • Posts: 179
    • Astrophotography
noise stddev estimation in PCL
« on: 2011 July 03 12:12:31 »
Hi,

what is the best way to obtain estimation of standard deviation of noise in the image using PCL?

thanks in advance, Zbynek

Offline Juan Conejero

  • PTeam Member
  • PixInsight Jedi Grand Master
  • ********
  • Posts: 7111
    • http://pixinsight.com/
Re: noise stddev estimation in PCL
« Reply #1 on: 2011 July 03 13:11:21 »
Hi Zbynek,

The best way is by using the MRS noise estimation algorithm. See pcl/ATrousWaveletTransform.h for references and documentation on these routines; in particular, look for the NoiseKSigma() and NoiseMRS() member functions.

The following snippet is an adaptation of actual code from the ImageIntegration tool:

Code: [Select]
/*
 * 5x5 B3-spline wavelet scaling function. Used by the noise estimation
 * routines.
 *
 * Kernel filter coefficients:
 *
 *   1.0/256, 1.0/64, 3.0/128, 1.0/64, 1.0/256,
 *   1.0/64,  1.0/16, 3.0/32,  1.0/16, 1.0/64,
 *   3.0/128, 3.0/32, 9.0/64,  3.0/32, 3.0/128,
 *   1.0/64,  1.0/16, 3.0/32,  1.0/16, 1.0/64,
 *   1.0/256, 1.0/64, 3.0/128, 1.0/64, 1.0/256
 *
 * Note that we use this scaling function as a separable filter (row and column
 * vectors) for performance reasons.
 */
// Separable filter coefficients.
const float __5x5B3Spline_hv[] = { 0.0625F, 0.25F, 0.375F, 0.25F, 0.0625F };
// Gaussian noise scaling factors
const float __5x5B3Spline_kj[] =
{ 0.8907F, 0.2007F, 0.0856F, 0.0413F, 0.0205F, 0.0103F, 0.0052F, 0.0026F, 0.0013F, 0.0007F };

static const int __mrsNoiseEvaluationLayers = 3;
static const float __mrsMinDataFraction = 0.02;

// ...

template <class P>
static FVector NoiseSigma( const Generic2DImage<P>& img )
{
   FVector noise( img.NumberOfNominalChannels() );

   SeparableFilter H( __5x5B3Spline_hv, __5x5B3Spline_hv, 5 );
   ATrousWaveletTransform W( H, __mrsNoiseEvaluationLayers );

   SpinStatus spin;
   img.SetStatusCallback( &spin );
   img.Status().Initialize( "MRS noise evaluation" );
   img.Status().DisableInitialization();

   for ( int c = 0; c < img.NumberOfNominalChannels(); ++c )
   {
      img.SelectChannel( c );

      W << img;
      double s0 = W.NoiseKSigma( 0 )/__5x5B3Spline_kj[0];
      size_type N;

      noise[c] = W.NoiseMRS( ImageVariant( &img ), __5x5B3Spline_kj, s0, 3, &N );

      if ( noise[c] == 0 )
      {
         console.WriteLn( "<end><cbr>** Warning: No convergence in MRS noise estimation routine"
                          " - using k-sigma noise estimate." );
         noise[c] = s0;
      }
      else if ( N < __mrsMinDataFraction*img.NumberOfPixels() )
      {
         console.WriteLn( "<end><cbr>** Warning: No significant data in MRS noise estimation routine"
                          " - using k-sigma noise estimate." );
         noise[c] = s0;
      }
   }

   img.Status().Complete();
   img.Status().EnableInitialization();
   img.SetStatusCallback( 0 );

   return noise;
}

The code above is perfectible (it is a sum of fragment out of their actual context). In particular, the above routine doesn't need to be a template (you can work with ImageVariant). I hope you get the idea, though. Let me know if you need more information.
Juan Conejero
PixInsight Development Team
http://pixinsight.com/

Offline zvrastil

  • PixInsight Addict
  • ***
  • Posts: 179
    • Astrophotography
Re: noise stddev estimation in PCL
« Reply #2 on: 2011 July 03 13:13:43 »
Hi Juan,

thanks for the code, this is just what I need.

regards, Zbynek

Offline Nocturnal

  • PixInsight Jedi Council Member
  • *******
  • Posts: 2727
    • http://www.carpephoton.com
Re: noise stddev estimation in PCL
« Reply #3 on: 2011 July 03 13:35:54 »
But that only works on dark subtracted uniformly illuminated images, right? You need to get rid of fixed pattern signal before you have a chance at estimating noise.
Best,

    Sander
---
Edge HD 1100
QHY-8 for imaging, IMG0H mono for guiding, video cameras for occulations
ASI224, QHY5L-IIc
HyperStar3
WO-M110ED+FR-III/TRF-2008
Takahashi EM-400
PIxInsight, DeepSkyStacker, PHD, Nebulosity

Offline Juan Conejero

  • PTeam Member
  • PixInsight Jedi Grand Master
  • ********
  • Posts: 7111
    • http://pixinsight.com/
Re: noise stddev estimation in PCL
« Reply #4 on: 2011 July 03 13:55:29 »
It depends. For example, in the ImageCalibration tool I use these noise evaluation algorithms precisely to detect thermal noise, as part of the dark frame optimization routine. From the perspective of these algorithms, dark 'signal' is just noise due to its spatial distribution.

What you say is true if what one wants to evaluate is the noise originated from other sources (read noise + photon noise + ... ); from this perspective, thermal noise is 'unwanted signal'.
Juan Conejero
PixInsight Development Team
http://pixinsight.com/

Offline Nocturnal

  • PixInsight Jedi Council Member
  • *******
  • Posts: 2727
    • http://www.carpephoton.com
Re: noise stddev estimation in PCL
« Reply #5 on: 2011 July 03 14:29:25 »
I'm kind of a stickler for definitions in this context. A measurement (ADU value in out case) is signal plus noise. Just because dark signal is undesired doesn't make it all 'noise'. It's dark signal plus the uncertainty, noise.

It's really important not to call dark signal 'noise' because it implies you can subtract that noise by subtracting a master dark and that's not true. In fact subtracting a master dark from a light frame is going to decrease SNR (add noise) even if the resulting image appears cleaner because fixed pattern dark signal (say gradients in the dark signal or other patterns) are removed.
Best,

    Sander
---
Edge HD 1100
QHY-8 for imaging, IMG0H mono for guiding, video cameras for occulations
ASI224, QHY5L-IIc
HyperStar3
WO-M110ED+FR-III/TRF-2008
Takahashi EM-400
PIxInsight, DeepSkyStacker, PHD, Nebulosity