import numpy as np
import cv2

a0 = cv2.imread('dom.png',0)
b0 = cv2.imread('okno.png',0)

a = 1 - a0 / 255
b = np.zeros(a.shape)
b[0:b0.shape[0],0:b0.shape[1]] = 1 - b0 / 255

ashift = (0,0) # (shift in columns, shift in raws)

c = np.zeros(a.shape)
for i in range(0,a.shape[0]):
    for j in range(0,a.shape[1]):
        #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)
        


