Statistical comparison of two 2d matrices, corr2 or xcorr or other?

27 views (last 30 days)
Hi everybody,
I'm trying to compare 2d matrices (just 2 at a time) of identical sizes to see if they are similar, what I mean by similar is that high and low values appear in similar areas in each matrix (i.e. they share a similar distribution but not necessarily similar values, although I'm open to an approach which would require similar values too).
Currently I'm using corr2 but this seems the results of this don't always seem to be the most obvious; matrices which I think are very similar don't get very high correlations but matrices which are only vaguely similar get high(ish) correlation values. I think part of the problem is that my matrices contain a lot of zero values, so it only takes some low numbers in the right places to create in implied correlation.
I was wondering if anybody knows of or could recommend an alternative approach. I was wondering about xcorr, from the description it seems to do what I want, but in practice I can't really understand it, I'm not sure why it returns a matrix the same size as the inputs or what the values contained are.
Ideally I would like a test which takes two matrices of identical size and ouputs a single value signifying their similarity.
Thanks in advance for any help,
Rod.

Answers (1)

Image Analyst
Image Analyst on 13 Jul 2014
A common way of measuring similarity of 2D matrices (images) is ssim(), which is in the Image Processing Toolbox. You could also use psnr(), , or median absolute deviation, or mean absolute deviation, or something of your own choosing such as comparing the mean and standard deviation.
  4 Comments
Right Grievous
Right Grievous on 23 Jul 2014
Edited: Right Grievous on 23 Jul 2014
That's a very nice demo, thank you, and the Wikipedia article actually helped, I work quite a bit with electrical signals but was not aware MSE and PSNR could be calculated like this for an image.
I can see how these could be used to compare a noisy image with an original, do you know if it is ever used to compare the similarity of completely different images though (with the same dimensions and color space)?
I would love to use SSIM too, I found a code for this on the file exchange, however, it doesn't seem to work when there are NaN values. Is it not possible to compute when NaNs are present? When the means and std deviations are calculated for the different windows, why can't they be calculated ignoring nans?
Again, thank you very much for your help!
EDIT
Also, when calculating my psnr, should the maximum possible image value be 256? The data in my matrices can contain any values, can I set my maximum possible value to be the maximum observed value in the matrices?
- I think I can use ind2gray to convert my matrices to intensity maps with a range [0 1] to solve that problem
Image Analyst
Image Analyst on 24 Jul 2014
SSIM can be used to compare different images. But after a while it doesn't make sense. Let's say you compared lena to baboon and got 100, and then you compared cameraman to moon and got 200. Does that mean that lena is twice as similar to baboon as cameraman is to moon? No. They were not meant for that. These kinds of measures are usually used for denoising and compression algorithm comparisons and are generally valid only for smaller values, not huge values.
Not sure how you'd handle nans. Perhaps you can convert to a 1D vector and remove them, or else set them to zero.
I think you'd use the max value for your data type. Like I said, they're mostly used for compression comparisons so that's why.
I don't think ind2gray() would be of use here.

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!