import numpy as np
import cv2
from time import time

a0 = cv2.imread('bin_images\dom.png',0)
b0 = cv2.imread('bin_images\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)

s = time()
print("START")
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




cshift = np.unravel_index(np.argmax(c), c.shape)
# now cshift == ashift
print(cshift,ashift)
print(f'cas = {s - time()}')

s = time()

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

dshift = np.unravel_index(np.argmax(np.real(d)), d.shape)
# now dshift == ashift
print(dshift,ashift)
print(f'cas = {s - time()}')      


