perspective-correction.py 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. #!/usr/bin/env python
  2. import cv2
  3. import numpy as np
  4. from utils import get_four_points
  5. if __name__ == '__main__' :
  6. # Read in the image.
  7. im_src = cv2.imread("book1.jpg")
  8. # Destination image
  9. size = (300,400,3)
  10. im_dst = np.zeros(size, np.uint8)
  11. pts_dst = np.array(
  12. [
  13. [0,0],
  14. [size[0] - 1, 0],
  15. [size[0] - 1, size[1] -1],
  16. [0, size[1] - 1 ]
  17. ], dtype=float
  18. )
  19. print '''
  20. Click on the four corners of the book -- top left first and
  21. bottom left last -- and then hit ENTER
  22. '''
  23. # Show image and wait for 4 clicks.
  24. cv2.imshow("Image", im_src)
  25. pts_src = get_four_points(im_src);
  26. # Calculate the homography
  27. h, status = cv2.findHomography(pts_src, pts_dst)
  28. # Warp source image to destination
  29. im_dst = cv2.warpPerspective(im_src, h, size[0:2])
  30. # Show output
  31. cv2.imshow("Image", im_dst)
  32. cv2.waitKey(0)