ClementPinard / FlowNetPytorch

Pytorch implementation of FlowNet by Dosovitskiy et al.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

The idea of visualizing the flow map

RokiaAbdeen opened this issue · comments

can you explain how this code converts the flow feature map output into RGB image values?? I am really confused and I can't understand it :
def flow2rgb(flow_map, max_value):
flow_map_np = flow_map.detach().cpu().numpy()
_, h, w = flow_map_np.shape
flow_map_np[:,(flow_map_np[0] == 0) & (flow_map_np[1] == 0)] = float('nan')
rgb_map = np.ones((3,h,w)).astype(np.float32)
if max_value is not None:
normalized_flow_map = flow_map_np / max_value
else:
normalized_flow_map = flow_map_np / (np.abs(flow_map_np).max())
rgb_map[0] += normalized_flow_map[0]
rgb_map[1] -= 0.5*(normalized_flow_map[0] + normalized_flow_map[1])
rgb_map[2] += normalized_flow_map[1]
return rgb_map.clip(0,1)

This is a convenience function for flow vizualisation but nothing too formal. The main principle is to consider the image as a YUV space, where Y is always 1, and U and V are the optical flow vector coordinates.
The operation would be

yuv_map = np.ones((3,h,w)).astype(np.float32)
yuv_map[0] = 1
yuv_map[1] = flow_map[0]
yuv_map[2] = flow_map[1]
rgb_map = convert_to_rgb(yuv_map)

Where the function convert_to_rgb would do the same operation as explained here
Here, we simply directly apply both operations in the same step, with a simplified version of rgb to yuv conversion, where the conversion matrix is

[[1,    1,    0],
 [1, -0.5, -0.5],
 [1,    0,    1]]