(estimator, marker, scene, frame, args, warp = 'homography')
| 70 | return outImg |
| 71 | |
| 72 | def blend(estimator, marker, scene, frame, args, warp = 'homography'): |
| 73 | H, W = 480, 640 |
| 74 | scene_ori_H, scene_ori_W = scene.shape[:2] |
| 75 | frame_ori_H, frame_ori_W = frame.shape[:2] |
| 76 | marker_ori_H, marker_ori_W = marker.shape[:2] |
| 77 | zero = np.zeros_like(marker) |
| 78 | |
| 79 | if frame_ori_H > frame_ori_W: |
| 80 | ratio = marker_ori_H / frame_ori_H |
| 81 | frame = cv2.resize(frame, None, fx=ratio, fy=ratio) |
| 82 | frame_H, frame_W = frame.shape[:2] |
| 83 | start_x = int(marker_ori_W/2 - frame_W/2) |
| 84 | zero[0:frame_H, start_x:start_x+frame_W] = frame |
| 85 | |
| 86 | else: |
| 87 | ratio = marker_ori_W / frame_ori_W |
| 88 | frame = cv2.resize(frame, None, fx=ratio, fy=ratio) |
| 89 | frame_H, frame_W = frame.shape[:2] |
| 90 | start_y = int(marker_ori_H/2 - frame_H/2) |
| 91 | zero[start_y:start_y+frame_H, 0:frame_W] = frame |
| 92 | frame = zero |
| 93 | |
| 94 | marker = cv2.resize(marker, (W, H)) |
| 95 | scene = cv2.resize(scene, (W, H)) |
| 96 | frame = cv2.resize(frame, (W, H)) |
| 97 | |
| 98 | flow = estimator.estimate(scene, marker) |
| 99 | frame = cv2.GaussianBlur(frame,(5,5),1,borderType=cv2.BORDER_CONSTANT) |
| 100 | |
| 101 | if warp == 'grid_sample': |
| 102 | out = image_flow_warp(frame, flow[0].permute([1,2,0]),padding_mode='zeros') |
| 103 | mask_origin = (np.ones(shape=(frame.shape[0], frame.shape[1], 1)) * 255).astype(np.uint8) |
| 104 | mask_origin = image_flow_warp(mask_origin, flow[0].permute([1,2,0]),padding_mode='zeros') |
| 105 | mask = mask_origin.astype(np.float64) / 255.0 |
| 106 | elif warp == 'homography': |
| 107 | flow = flow[0].permute([1,2,0]) |
| 108 | image = marker |
| 109 | image = torch.from_numpy(image) |
| 110 | if image.ndim == 2: |
| 111 | image = image[None].permute([1,2,0]) |
| 112 | H, W, _ = image.shape |
| 113 | coords = coords_grid(1, H, W).cuda().float().contiguous() |
| 114 | flow = flow[None].repeat(1, 1, 1, 1).permute([0, 3, 1, 2]).float().contiguous() |
| 115 | grid = (flow + coords).permute([0, 2, 3, 1]).contiguous() # (1, H, W, 2) |
| 116 | grid = grid[0].cpu() |
| 117 | src_pts = [] |
| 118 | dst_pts = [] |
| 119 | for y in range(H): |
| 120 | for x in range(W): |
| 121 | if grid[y,x,0]>=0 and grid[y,x,0]<W and grid[y,x,1]>=0 and grid[y,x,1]<H: |
| 122 | src_pts.append((grid[y,x,0], grid[y,x,1])) |
| 123 | dst_pts.append((x, y)) |
| 124 | src_pts = np.float32(src_pts) |
| 125 | dst_pts = np.float32(dst_pts) |
| 126 | |
| 127 | M, _ = cv2.findHomography(src_pts, dst_pts, cv2.RANSAC, 5.0) |
| 128 | out = cv2.warpPerspective(frame, M, (scene.shape[1], scene.shape[0])) |
| 129 | mask_origin = (np.ones(shape=(frame.shape[0], frame.shape[1], 1)) * 255).astype(np.uint8) |
no test coverage detected