# Synopse

Simple correlation between one image and all images of the same size in an array

• corr= iacorr(X, Yi)
• X, Yi: input images
• corr: correlation between X and each Yi
Where:
• E[] is the expected value operator
• cov means covariance
• µ is the mean value
• σ is standard deviation
```01. def iacorr(X, Y):
02.     import numpy as np
03.
04.     if X.size == Y.size:
05.         X = X.ravel()
06.         Y = Y.ravel()
07.
08.         Xm = X.mean()
09.         Ym = Y.mean()
10.         Xc = X - Xm
11.         Yc = Y - Ym
12.         cov = (Xc*Yc).mean()
13.         v1 = (Xc*Xc).mean()
14.         v2 = (Yc*Yc).mean()
15.     else:
16.
17.         if Y.ndim == (X.ndim + 1):
18.             # m is the number of pixels of X
19.             n = Y.shape[0]              # number of images in Y
20.             X = X.ravel().reshape(1,-1) # shape (1,m)
21.             Y = Y.reshape(n,-1)         # shape (n,m)
22.
23.             Xm = X.mean()                     # scalar
24.             Ym = Y.mean(axis=1,keepdims=True) # shape (n,1)
25.             Xc = X - Xm                       # shape (1,m)
26.             Yc = Y - Ym                # broadcast  shape (n,m)-(n,1) -> (n,m)
27.             cov = (Xc*Yc).mean(axis=1) # broadcast  shape (1,m)*(n,m) -> (n,)
28.             v1 = (Xc*Xc).mean()        # scalar
29.             v2 = (Yc*Yc).mean(axis=1)  # reduction (n,m) -> (n,)
30.
31.     return cov/(np.sqrt(v1*v2))```

# Example

## Correlation between two images

```01. import numpy as np
02. import ia636 as ia
03.
04. a = arange(4*5).reshape((4,5))
05. c1 = ia.iacorr(a,a)
06. c2 = ia.iacorr(a,a[:,::-1])
07. c3 = ia.iacorr(a, -10 * a + 100)
08. c4 = ia.iacorr(a,-a)
09. c5 = ia.iacorr(a,a+100)
10. c6 = ia.iacorr(a, -10*a)
11. c7 = ia.iacorr(a, ones(a.shape))
12. c8 = ia.iacorr(a,zeros(a.shape))
13.
14. print 'a=\n',
15. print 'c1', c1
16. print 'c2', c2
17. print 'c3', c3
18. print 'c4', c4
19. print 'c5', c5
20. print 'c6', c6
21. print 'c7', c7
22. print 'c8', c8```
```a=
c1 1.0
c2 0.87969924812
c3 -1.0
c4 -1.0
c5 1.0
c6 -1.0
c7 nan
c8 nan
```

## Correlation between one image and many images

```1. import ia636 as ia
2.
3. X = np.arange(4*5).reshape((4,5))
4. XX = X[np.newaxis,:,:]
5. Y = np.concatenate((XX,XX[:,:,::-1],-10 * XX + 100, np.ones_like(XX)),axis=0)
6. c = ia.iacorr(X,Y)
7. print 'c=',c```
```c= [ 1.          0.87969925 -1.                 nan]
```

## Correlation between one signal and many signals

```1. X = np.arange(4*5)
2. XX = X[np.newaxis,:]
3. Y = np.concatenate((XX,XX*XX,-10 * XX + 100, np.ones_like(XX)),axis=0)
4. c = ia.iacorr(X,Y)
5. print 'c=',c```
```c= [ 1.          0.96532553 -1.                 nan]
```