Visualize Optical Flow with color model

Dark_Daiver picture Dark_Daiver · Mar 6, 2015 · Viewed 8.7k times · Source

I've implemented a dense optical flow algorithm and I want to visualize it with following color model

enter image description here

(color denotes direction of flow at some point, intensity denotes length of displacement vector)

I've implemented a dirty version of the visualization

def visualizeFlow(u, v):
    colorModel = cv2.imread('../colormodel.png')
    colorModelCenter = (colorModel.shape[0]/2, colorModel.shape[1]/2)
    res = np.zeros((u.shape[0], u.shape[1], 3), dtype=np.uint8)
    mag = np.max(np.sqrt(u**2 + v**2)) 
    if mag == 0:
        return res, colorModel
    for i in xrange(res.shape[0]):
        for j in xrange(res.shape[1]):
            res[i, j] = colorModel[
                        colorModelCenter[0] + (v[i, j]/mag*colorModelCenter[0]),
                        colorModelCenter[1] + (u[i, j]/mag*colorModelCenter[1])
                    ]
    return res, colorModel

It produce nice in general case pictures but it really slow

enter image description here

So my question is can anyone help me make this visualization faster? If somebody knows a better way to visualize dense flow it may be cool

Answer

Mastergalen picture Mastergalen · Apr 3, 2018

Code from OpenCV's tutorial:

import cv2
import numpy as np

# Use Hue, Saturation, Value colour model 
hsv = np.zeros(im1.shape, dtype=np.uint8)
hsv[..., 1] = 255

mag, ang = cv2.cartToPolar(flow[..., 0], flow[..., 1])
hsv[..., 0] = ang * 180 / np.pi / 2
hsv[..., 2] = cv2.normalize(mag, None, 0, 255, cv2.NORM_MINMAX)
bgr = cv2.cvtColor(hsv, cv2.COLOR_HSV2BGR)
cv2.imshow("colored flow", bgr)
cv2.waitKey(0)
cv2.destroyAllWindows()

enter image description here