banner



How To Compute And Draw Optical Flow

Python OpenCV – Dense optical flow

Prerequisites: Python OpenCV, Grayscaling

Optical period is the motion of objects between the sequent frames of the sequence, caused past the relative movement between the camera and the object. It tin can exist of ii types-Sparse Optical flow and Dumbo Optical flow.

Dumbo Optical menstruation

Dense Optical flow computes the optical menstruation vector for every pixel of the frame which may be responsible for its slow speed just leading to a better authentic event. It tin be used for detecting move in the videos, video segmentation, learning construction from move. In that location tin be various kinds of implementations of dense optical catamenia. The example below volition follow the Farneback method along with OpenCV.

Franeback Method

The outset step is that the method approximates the windows of image frames by a quadratic polynomial with the help of the polynomial expansion transform. Next, by observing how the polynomial transforms nether the state of motion. i.e. to estimate displacement fields. Dense optical flow is computed, after a series of refinements.

For OpenCV's implementation, the magnitude and direction of optical flow from a 2-D channel assortment of flow vectors are computed for the optical flow problem. The angle (direction) of flow by hue is visualized and the altitude (magnitude) of flow by the value of HSV colour representation. The strength of HSV is always gear up to a maximum of 255 for optimal visibility.
The method defined is caclopticalFlowFarneback() .

Below is the implementation.

import cv2 as cv

import numpy as np

cap = cv.VideoCapture( "videoplayback.mp4" )

ret, first_frame = cap.read()

prev_gray = cv.cvtColor(first_frame, cv.COLOR_BGR2GRAY)

mask = np.zeros_like(first_frame)

mask[..., 1 ] = 255

while (cap.isOpened()):

ret, frame = cap.read()

cv.imshow( "input" , frame)

gray = cv.cvtColor(frame, cv.COLOR_BGR2GRAY)

menstruum = cv.calcOpticalFlowFarneback(prev_gray, greyness,

None ,

0.5 , three , 15 , 3 , 5 , 1.2 , 0 )

magnitude, angle = cv.cartToPolar(catamenia[..., 0 ], menstruum[..., 1 ])

mask[..., 0 ] = angle * 180 / np.pi / 2

mask[..., 2 ] = cv.normalize(magnitude, None , 0 , 255 , cv.NORM_MINMAX)

rgb = cv.cvtColor(mask, cv.COLOR_HSV2BGR)

cv.imshow( "dense optical flow" , rgb)

prev_gray = gray

if cv.waitKey( 1 ) & 0xFF = = ord ( 'q' ):

pause

cap.release()

cv.destroyAllWindows()

Output


Source: https://www.geeksforgeeks.org/python-opencv-dense-optical-flow/

Posted by: murphyconst1993.blogspot.com

0 Response to "How To Compute And Draw Optical Flow"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel