import cv2
import numpy as np
def warp_flow(img, flow, binarize=True):
'''
img: h*w*c ndarray
flow: h*w*2 ndarray
'''
if flow is None:
return img
h, w = flow.shape[:2]
#flow = -flow
flow[:, :, 0] += np.arange(w)
flow[:, :, 1] += np.arange(h)[:, np.newaxis]
res = cv2.remap(img, flow, None, cv2.INTER_LINEAR)
if binarize:
res = np.equal(res, 1).astype(np.uint8)
return res