calcOpticalFlowPyrLK
Calculates sparse optical flow using the iterative Lucas-Kanade method with pyramids.First 8-bit input image or pyramid constructed by
buildOpticalFlowPyramid.Second input image or pyramid of the same size and type as prevImg.
Vector of 2D points for which the flow needs to be found. Point coordinates must be single-precision floating-point.
Output vector of 2D points containing the calculated new positions of input features in the second image.
Output status vector (unsigned chars). Each element is set to 1 if flow was found for the corresponding feature, otherwise 0.
Output vector of errors for each feature. The error type depends on the flags parameter.
Size of the search window at each pyramid level. Default: Size(21, 21)
0-based maximal pyramid level number. 0 means no pyramid (single level), 1 means two levels, etc. Default: 3
Termination criteria for the iterative search algorithm. Default: 30 iterations or epsilon of 0.01
Operation flags:
OPTFLOW_USE_INITIAL_FLOW: Use initial estimations stored in nextPtsOPTFLOW_LK_GET_MIN_EIGENVALS: Use minimum eigen values as error measure
Minimum eigen value threshold. Features with smaller values are filtered out. Default: 1e-4
The function implements a sparse iterative version of the Lucas-Kanade optical flow in pyramids. It is parallelized with TBB for better performance. The algorithm calculates the minimum eigen value of a 2×2 spatial gradient matrix; if this value is less than minEigThreshold, the feature is filtered out.
Example
buildOpticalFlowPyramid
Constructs an image pyramid for use with calcOpticalFlowPyrLK.8-bit input image.
Output pyramid.
Window size of optical flow algorithm. Must be at least as large as the winSize argument of calcOpticalFlowPyrLK.
0-based maximal pyramid level number.
Set to precompute gradients for every pyramid level. If false, calcOpticalFlowPyrLK will compute them internally. Default: true
Border mode for pyramid layers. Default: BORDER_REFLECT_101
Border mode for gradients. Default: BORDER_CONSTANT
Put ROI of input image into the pyramid if possible. Set to false to force data copying. Default: true
calcOpticalFlowFarneback
Computes dense optical flow using the Gunnar Farneback algorithm.First 8-bit single-channel input image.
Second input image of the same size and type as prev.
Computed flow image with the same size as prev and type CV_32FC2.
Image scale (<1) to build pyramids. 0.5 means a classical pyramid where each next layer is twice smaller.
Number of pyramid layers including the initial image. levels=1 means no extra layers.
Averaging window size. Larger values increase robustness to noise and detect fast motion better, but yield more blurred motion fields.
Number of iterations the algorithm does at each pyramid level.
Size of pixel neighborhood used to find polynomial expansion. Larger values mean smoother surfaces. Typically 5 or 7.
Standard deviation of the Gaussian used to smooth derivatives. For poly_n=5, use poly_sigma=1.1; for poly_n=7, use poly_sigma=1.5.
Operation flags:
OPTFLOW_USE_INITIAL_FLOW: Use input flow as initial approximationOPTFLOW_FARNEBACK_GAUSSIAN: Use Gaussian filter instead of box filter (more accurate but slower)
Example
readOpticalFlow / writeOpticalFlow
Read and write optical flow files in .flo format.Path to the .flo file.
Flow field to be stored. Must be 2-channel, floating-point (CV_32FC2). First channel is horizontal (u), second is vertical (v).
DenseOpticalFlow Interface
Base class for dense optical flow algorithms.calc
Calculates optical flow between two frames.First 8-bit single-channel input image.
Second input image of the same size and type.
Computed flow image that has the same size as I0 and type CV_32FC2.
collectGarbage
Releases all inner buffers to free memory.SparseOpticalFlow Interface
Base interface for sparse optical flow algorithms.FarnebackOpticalFlow
Class computing dense optical flow using the Gunnar Farneback algorithm.Example
SparsePyrLKOpticalFlow
Class for calculating sparse optical flow using the iterative Lucas-Kanade method with pyramids.Example
DISOpticalFlow
Dense Inverse Search (DIS) optical flow algorithm with configurable speed/quality presets.DIS includes several enhancements over the paper implementation, including spatial propagation of flow vectors and support for initial flow approximation. Even the slowest preset is relatively fast; use DeepFlow if you need better quality and don’t care about speed.
Presets
- PRESET_ULTRAFAST
- PRESET_FAST
- PRESET_MEDIUM
Fastest preset with basic quality. Suitable for real-time applications where speed is critical.
Example
VariationalRefinement
Variational optical flow refinement for improving existing flow fields.Example
Algorithm Comparison
- Sparse Methods
- Dense - Fast
- Refinement
Lucas-Kanade (calcOpticalFlowPyrLK)
- Type: Sparse (feature points)
- Speed: Very fast
- Accuracy: Good for well-textured features
- Use case: Feature tracking, structure from motion
