Visualize Optical Flow With Color Model
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()
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 usingmerge
ormixChannels
.Apply
cvtColor
with the flagCV_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:
pip install flow_vis
Then in your code:
import flow_vis
flow_color = flow_vis.flow_to_color(flow_uv, convert_to_bgr=False)
Post a Comment for "Visualize Optical Flow With Color Model"