Correlation among 2 images
Solution 1
This is the function used to do correlation (coefficient) between two images (matrices):
r = corr2(A,B) computes the correlation coefficient between A and B, where A and B are matrices or vectors of the same size.
while xcorr2 (A, B) solves for CROSS correlation.
Solution 2
MATLAB has xcorr2
just for this purpose. I suppose your code would look something like:
r = xcorr2(f1, f2) / (P .^ 2)
Where f1
and f2
are the two images. The resulting matrix r
is a (2P-1)×(2P-1) matrix, and each of its elements reflect the measure of similarity between f1 and f2, when the two images are shifted by an offset corresponding to that element's offset from the center.
Note that if you're interested only in the correlation between two unshifted images, then you should save execution time and use corr2
, like @TheByzantine has suggested in his answer.
Solution 3
use the xcorr2 function. For example:
C=xcorr2(A,B)
user671805
Updated on July 03, 2020Comments
-
user671805 almost 4 years
I am trying to find the following correlation among two images f1 and f2 where the size of the image is PXP.
I have written a for loop program for the same but I think an inbuilt function would be faster for the same.
Which function in matlab can help me compute this ?
Also if the size of both the images are M X N can someone tell me how this formula will change or if the function will be able to handle it.
EDIT:
Is there any faster function than xcorr2 that can help me seeing that it takes too much time when I only need to have the value for correlation the unshifted images....
-
Serg over 11 yearsif you need correlation, it's just
dot product
between two arraysf(:)
after centering (mean and variance). This is very fast and is done bycorr2
: typeedit corr2
to see how. if you need cross-correlation, then there arexcorr2
orimfilter
, but they can't be fast because of the number of computations.
-
-
user671805 over 11 yearsFor both M X N images is it supposed to give me a matrix rather than a single number ?
-
Eitan T over 11 years@user671805 Yes, the resulting correlation matrix would be (2P-1)x(2P-1).
-
user671805 over 11 yearsBut it is supposed to be a single number since this measure has been used as a similarity measure between 2 images.... Or would it be sum(sum(r)) ?
-
Eitan T over 11 years@user671805 No,
r
is supposed to be a matrix, where each element reflects the measure of similarity betweenf1
andf2
, with the corresponding offset (phase) of that element from the center. For example, element (0, 0) shows the correlation between the images if one is shifted by P pixels in each axis relative to the other. This means that if you want a number that tells how similar the pictures without any shift, that would be the element in the (P, P) position. -
user671805 over 11 years@Eitan The link you have mentioned talks about sliding one matrix on top of another....Just to check , when the 2 matrices are exactly on top of each other, to get the co-relation would be equivalent to finding ( M , N ) position in xcorr2 ?
-
user671805 over 11 yearsHey, Is there a faster method seeing that I only want the unshifted value ? Its taking a huge time to compute...
-
Eitan T over 11 years@user671805 You should use
corr2
then, like @TheByzantine suggested. -
Eitan T over 11 yearsApparently that's what the OP wants, so +1 for you.