import numpy as np

a = np.array([\
    [0,0,0,1,0],\
    [0,0,0,1,0],\
    [0,0,1,0,0],\
    [0,1,0,1,0],\
    [0,1,0,0,1]\
])

ashift = (2,2) # (shift in columns, shift in raws)

b = np.array([\
    [1,0,0,0,0],\
    [0,1,0,0,0],\
    [0,0,1,0,0],\
    [0,0,0,0,0],\
    [0,0,0,0,0]\
])

c = np.zeros(a.shape)
for i in range(0,5):
    for j in range(0,5):
        #c[i,j]=np.sum((a-np.roll(b,(j,i),axis=(1,0)))**2) # look for zero
        c[i,j]=np.sum(a*np.roll(b,(j,i),axis=(1,0))) # look for maximum
print(c)

cshift = np.unravel_index(np.argmax(c), c.shape)
# now cshift == ashift
print(cshift,ashift)

fa = np.fft.fft2(a)
fb = np.fft.fft2(b)
fd = np.multiply(fa,np.conj(fb))
d = np.fft.ifft2(fd)
# now c == d
print(d)

dshift = np.unravel_index(np.argmax(np.real(d)), d.shape)
# now dshift == ashift
print(dshift,ashift)
        


