Ask Your Question
0

how to find correlation coefficient for two images?

asked 2013-06-27 09:13:05 -0600

chris_chris gravatar image

I have two 16-Bit grey level images in cv::Mat format. I just have to find the correlation coefficient scores i.e) a score says how much the two images match each other.

Is there any functions in opencv or any other libraries to find it?

edit retag flag offensive close merge delete

3 answers

Sort by ยป oldest newest most voted
1

answered 2016-03-15 09:20:20 -0600

epitalon gravatar image

I know that this thread is old but here is an answer for those who continuously browse the internet in search of answers.

The following code works with Opencv 3.0 and it should work with opencv 2.4 with few changes.

double correlation(cv::Mat &image_1, cv::Mat &image_2)   {

// convert data-type to "float"
cv::Mat im_float_1;
image_1.convertTo(im_float_1, CV_32F);
cv::Mat im_float_2;
image_2.convertTo(im_float_2, CV_32F);

int n_pixels = im_float_1.rows * im_float_1.cols;

// Compute mean and standard deviation of both images
cv::Scalar im1_Mean, im1_Std, im2_Mean, im2_Std;
meanStdDev(im_float_1, im1_Mean, im1_Std);
meanStdDev(im_float_2, im2_Mean, im2_Std);

// Compute covariance and correlation coefficient
double covar = (im_float_1 - im1_Mean).dot(im_float_2 - im2_Mean) / n_pixels;
double correl = covar / (im1_Std[0] * im2_Std[0]);

return correl;

}

edit flag offensive delete link more

Comments

Why do you have to convert the input images to float?

mhaghighat gravatar imagemhaghighat ( 2017-11-07 14:40:52 -0600 )edit

when computing "covar", we need to compute centered image, that is image minus im_mean, where im_mean is a floating point value.

epitalon gravatar imageepitalon ( 2017-11-08 08:08:07 -0600 )edit
1

answered 2015-07-07 08:58:07 -0600

CataRAy gravatar image

Hi, you can use the template matching function of opencv with the method: TM_CCOEFFThis will give you the coeffcient you need. Just use the second image as a template. Here is an example: [http://docs.opencv.org/doc/tutorials/...]

edit flag offensive delete link more

Comments

The function is matchTemplate, tm_ccoeff ist just a parameter for this function.

FooBar gravatar imageFooBar ( 2015-07-07 09:22:33 -0600 )edit
0

answered 2017-11-07 16:00:20 -0600

mhaghighat gravatar image

Assuming both image1 and image2 are the same size:

float correlation(cv::Mat &image1, cv::Mat &image2) 
{
    cv::Mat corr;
    cv::matchTemplate(image1, image2, corr, cv::TM_CCORR_NORMED);
    return corr.at<float>(0,0);  // corr only has one pixel
}
edit flag offensive delete link more

Comments

Can I ask what value returns it on the return line?

andane gravatar imageandane ( 2019-06-11 09:20:12 -0600 )edit

Question Tools

3 followers

Stats

Asked: 2013-06-27 09:13:05 -0600

Seen: 21,264 times

Last updated: Nov 07 '17