CAP5415-Computer Vision Lecture 7-Optical Flow Ulas Bagci [email protected] Lecture 7: Optical Flow 1
Readings• Szeliski,R.Ch.7• Bergenetal.ECCV92,pp.237-252.• Shi,J.andTomasi,C.CVPR94,pp.593-600.• Baker,S.andMatthews,I.IJCV2004,pp.221-255.
• SlideCredits:Szeliski andShah.• NewYorkTimes• VideofromMichaelBlack
2
Lecture7:OpticalFlow
Reasons?
• thepatternsonlymovewhenyoublinkormoveyoureyes
• thearrangementofthebackgroundsofthe‘rabbits’determineswhichwaythepatternsrotate.
5
Lecture7:OpticalFlow
MotionPerceptionofchangeintheoutsideworldisofthehighestimportanceforalllivingbeings.
Agreatmajorityofchanges insensoryexcitationintheimageresultfrommotion.
Imagesalsochangeverymarkedlywhenalightsourceisturnedonoroff-butthischangecannotbeinterpretedasmotion.
6
Lecture7:OpticalFlow
WhyEstimateVisualMotion?
• VisualMotioncanbeduetoproblems– Camerainstabilities,jitter
7
Lecture7:OpticalFlow
WhyEstimateVisualMotion?
• VisualMotioncanbeduetoproblems– Camerainstabilities,jitter
• VisualMotion Indicatesdynamicsinthescene–Movingobjects,behavior,trackingobjects,analyzetrajectories
8
Lecture7:OpticalFlow
WhyEstimateVisualMotion?
• VisualMotioncanbeduetoproblems– Camerainstabilities,jitter
• VisualMotion Indicatesdynamicsinthescene–Movingobjects,behavior,trackingobjects,analyzetrajectories
• VisualMotionrevealsspatiallayout–Motionparallax
9
Lecture7:OpticalFlow
Today’sLecture
VisualMotionEstimation• Patch-basedmotion– OpticalFlow
• Lucas-Kanade• Horn-Schunck
• MotionModels
10
Lecture7:OpticalFlow
Applications
11
Lecture7:OpticalFlow
Motionbasedsegmentation
Structurefrommotion
Alignment(e.g.,UAVanalysis)
VideoCompression
TumorTracking
DeformationAnalysis
Nature News-Vol 525,Issue7567
12
Lecture7:OpticalFlow
• CVToolsthattrackhowanimalsmovearehelpingresearcherstodoeverythingfromdiagnosingneurologicalconditionstoilluminatingevolution
Higher-speedcameraseventuallyimprovedwhatcouldbecaptured.Butmovementstudiesstillneededapersontolookthroughtheresultsframebyframe,laboriouslytracingthearcofeachstep,armswingorwingflaptoextractinformationaboutanglesandforces.
Video
• Avideoisasequenceofframescapturedovertime– Imagedataisafunctionofspace(x,y)andtime(t)
15
Lecture7:OpticalFlow
I(x,y,t)
tTemporalchange
OpticalFlow
• Referstotheproblemofestimatingavectorfieldoflocaldisplacementinasequenceofimages.
20
Lecture7:OpticalFlow
OpticalFlow
• Referstotheproblemofestimatingavectorfieldoflocaldisplacementinasequenceofimages.
• Whenwefixourattentiontoasinglepointandmeasurevelocitiesflowingthroughthatlocation,thentheproblemiscalledopticalflow.
21
Lecture7:OpticalFlow
OpticalFlow
• Referstotheproblemofestimatingavectorfieldoflocaldisplacementinasequenceofimages.
• Whenwefixourattentiontoasinglepointandmeasurevelocitiesflowingthroughthatlocation,thentheproblemiscalledopticalflow.– Stereomatching,imagematching,tracking,…
22
Lecture7:OpticalFlow
EstimatingOpticalFlow
23
Lecture7:OpticalFlow
• Assumetheimageintensityisconstant
( )tyxI ,, ( )dttdyydxxI +++ ,,=
I
Time=t Time=t+dt
EstimatingOpticalFlow
25
Lecture7:OpticalFlow
( )tyxI ,, ( )dttdyydxxI +++ ,, !I(x(t) + u.�t, y(t) + v.�t)� I(x(t), y(t), t) ⇡ 0
EstimatingOpticalFlow
26
Lecture7:OpticalFlow
( )tyxI ,, ( )dttdyydxxI +++ ,, !I(x(t) + u.�t, y(t) + v.�t)� I(x(t), y(t), t) ⇡ 0
AssumingIisdifferentiablefunction,andexpandthefirsttermusingTaylor’sseries:
@I
@x
dx
dt
+@I
@y
dy
dt
+@I
@t
= 0
EstimatingOpticalFlow
27
Lecture7:OpticalFlow
( )tyxI ,, ( )dttdyydxxI +++ ,, !I(x(t) + u.�t, y(t) + v.�t)� I(x(t), y(t), t) ⇡ 0
AssumingIisdifferentiablefunction,andexpandthefirsttermusingTaylor’sseries:
@I
@x
dx
dt
+@I
@y
dy
dt
+@I
@t
= 0
Ix
u+ Iy
v + It
= 0Compactrepresentation
Brightnessconstancyconstraint
Assumption:TheBrightnessConstraint
• Expressestheideaofsimilarbrightnessforthesameobjectsobservedinasequence
28
Lecture7:OpticalFlow
Assumption:TheBrightnessConstraint
• Expressestheideaofsimilarbrightnessforthesameobjectsobservedinasequence
• Whenwefollowwithagivenlocationandtracetheirpositioninconsecutiveimagesofasequence,thentheproblemiscalled“featuretracking”
29
Lecture7:OpticalFlow
Assumption:TheBrightnessConstraint
• Expressestheideaofsimilarbrightnessforthesameobjectsobservedinasequence
• Whenwefollowwithagivenlocationandtracetheirpositioninconsecutiveimagesofasequence,thentheproblemiscalled“featuretracking”
• Tryingtosolveasingleequationfortwovariables(uandv)àill-posed
30
Lecture7:OpticalFlow
Ix
u+ Iy
v + It
= 0 ApertureProblem
SecondAssumption:GradientConstraint
32
Lecture7:OpticalFlow
Velocityvectorisconstantwithinasmallneighborhood(LUCASANDKANADE)
SecondAssumption:GradientConstraint
33
Lecture7:OpticalFlow
Velocityvectorisconstantwithinasmallneighborhood(LUCASANDKANADE)
E(u, v) =
Z
x,y
(Ix
u+ I
y
v + I
t
)2dxdy
SecondAssumption:GradientConstraint
34
Lecture7:OpticalFlow
Velocityvectorisconstantwithinasmallneighborhood(LUCASANDKANADE)
E(u, v) =
Z
x,y
(Ix
u+ I
y
v + I
t
)2dxdy
@E(u, v)
@u=
@E(u, v)
@v= 0
SecondAssumption:GradientConstraint
35
Lecture7:OpticalFlow
Velocityvectorisconstantwithinasmallneighborhood(LUCASANDKANADE)
E(u, v) =
Z
x,y
(Ix
u+ I
y
v + I
t
)2dxdy
@E(u, v)
@u=
@E(u, v)
@v= 0
2(Ix
u+ Iy
v + It
)Ix
= 0
2(Ix
u+ Iy
v + It
)Iy
= 0
Lucas-Kanade
37
Lecture7:OpticalFlow
PI2x
PIx
IyP
Ix
Iy
PI2y
� uv
�= �
PIx
ItP
Iy
It
�
Txx
Txy
Txy
Tyy
� uv
�= �
Txt
Tyt
�StructuralTensorrepresentation
Lucas-Kanade
38
Lecture7:OpticalFlow
PI2x
PIx
IyP
Ix
Iy
PI2y
� uv
�= �
PIx
ItP
Iy
It
�
Txx
Txy
Txy
Tyy
� uv
�= �
Txt
Tyt
�StructuralTensorrepresentation
u =Tyt
Txy
� Txt
Tyy
Txx
Tyy
� T 2xy
and v =Txt
Txy
� Tyt
Txx
Txx
Tyy
� T 2xy
HowdoesLucas-Kanade behave?
40
Lecture7:OpticalFlow
Texturedregion
Txx
Txy
Txy
Tyy
� uv
�= �
Txt
Tyt
�
Homog.region
EigenvaluesofmatrixTarehigh Eigenvaluesof
matrixTareverysmall
Atedges,matrixTbecomessingular!
Pitfalls&Alternatives
• Brightnessconstancyisnotsatisfied– Correlation basedmethodcouldbeused
41
Lecture7:OpticalFlow
Pitfalls&Alternatives
• Brightnessconstancyisnotsatisfied– Correlation basedmethodcouldbeused
• Apointmaynotmovelikeitsneighbors– Regularization basedmethods
42
Lecture7:OpticalFlow
Pitfalls&Alternatives
• Brightnessconstancyisnotsatisfied– Correlation basedmethodcouldbeused
• Apointmaynotmovelikeitsneighbors– Regularization basedmethods
• Themotionmaynotbesmall(Taylordoesnothold!)–Multi-scaleestimationcouldbeused
43
Lecture7:OpticalFlow
Multi-ScaleFlowEstimation
44
Lecture7:OpticalFlow
imageIt-1 imageI
GaussianpyramidofimageIt GaussianpyramidofimageIt+1
imageIt+1imageItu=10pixels
u=5pixels
u=2.5pixels
u=1.25pixels
Example:OpticalFlowin1D
46
Lecture7:OpticalFlow
v!
xI
Spatialderivative
TemporalderivativetI
x
),( txI )1,( +txI
p
tx x
II¶¶
=px
t tII
=¶¶
=x
t
IIv -»
!Assumptions:
• Brightnessconstancy• Smallmotion
Example:OpticalFlowin1DLecture7:OpticalFlow
47
x
),( txI )1,( +txI
p
xI
tI
Temporalderivativeat2nd iteration
Iteratinghelpsrefiningthevelocityvector
Cankeepthesameestimateforspatialderivative
x
tprevious I
Ivv -¬!!
Convergesinabout5iterations
Horn&Schunck
• Globalmethodwithsmoothnessconstrainttosolveapertureproblem
• Minimizeaglobalenergyfunction
51
Lecture7:OpticalFlow
Horn&Schunck
• Globalmethodwithsmoothnessconstrainttosolveapertureproblem
• Minimizeaglobalenergyfunction
52
Lecture7:OpticalFlow
E(u, v) =
Z
x,y
[(Ix
u+ I
y
v + I
t
)2 + ↵
2(|ru|2 + |rv|2)]dxdy
Horn&Schunck
• Globalmethodwithsmoothnessconstrainttosolveapertureproblem
• Minimizeaglobalenergyfunction
• Takepartialderivativesw.r.t.uandv:
53
Lecture7:OpticalFlow
E(u, v) =
Z
x,y
[(Ix
u+ I
y
v + I
t
)2 + ↵
2(|ru|2 + |rv|2)]dxdy
Horn&Schunck
• Globalmethodwithsmoothnessconstrainttosolveapertureproblem
• Minimizeaglobalenergyfunction
• Takepartialderivativesw.r.t.uandv:
54
Lecture7:OpticalFlow
E(u, v) =
Z
x,y
[(Ix
u+ I
y
v + I
t
)2 + ↵
2(|ru|2 + |rv|2)]dxdy
(Ix
u+ Iy
v + It
)Ix
� ↵2ru = 0
(Ix
u+ Iy
v + It
)Iy
� ↵2rv = 0
Horn&Schunck
55
Lecture7:OpticalFlow
• Iterative scheme
• Yields high density flow• Fill in missing information in the homogenous regions• More sensitive to noise than local methods
2221
2221
)(
)(
yx
tk
yk
xykk
yx
tk
yk
xxkk
IIIvIuII
vv
IIIvIuII
uu
++
++-=
++
++-=
+
+
a
a
u
v
),( yx II
),( vu),( vu
OpticalFlowMatlab/C++/PythonCode
• http://people.csail.mit.edu/celiu/OpticalFlow/• http://opencv-python-tutroals.readthedocs.org/en/latest/py_tutorials/py_video/py_lucas_kanade/py_lucas_kanade.html
• https://github.com/Itseez/opencv_attic/blob/a6078cc8477ff055427b67048a95547b3efe92a5/opencv/samples/python2/lk_track.py
56
Lecture7:OpticalFlow
Applications: ActionRecognition
58
Lecture7:OpticalFlow
yx FF , +-+-yyxx FFFF ,,, blurred +-+-
yyxx FFFF ,,,
Recognitionactionsatadistance,Efros etal.
Applications:MotionModeling
59
Lecture7:OpticalFlow
Flippingbetweenimage1and2. Estimatedflowfield(hueindicatesorientationandsaturationindicatesmagnitude)
Secondimageiswarped!
SlidecredittoC.Liu
GlobalMotionModels(Parametric)Allpixelsareconsideredtosummarizeglobalmotion!
• 2DModels– Affine– Quadratic– Planarprojective(homography)
• 3DModels– Inst.Cameramotionmodels– Homography+epipole– Plane+parallalx
61
Lecture7:OpticalFlow
MotionModels
62
Lecture7:OpticalFlow
Translation
2 unknowns
Affine
6 unknowns
Perspective
8 unknowns
3D rotation
3 unknowns
Example:AffineMotion
64
Lecture7:OpticalFlow
u(x, y) = a1 + a2x+ a3y
u(x, y) = a4 + a5x+ a6y
Ix
u+ Iy
v + It
⇡ 0
Example:AffineMotion
65
Lecture7:OpticalFlow
u(x, y) = a1 + a2x+ a3y
u(x, y) = a4 + a5x+ a6y
I
x
(a1 + a2x+ a3y) + I
y
(a4 + a5x+ a6y) + I
t
⇡ 0
Example:AffineMotion
66
Lecture7:OpticalFlow
u(x, y) = a1 + a2x+ a3y
u(x, y) = a4 + a5x+ a6y
Eachpixelprovides1linearconstraintin6globalunknowns(a1,..,a6)
I
x
(a1 + a2x+ a3y) + I
y
(a4 + a5x+ a6y) + I
t
⇡ 0
Example:AffineMotion
67
Lecture7:OpticalFlow
u(x, y) = a1 + a2x+ a3y
u(x, y) = a4 + a5x+ a6y
Eachpixelprovides1linearconstraintin6globalunknowns(a1,..,a6)
Overallpixels,minimizetheleastsquaretofindunknowns!
Err
a1,...,a6 =X
[Ix
(a1 + a2x+ a3y) + I
y
(a4 + a5x+ a6y)]2
I
x
(a1 + a2x+ a3y) + I
y
(a4 + a5x+ a6y) + I
t
⇡ 0
Other2DMotionModels
• Quadratic
• Projective
68
Lecture7:OpticalFlow
u = q1 + q2x+ q3y + q7x2 + q8xy
u = q4 + q5x+ q6y + q7xy + q8y2
u =h1 + h2x+ h3y
h7 + h8x+ h9y� x
v =h4 + h5x+ h6y
h7 + h8x+ h9y� y