Ankan Ghosh commited on
Commit
6583bf5
1 Parent(s): 3a2c851

Upload billboard.py

Browse files
Files changed (1) hide show
  1. billboard.py +82 -0
billboard.py ADDED
@@ -0,0 +1,82 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import cv2
2
+ import numpy as np
3
+
4
+ # Create a named window for the display.
5
+ win_name = 'Destination Image'
6
+
7
+ # ------------------------------------------------------------------------------
8
+ # Define mouse callback function.
9
+ # ------------------------------------------------------------------------------
10
+ def mouse_handler(event, x, y, flags, data):
11
+ if event == cv2.EVENT_LBUTTONDOWN:
12
+ # Render points as yellow circles in destination image.
13
+ cv2.circle(data['img'], (x, y), radius=5, color=(0, 255, 255), thickness=-1, lineType=cv2.LINE_AA)
14
+ cv2.imshow(win_name, data['img'])
15
+ if len(data['points']) < 4:
16
+ data['points'].append([x, y])
17
+
18
+ # ------------------------------------------------------------------------------
19
+ # Define convenience function for retrieving ROI points in destination image.
20
+ # ------------------------------------------------------------------------------
21
+ def get_roi_points(img):
22
+ # Set up data to send to mouse handler.
23
+ data = {'img': img.copy(), 'points': []}
24
+ # Set the callback function for any mouse event.
25
+ cv2.imshow(win_name, img)
26
+ cv2.setMouseCallback(win_name, mouse_handler, data)
27
+ cv2.waitKey(0)
28
+
29
+ # Convert the list of four separate 2D ROI coordinates to an array.
30
+ roi_points = np.vstack(data['points']).astype(float)
31
+
32
+ return roi_points
33
+
34
+ # ------------------------------------------------------------------------------
35
+ # Main function to apply homography and warp images.
36
+ # ------------------------------------------------------------------------------
37
+ def apply_homography_and_warp(img_src, img_dst, roi_dst):
38
+ # Compute the coordinates for the four corners of the source image.
39
+ size = img_src.shape
40
+ src_pts = np.array([[0, 0], [size[1] - 1, 0], [size[1] - 1, size[0] - 1], [0, size[0] - 1]], dtype=float)
41
+
42
+ # Compute the homography.
43
+ h, status = cv2.findHomography(src_pts, roi_dst)
44
+
45
+ # Warp source image onto the destination image.
46
+ warped_img = cv2.warpPerspective(img_src, h, (img_dst.shape[1], img_dst.shape[0]))
47
+
48
+ # Black out polygonal area in destination image.
49
+ cv2.fillConvexPoly(img_dst, roi_dst.astype(int), 0, 16)
50
+
51
+ # Add the warped image to the destination image.
52
+ img_dst = img_dst + warped_img
53
+
54
+ return img_dst
55
+
56
+ # ------------------------------------------------------------------------------
57
+ # Main program
58
+ # ------------------------------------------------------------------------------
59
+
60
+ # # Read the source image.
61
+ # img_src = cv2.imread('Apollo-8-Launch.png')
62
+
63
+ # # Read the destination image.
64
+ # img_dst = cv2.imread('times_square.jpg')
65
+
66
+ # # Get four corners of the billboard
67
+ # print('Click on four corners of a billboard and then press ENTER')
68
+
69
+ # # Retrieve the ROI points from the user mouse clicks.
70
+ # roi_dst = get_roi_points(img_dst)
71
+
72
+ # # Apply homography and warp the images.
73
+ # result_img = apply_homography_and_warp(img_src, img_dst, roi_dst)
74
+
75
+ # # Display the updated image with the virtual billboard.
76
+ # cv2.imshow(win_name, result_img)
77
+
78
+ # # Wait for a key to be pressed to exit.
79
+ # cv2.waitKey(0)
80
+
81
+ # # Close the window.
82
+ # cv2.destroyAllWindows()