Author Topic: IKSS available in javascript?  (Read 350 times)

Offline jonyepsilon

  • Newcomer
  • Posts: 2
    • View Profile
IKSS available in javascript?
« on: 2019 April 02 02:51:55 »
Hi there,

 quick question: is there a straightforward way to calculate the IKSS scale and location estimates for an image from javascript?

I see there was a forum topic on this a while back (https://pixinsight.com/forum/index.php?topic=6442.0) but there didn't seem to be a conclusion there. I guess if push comes to shove, I could expose the IKSS code (https://github.com/PixInsight/PCL/blob/master/src/modules/processes/ImageIntegration/ImageIntegrationInstance.cpp#L370) manually, but I wonder if there's an easier way?

Context: trying to do some quantitative work on batches of images.

Thanks,


Jony

Offline Juan Conejero

  • PTeam Member
  • PixInsight Jedi Grand Master
  • ********
  • Posts: 6690
    • View Profile
    • http://pixinsight.com/
Re: IKSS available in javascript?
« Reply #1 on: 2019 April 04 02:34:22 »
Hi Jony,

The IKSS estimator is not available on the core JavaScript runtime, so you'll have to implement it in JavaScript. For your convenience, here you have it:

Code: [Select]
/*
 * Iterative k-sigma Estimator of Location and Scale (IKSS).
 *
 * Parameters:
 *
 * x     A Vector object with the sample values.
 * eps   Fractional accuracy. The default value is 1e-6.
 *
 * Returns an Array object of the form:
 *
 * [L,S,N,I]
 *
 * where:
 *
 * L is the location estimate.
 * S is the scale estimate.
 * N is the number of non-rejected samples used for estimation.
 * I is the number of iterations performed.
 *
 * If the algorithm does not converge to significant values, one or both of the
 * returned location and scale estimates will be zero.
 */
function IKSS( x, eps )
{
   if ( eps == undefined )
      eps = 1.0e-06;
   x.sort();
   var N = x.length;
   var i = 0;
   var j = N;
   var s0 = 1;
   for ( var it = 1; ; ++it )
   {
      if ( j - i < 1 )
         return [0,0,0,it];
      var v = new Vector( x, i, j );
      var m = v.median();
      var s = Math.sqrt( v.BWMV( m ) );
      if ( 1 + s == 1 )
         return [m,0,j-i,it];
      if ( (s0 - s)/s < eps )
         return [m,0.991*s,j-i,it];
      s0 = s;
      for ( var x0 = m - 4*s; x.at( i ) < x0; ++i ) {}
      for ( var x1 = m + 4*s; x.at( j-1 ) > x1; --j ) {}
   }
}

To use this function for an image, you have to gather pixel samples into a Vector object, as a result of some limitations in the Math JavaScript object implemented in current versions (this will change in the next version of PixInsight, where a Vector object won't be required, so the IKSS function above will be able to work directly with an Array). For example:

Code: [Select]
function ikssOfImage( image )
{
   var A = new Array;
   image.getSamples( A );
   return IKSS( new Vector( A ) );
}

You'll have to iterate through a set of files, loading the images, calling the above functions, and working with the results. Let me know if you also need help with this.
Juan Conejero
PixInsight Development Team
http://pixinsight.com/

Offline jonyepsilon

  • Newcomer
  • Posts: 2
    • View Profile
Re: IKSS available in javascript?
« Reply #2 on: 2019 April 04 18:01:28 »
Thanks! This is extremely helpful :-)


Jony