contrib/rpl/rgrl/rgrl_evaluator_ssd.cxx
Go to the documentation of this file.
00001 #include "rgrl_evaluator_ssd.h"
00002 //:
00003 // \file
00004 #include <vnl/vnl_math.h>
00005 #include <vcl_cassert.h>
00006 
00007 // Normalized SSD
00008 double
00009 rgrl_evaluator_ssd::
00010 evaluate( vcl_vector< double > const& a,
00011           vcl_vector< double > const& b,
00012           vcl_vector< double > const& weight ) const
00013 {
00014   assert( a.size() == b.size() && a.size() > 2);
00015 
00016   double sum_a = 0, sum_b = 0;
00017   for ( unsigned i = 0; i < a.size(); ++i ) {
00018     sum_a += a[ i ];
00019     sum_b += b[ i ];
00020   }
00021   double mean_a = sum_a / a.size();
00022   double mean_b = sum_b / b.size();
00023   double sigma_a = 0, sigma_b = 0;
00024 
00025   for ( unsigned i = 0; i < a.size(); ++i ) {
00026     sigma_a += vnl_math_sqr( a[ i ] - mean_a );
00027     sigma_b += vnl_math_sqr( b[ i ] - mean_b );
00028   }
00029 
00030   sigma_a = vcl_sqrt( sigma_a / (a.size()-1) );
00031   sigma_b = vcl_sqrt( sigma_b / (b.size()-1) );
00032 
00033   double f = 0;
00034   double aa, bb;
00035   for ( unsigned i = 0 ; i < a.size(); ++i ) {
00036 
00037     // The region is constant
00038     if ( sigma_a == 0 )
00039       aa = 0;
00040     else
00041       aa = ( a[ i ] - mean_a ) / sigma_a;
00042 
00043     if ( sigma_b == 0 )
00044       bb = 0;
00045     else
00046       bb = ( b[ i ] - mean_b ) / sigma_b;
00047 
00048     double ff = (double) vnl_math_sqr ( aa - bb ) / a.size();
00049     f += weight[i] * vcl_sqrt ( ff );
00050   }
00051 
00052   return f;
00053 }