#!/usr/bin/env python

import cv2
import numpy as np

if __name__ == '__main__':
    # Read source image.
    im_src = cv2.imread('2020-03-16_142723_uprav.jpg')
    # Four corners of the book in source image
    pts_src = np.array([[1499, 652], [2801, 618], [1631, 3815], [2945, 3758]])

    # Read destination image.
    im_dst = cv2.imread('2020-03-16_142723_orez.jpg')
    # Four corners of the book in destination image.
    pts_dst = np.array([[1479, 631], [2781, 598], [1611, 3794], [2925, 3738]])

    # Calculate Homography
    h, status = cv2.findHomography(pts_src, pts_dst)
    np.savetxt('homography_matrix.txt', h)

    # Warp source image to destination based on homography
    im_out = cv2.warpPerspective(im_src, h, (im_dst.shape[1], im_dst.shape[0]))

    # Display images
    cv2.imshow("Source Image", im_src)
    cv2.imwrite("Source Image.jpg", im_src)
    cv2.imshow("Destination Image", im_dst)
    cv2.imwrite("Destination Image.jpg", im_dst)
    cv2.imshow("Warped Source Image", im_out)
    cv2.imwrite("Warped Source Image.jpg", im_out)

    cv2.waitKey(0)
