Skip to content Skip to sidebar Skip to footer

Visualize Optical Flow With Color Model

I've implemented a dense optical flow algorithm and I want to visualize it with following color model (color denotes direction of flow at some point, intensity denotes length of

Solution 1:

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

Solution 2:

If you use function provided by opencv your code will run faster. The optical flow visualization works as follow:

  • Transform u and v motion components matrices into polar coordinate. Applying cartToPolar function (x array = u array, y array = v array) will get you angle and magnitude matrices of your motion vectors.

The final colour visualization can than be found by an inverse HSV to RGB transformation, where the angle matrice corresponde to the Hue (H) channel and the magnitude to the saturation (S) the value (V) is set to maxima. ( In your example the value and saturation channels are swapped).

  • Merge the magnitude, angle and a matrice filled with 1 to a CV_32FC3 channel matric using merge or mixChannels.

  • Apply cvtColor with the flag CV_HSV2BGR. Note angle matric is in degrees and magnitude has to be rescaled to fit i [0,1] which can be done by dividing it by the maximun of the magnitude using e.g. MinMaxLoc

Solution 3:

You might wanna check the awesome flow_vis package. Quoting from their page:

  1. pip install flow_vis

  2. Then in your code:

importflow_visflow_color= flow_vis.flow_to_color(flow_uv, convert_to_bgr=False)

Post a Comment for "Visualize Optical Flow With Color Model"