MCPcopy Create free account
hub / github.com/drinkingcoder/NeuralMarker / image_flow_warp

Function image_flow_warp

core/utils/utils.py:344–364  ·  view source on GitHub ↗

Input: image: HxWx3 numpy flow: HxWx2 torch.Tensor Output: outImg: HxWx3 numpy

(image, flow, padding_mode='zeros')

Source from the content-addressed store, hash-verified

342 return mask
343
344def image_flow_warp(image, flow, padding_mode='zeros'):
345 '''
346 Input:
347 image: HxWx3 numpy
348 flow: HxWx2 torch.Tensor
349 Output:
350 outImg: HxWx3 numpy
351 '''
352 image = torch.from_numpy(image)
353 if image.ndim == 2:
354 image = image[None].permute([1,2,0])
355 H, W, _ = image.shape
356 coords = coords_grid(1, H, W).cuda().float().contiguous()
357 flow = flow[None].repeat(1, 1, 1, 1).permute([0, 3, 1, 2]).float().contiguous()
358 grid = (flow + coords).permute([0, 2, 3, 1]).contiguous()
359 grid[:, :, :, 0] = (grid[:, :, :, 0] * 2 - W + 1) / (W - 1)
360 grid[:, :, :, 1] = (grid[:, :, :, 1] * 2 - H + 1) / (H - 1)
361 image = image[None].permute([0, 3, 1, 2]).cuda().float()
362 outImg = F.grid_sample(image, grid, padding_mode=padding_mode, align_corners=False)[0].cpu().numpy().transpose([1, 2, 0])
363
364 return outImg.astype(np.uint8)
365
366def image_forward_warp(image, flow, padding_mode='zeros'):
367 '''

Callers 2

image_editingFunction · 0.90
visualizationFunction · 0.90

Calls 1

coords_gridFunction · 0.70

Tested by

no test coverage detected