Tracking motion using optical flow
In this recipe we will learn how to track motion in the images produced from a webcam using OpenCV using the popular Lucas Kanade optical flow algorithm.
Getting ready
We will need to use OpenCV in this recipe, so please refer to the Integrating with OpenCV recipe from Chapter 3, Using Image Processing Techniques and add OpenCV and it's CinderBlock to your project. Include the following files to your source file:
#include "cinder/Capture.h" #include "cinder/gl/Texture.h" #include "CinderOpenCV.h"
Add the following using
statements:
using namespace ci; using namespace ci::app; using namespace std;
How to do it…
We will read frames from the camera and track motion.
Declare the
ci::gl::Texture
andci::Capture
objects to display and capture from a camera. Also, declare acv::Mat
object as the previous frame, twostd::vector<cv::Point2f>
objects to store the current and previous features, and astd::vector<uint8_t>
object to store the status of each feature...