homography_book.py 879 B

12345678910111213141516171819202122232425262728293031
  1. #!/usr/bin/env python
  2. import cv2
  3. import numpy as np
  4. if __name__ == '__main__' :
  5. # Read source image.
  6. im_src = cv2.imread('book2.jpg')
  7. # Four corners of the book in source image
  8. pts_src = np.array([[141, 131], [480, 159], [493, 630],[64, 601]], dtype=float)
  9. # Read destination image.
  10. im_dst = cv2.imread('book1.jpg')
  11. # Four corners of the book in destination image.
  12. pts_dst = np.array([[318, 256],[534, 372],[316, 670],[73, 473]], dtype=float)
  13. # Calculate Homography
  14. h, status = cv2.findHomography(pts_src, pts_dst)
  15. # Warp source image to destination based on homography
  16. im_out = cv2.warpPerspective(im_src, h, (im_dst.shape[1],im_dst.shape[0]))
  17. # Display images
  18. cv2.imshow("Source Image", im_src)
  19. cv2.imshow("Destination Image", im_dst)
  20. cv2.imshow("Warped Source Image", im_out)
  21. cv2.waitKey(0)