import cv2
import numpy as np
from matplotlib import pyplot as plt

def find_shift(img, obj):

    n_img = 1 - img / 255
    height, width = len(img) , len(img[0])

 
    pad_obj = 1 - obj / 255

    
    pad_obj = np.pad(pad_obj, ((0,height-len(obj)),(0,width-len(obj[0]))), 'constant')
      
    
    f1 = np.fft.fft2(n_img)
    f2 = np.fft.fft2(pad_obj)

    f_res = np.multiply(f1, np.conj(f2))

    res = np.fft.ifft2(f_res)
    
    idx = np.argmax(np.real(res))

    y, x = idx % width, idx // width

    return y, x
    

dom = cv2.imread('dom.png',0)
okno = cv2.imread('okno.png',0)

shift = find_shift(dom, okno)
print(shift)

res = cv2.cvtColor(dom,cv2.COLOR_GRAY2RGB)
cv2.rectangle(res,shift,(shift[0]+okno.shape[1],shift[1]+okno.shape[0]),(0,0,255),2)
cv2.imshow('found',res)
cv2.waitKey(0)

cv2.destroyAllWindows()
