RSS
 

OpenCV: The cross correlation of two opencv images

11 Jun

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;
}
 
7 Comments

Posted in OpenCV

 

Leave a Reply

 

 
  1. KonstantinMiller

    July 7, 2009 at 7:46 am

    Hi! I like your srticle and I would like very much to read some more information on this issue. Will you post some more?

     
  2. admin

    July 8, 2009 at 2:30 pm

    Just tell me what you want and I will do

     
  3. Witek

    August 1, 2009 at 8:14 am

    What is BwImage? The code won’t compile as a standard Opencv project :(

     
  4. admin

    August 1, 2009 at 10:55 pm

    BwImage is a black and white C++ warper, you can see this reference:
    http://opencv.willowgarage.com/wiki/faq#Howtoaccessimagepixels
    Or you can use any other way to access a pixel in the image

     
  5. admin

    August 1, 2009 at 11:03 pm

     
  6. Bradly Krallis

    September 6, 2010 at 6:55 pm

    This post is usefull.

     
  7. ntp for windows

    September 28, 2010 at 10:16 am

    This post is usefull.