跟踪就是在连续视频帧中定位物体,一般的跟踪算法包括如下几类:html
1. Dense Optical Flow 稠密光流算法
2. Sparse Optical Flow 稀疏光流 最典型的如KLT算法(Kanade-Lucas-Tomshi)ide
3. Kalman Filterspa
4. Meanshift and Camshiftcode
5. Multiple object tracking视频
须要注意跟踪和识别的区别,一般来讲跟踪能够比识别快不少,且跟踪失败了能够找回来。htm
OpenCV 3之后实现了不少追踪算法,都实如今contrib模块中,安装参考。blog
下面code实现了跟踪笔记本摄像头画面中的固定区域物体,能够选用OpenCV实现的算法ip
#include <opencv2/opencv.hpp> #include <opencv2/tracking.hpp> using namespace std; using namespace cv; int main(int argc, char** argv){ // can change to BOOSTING, MIL, KCF (OpenCV 3.1), TLD, MEDIANFLOW, or GOTURN (OpenCV 3.2) Ptr<Tracker> tracker = Tracker::create("MEDIANFLOW"); VideoCapture video(0); if(!video.isOpened()){ cerr << "cannot read video!" << endl; return -1; } Mat frame; video.read(frame); Rect2d box(270, 120, 180, 260); tracker->init(frame, box); while(video.read(frame)){ tracker->update(frame, box); rectangle(frame, box, Scalar(255, 0, 0), 2, 1); imshow("Tracking", frame); int k=waitKey(1); if(k==27) break; } }
着重了解效果较好的KCF(Kernelized Correlation Filters)和经典的KLT算法get