virtual-billboard.py 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. #!/usr/bin/env python
  2. import cv2
  3. import numpy as np
  4. from utils import mouse_handler
  5. from utils import get_four_points
  6. import sys
  7. if __name__ == '__main__' :
  8. # Read source image.
  9. im_src = cv2.imread('first-image.jpg');
  10. size = im_src.shape
  11. # Create a vector of source points.
  12. pts_src = np.array(
  13. [
  14. [0,0],
  15. [size[1] - 1, 0],
  16. [size[1] - 1, size[0] -1],
  17. [0, size[0] - 1 ]
  18. ],dtype=float
  19. );
  20. # Read destination image
  21. im_dst = cv2.imread('times-square.jpg');
  22. # Get four corners of the billboard
  23. print 'Click on four corners of a billboard and then press ENTER'
  24. pts_dst = get_four_points(im_dst)
  25. # Calculate Homography between source and destination points
  26. h, status = cv2.findHomography(pts_src, pts_dst);
  27. # Warp source image
  28. im_temp = cv2.warpPerspective(im_src, h, (im_dst.shape[1],im_dst.shape[0]))
  29. # Black out polygonal area in destination image.
  30. cv2.fillConvexPoly(im_dst, pts_dst.astype(int), 0, 16);
  31. # Add warped source image to destination image.
  32. im_dst = im_dst + im_temp;
  33. # Display image.
  34. cv2.imshow("Image", im_dst);
  35. cv2.waitKey(0);