Reputation: 6592
I need to calculate the correlation between two binary images in Python. The script should return 1 if the matrices are identical, and 0 if they are totally uncorrelated. It should be something similar to corr2
in Matlab (http://www.mathworks.se/help/images/ref/corr2.html). Here is the test I am using:
import numpy as np
from scipy import signal
A = np.matrix('1 0; 1 0')
B = np.matrix('1 0; 1 0')
cor = signal.correlate2d(A, B)
print cor
How could I get a single value instead of a matrix?
Upvotes: 1
Views: 2932
Reputation: 12689
Try corrcoef
. It will return a 2*2 matrix with the non-diagonal element correlation coefficient between the two matrices:
import numpy as np
A = np.matrix('1 1; 1 0')
B = np.matrix('1 0; 1 0')
cor = np.corrcoef(A.reshape(-1), B.reshape(-1))[0][1]
print cor
Upvotes: 1