Guest User

Untitled

a guest
Sep 4th, 2018
20
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.11 KB | None | 0 0
  1. import cv2
  2. import numpy as np
  3.  
  4. #load image
  5. img = cv2.imread('homography-test.jpg', cv2.IMREAD_COLOR)
  6.  
  7. #corners of book covers (before)
  8. frontCoverPtsBefore = np.array([[32, 48], [279, 136], [247, 430], [39, 281]], dtype="float32")
  9. backCoverPtsBefore = np.array([[279, 136], [474, 36], [463, 316], [247, 430]], dtype="float32")
  10.  
  11. #corners of book covers (after)
  12. frontCoverPtsAfter = np.array([[0, 0], [299, 0], [299, 599], [0, 599]], dtype="float32")
  13. backCoverPtsAfter = np.array([[300, 0], [599, 0], [599, 599], [300, 599]], dtype="float32")
  14.  
  15. #get the transformation matrices for both covers
  16. M_front = cv2.getPerspectiveTransform(frontCoverPtsBefore, frontCoverPtsAfter)
  17. M_back = cv2.getPerspectiveTransform(backCoverPtsBefore, backCoverPtsAfter)
  18.  
  19. #warpPerspective both images
  20. img_front = cv2.warpPerspective(img, M_front, (600, 600))
  21. img_back = cv2.warpPerspective(img, M_back, (600, 600))
  22.  
  23. #copy half of the warped back cover into the warped front cover
  24. np.copyto(img_front[:, 300:, :], img_back[:, 300:, :])
  25.  
  26. #display before and after
  27. cv2.imshow('img', img)
  28. cv2.imshow('img_front', img_front)
  29. cv2.waitKey(0)
  30. cv2.destroyAllWindows()
Add Comment
Please, Sign In to add comment