I40 Answer ― RMS Difference


#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include "basicImage.c"

int main ( int argc, char* argv[] )
{
  image imgA, imgB;
  int r, c, a, b;
  double sumSQdiff=0.0;
  
  if ( argc != 3 )
  {
    printf( "RMSdifference imageA  imageB \n" );
    system( "pause" );
    exit( EXIT_FAILURE );
  }
  
  /* read in the  images */
  readPGMimage( &imgA, argv[1] );
  readPGMimage( &imgB, argv[2] );

  /* scan through the image */
  for ( r=0; r<imgA.nrows; r++ )
    for ( c=0; c<imgA.ncols; c++ )
    {
      a = getPixel( imgA, r, c ) ;
      b = getPixel( imgB, r, c ) ;
      sumSQdiff += (a-b)*(a-b) ;
    }

  /* square root of average squared difference */
  sumSQdiff = sumSQdiff/(imgA.nrows*imgA.ncols);
  printf("RMS difference: %lf\n", sqrt( sumSQdiff ) );
  
  /* free memory */
  freeImage( &imgA );
  freeImage( &imgB );
}

Comments: