This repository contains a small C++/OpenCV implementation of sparse optical flow using the Lucas-Kanade method.
The program compares two consecutive sample images:
test/data/img/scene00140.pngtest/data/img/scene00141.png
It estimates local motion vectors and draws them on the first frame.
- C++17 compiler
- Xcode Command Line Tools
The macOS build does not require OpenCV, CMake, or Homebrew.
The repository includes a macOS-native implementation that uses Apple's built-in ImageIO/CoreGraphics frameworks for PNG input/output. It does not require Homebrew, OpenCV, or CMake.
Build:
make macRun:
make run-macThe result is saved as:
output_mac.png
This path is optional and requires OpenCV 4.x plus CMake 3.16 or newer.
From the repository root:
cmake -S . -B build
cmake --build buildRun:
./build/lucas_kanade_optical_flowOn Windows, the executable may be under a configuration directory:
.\build\Debug\lucas_kanade_optical_flow.exeOpen Opencv_test.sln in Visual Studio and build the x64 configuration.
The Visual Studio project expects OpenCV at:
C:\opencv\build
If OpenCV is installed somewhere else, define the OpenCVRoot environment variable before opening Visual Studio:
setx OpenCVRoot "C:\path\to\opencv\build"The project currently links against OpenCV 4.7.0 world libraries:
- Debug:
opencv_world470d.lib - Release:
opencv_world470.lib
If your OpenCV version is different, update the library names in test/test.vcxproj.
The implementation follows the Lucas-Kanade least-squares form:
[Ix Iy] [u v]^T = -It
Low-texture windows are skipped using a minimum eigenvalue threshold to avoid unstable motion vectors.