Motion Optical Flow
Motion Optical Flow
1. Motion
2. Optical Flow
• Brightness Constancy constraint equation
• Lucas-Kanade algorithm
3. Project: Action Recognition
2
LECTURE OUTCOMES
3
M O T I O N E S T I M AT I O N
8
https://sandipanweb.wordpress.com/2018/02/25/implementing-lucas-kanade-optical-flow-algorithm-in-python/
O P T I C A L F L OW
• Given an image 𝐼(𝑥, 𝑦, 𝑡) and move its pixels by (𝑑𝑥, 𝑑𝑦) over t time, we obtain the new
image 𝐼(𝑥 + 𝑑𝑥, 𝑦 + 𝑑𝑦, 𝑡 + 𝑑𝑡). Assume that the pixel intensities of an object are constant
between consecutive frame, then
𝐼 𝑥, 𝑦, 𝑡 = 𝐼 𝑥 + 𝑑𝑥, 𝑦 + 𝑑𝑦, 𝑡 + 𝑑𝑡
• Taking the Taylor Series Approximation 1 of the right-hand side of the above equation
𝜕𝐼 𝜕𝐼 𝜕𝐼
𝐼 𝑥 + 𝑑𝑥, 𝑦 + 𝑑𝑦, 𝑡 + 𝑑𝑡 = 𝐼 𝑥, 𝑦, 𝑡 + 𝑑𝑥 + 𝑑𝑦 + 𝑑𝑡 + ⋯
𝜕𝑥 𝜕𝑦 𝜕𝑡
• Rearranging terms and setting the approximation equal to the original expression, we get
𝜕𝐼 𝜕𝐼 𝜕𝐼
𝑑𝑥 + 𝑑𝑦 + 𝑑𝑡 = 0
𝜕𝑥 𝜕𝑦 𝜕𝑡
• Dividing by dt to get optical flow equation
𝜕𝐼 𝜕𝐼 𝜕𝐼 𝑑𝑥 𝑑𝑦
𝑢+ 𝑣+ = 𝑰𝒙 𝒖 + 𝑰𝒚 𝒗 + 𝑰𝒕 = 0, 𝑢 = ,𝑣 =
𝜕𝑥 𝜕𝑦 𝜕𝑡 𝑑𝑡 𝑑𝑡
𝜕𝐼 Τ𝜕𝑥, 𝜕𝐼 Τ𝜕𝑦 , and 𝜕𝐼 Τ𝜕𝑡 are the image gradients (derivative of intensity) along the
horizontal axis, the vertical axis, and time
11
https://brilliant.org/wiki/taylor-series-approximation/
O P T I C A L F L OW E Q UAT I O N
𝒅𝒙 𝒅𝒙
• The problem of optical flow, that is, solving 𝒖 = and 𝐯 = to
𝒅𝒕 𝒅𝒕
determine movement over time.
• We cannot directly solve the optical flow equation for u and v
since there is only one equation for two unknown variables.
12
B R I G H T N E S S C O N S TA N C Y E Q UAT I O N
• The goal of optical flow algorithms is to estimate the optical flow vector
𝒖, 𝒗 for each pixel in the image, satisfying this brightness constancy
constraint.
• Optical flow works on several assumptions
• The pixel intensities of an object do not change between consecutive frames
• Neighbouring pixels have similar motion
• Sparse Optical Flow: This approach tracks specific features or interest
points in the image, such as corners or key points, and estimates the
motion vectors of these points.
• Dense Optical Flow: This method calculates motion vectors for every
pixel in an image, providing a dense field of motion information across
the entire image
14
S PA R S E O P T I C A L F L OW M E T H O D S
18
L U C A S - K A N A D E O P T I C A L F L OW A L G O R I T H M
𝐼𝑥 = 𝐻𝑥 ∗ I
• Temporal Gradient: compute the temporal gradient 𝐼𝑡 by taking the
pixel-wise difference between the two consecutive frames.
𝜕𝐼
𝐼𝑡 𝑥, 𝑦 = = 𝐼 𝑥, 𝑦, 𝑡 + 1 − 𝐼(𝑥, 𝑦, 𝑡)
𝜕𝑡
• Windowed Intensity Values: Choose a local window around a pixel
19
L U C A S - K A N A D E O P T I C A L F L OW A L G O R I T H M
𝑢
• Lucas-Kanade Equation: 𝐼𝑥 𝑢 + 𝐼𝑦 𝑣 = −𝐼𝑡 ⇔ 𝑥 𝐼 𝐼𝑦 = −𝐼𝑡
𝑣
𝑢
𝐼𝑥 𝐼𝑦 𝑇 𝐼𝑥 𝐼𝑦 = − 𝐼𝑥 𝐼𝑦 𝑇 𝐼𝑡
𝑣
𝑢
= − 𝐼𝑥 𝐼𝑦 𝑇 𝐼𝑥 𝐼𝑦 −1 𝐼𝑥 𝐼𝑦 𝑇 𝐼𝑥 𝐼𝑦 𝐼𝑡
𝑣
• Consider n = 𝑘𝑥𝑘 window (n pixels) arounding the specific pixel
𝐼𝑥1 𝐼𝑦1 −𝐼𝑡1
𝐼𝑥2 𝐼𝑦2 𝑢 −𝐼𝑡2
=
⋮ ⋮ 𝑣 ⋮
𝐼𝑥𝑛 𝐼𝑦𝑛 −𝐼𝑡𝑛
𝑢 𝑇 𝑢 𝑇 𝑢
𝐴 = 𝐼𝑡 ⇒ 𝐴 𝐴 = 𝐴 𝐼𝑡 ⇒ = 𝐴𝑇 𝐴 −1 𝐴𝑇 𝐼𝑡
𝑣 𝑣 𝑣
• We solve a least-square problem to find the best flow vectors for the pixel
20
L U C A S - K A N A D E O P T I C A L F L OW A L G O R I T H M
𝑢
• Instead of one equation 𝐼𝑥 𝐼𝑦 = −𝐼𝑡 with two unknow variables, we have n linear
𝑣
equations (local window with n pixels). The problem of over-determined.
𝐼𝑥 𝑝1 𝐼𝑦 𝑝1 −𝐼𝑡 𝑝1
𝐼𝑥 𝑝2 𝐼𝑦 𝑝2 𝑢 −𝐼𝑡 𝑝2
=
⋮ ⋮ 𝑣 ⋮
𝐼𝑥 𝑝𝑛 𝐼𝑥 𝑝𝑛 −𝐼𝑡 𝑝𝑛
• In such cases, a least squares approach is used to find the best-fitting solution.
• The least squares fit provides a way to solve this system of equations and find the motion
parameters that best satisfy the constraints imposed by the brightness constancy assumption.
• The least squares fit aims to minimize the sum of squared differences (residuals) between
the observed intensity changes (temporal gradient 𝐼𝑡 ) and the predicted changes based on
the motion parameters. This minimization process provides a robust estimation of the
motion parameters that best fits the observed data.
21
L U C A S - K A N A D E O P T I C A L F L OW A L G O R I T H M
−1
2
𝐼𝑥 𝑝𝑖 𝐼𝑥 𝑝𝑖 𝐼𝑦 𝑝𝑖
𝑢 𝑖 𝑖 − 𝐼𝑥 𝑝𝑖 𝐼𝑡 𝑝𝑖 − 𝐼𝑦 𝑝𝑖 𝐼𝑡 𝑝𝑖
=
𝑣 2
𝐼𝑦 𝑝𝑖 𝐼𝑥 𝑝𝑖 𝐼𝑦 𝑝𝑖 𝑖 𝑖
𝑖 𝑖
23
LUCAS-KANADE PYTHON STEP BY STEP
24
LUCAS-KANADE PYTHON WITH OPENCV
26
A C T I O N R E C O G N I T I O N W I T H O P T I C A L F L OW
• Optical can be used for motion analysis. It involves computing the flow
vectors that represent the apparent motion of pixels between consecutive
frames in a video sequence.
• Optical flow can be used to estimate the direction and speed of moving
objects.
• OpenCV provides functions like cv2.calcOpticalFlowFarneback(),
cv2.goodFeaturesToTrack() and cv2.calcOpticalFlowPyrLK() that can be
used for optical flow analysis, feature tracking.
• Extract pertinent information from the video frames such as optical flow, or
feature tracking to extract motion-related features from the video frames and
feed them into machine learning model (SVM, AdaBoost, etc).
• Lucas-Kanade itself is not typically used for action recognition, it can be a
component of a larger system for motion analysis.
27
A C T I O N R E C O G N I T I O N W I T H O P T I C A L F L OW
28
A C T I O N R E C O G N I T I O N W I T H O P T I C A L F L OW
29
S O M E C V 2 F U N C T I O N S F O R O P T I C F L OW
30
ACTIVITY RECOGNITION USING MEDIAPIPE
31
ACTIVITY RECOGNITION USING MEDIAPIPE
12 keypoints
34