This function calculates the cross correlation between two images of the type IplImage*. Returns the resulting value.
double cross_correlation( IplImage* img1, IplImage* img2 ){
double corr;
int M = img1->width;
int N = img1->height;
BwImage img_1( img1 ); // using opencv wrapper for accessing image pixels
BwImage img_2( img2 );
CvScalar img1_avg = cvAvg( img1, NULL );
CvScalar img2_avg = cvAvg( img2, NULL );
double sum_img1_img2 = 0;
double sum_img1_2 = 0;
double sum_img2_2 = 0;
for( int m=0; m<m ; ++m ){
for( int n=0; n<n; ++n ){
sum_img1_img2 = sum_img1_img2 + (img_1[m][n]-img1_avg.val[0])*(img_2[m][n]-img2_avg.val[0]);
sum_img1_2 = sum_img1_2 + (img_1[m][n]-img1_avg.val[0])*(img_1[m][n]-img1_avg.val[0]);
sum_img2_2 = sum_img2_2 + (img_2[m][n]-img2_avg.val[0])*(img_2[m][n]-img2_avg.val[0]);
}
}
corr = sum_img1_img2/sqrt(sum_img1_2*sum_img2_2);
return corr;
}