转自:https://blog.csdn.net/qq_33389308/article/details/83049479
本文参考链接:https://blog.csdn.net/zy122121cs/article/details/44955353
参考论文:”Pyramidal Implementation of the Lucas Kanade Feature TrackerDescription of the algorithm”
一、金字塔光流法介绍
光流金字塔即对图像进行分层处理,一般来说不算原始图像(最底层)的话分为四层就能满足需求,按照论文中的话说就是超过4层在大多数情况下没有意义。如果原始图像的大小为640x480,那么分为4层的大小分别为320x240,160x120,80x60,40x30。
如下图所示:
金字塔分层
接下来对金字塔光流法的过程进行简单描述,期间不会出现任何数学公式,对公式有兴趣的小伙伴可以直接搜索查阅参考文献的论文。
首先展示一张图:
金字塔光流的过程
我们对视频中点的跟踪实际上是对相邻两帧的图像进行处理,设图像I和J为相邻两帧的图像,我们希望在图像J中找到u0的对应点v,那么首先对两幅图像进行分层,假设如上图分为3层,如此可以分别计算得到u1、u2、u3。
对于金字塔我们从最高层开始进行处理, u3在图像J中的对应初始点为v31(v31和u3是相等的,图画的不太准),然后通过某种计算符合相应的条件后,得到当前层最小误差点v3n(n表示经过n次计算)和相应的光流。然后利用计算得到的光流能够在图像J中找到点v21作为第二层的初始点,以此类推进行和第3层一样的迭代计算最终能够获得包含各层光流分量的总光流,就能得到最终的对应点v0r。
注:1.某种计算具体见论文。
2.相应条件包含两种,一是达到设置的迭代次数上限,二是计算结果符合精确度阈值。这在opencv的函数中有体现。
3.论文中能够得到一些参数设置信息,迭代次数一般设置为5次即可(但是opencv中默认为30次),金字塔层数≤4,搜索窗大小为奇数x奇数。
二、OpenCV金字塔光流函数介绍
函数声明如下:
-
CV_EXPORTS_W void calcOpticalFlowPyrLK( InputArray prevImg, InputArray nextImg,
-
InputArray prevPts, InputOutputArray nextPts,
-
OutputArray status, OutputArray err,
-
Size winSize = Size(21,21), int maxLevel = 3,
-
TermCriteria criteria = TermCriteria(TermCriteria::COUNT+TermCriteria::EPS, 30, 0.01),
-
int flags = 0, double minEigThreshold = 1e-4 );
InputArray prevImg | 前一幅图像 |
InputArray nextImg | 后一幅图像 |
InputArray prevPts | vector,前一幅图像中想要跟踪的点集 |
InputOutputArray nextPts | vector,后一幅图像中计算得到的对应点集 |
OutputArray status | vector,记录状态,如果对应点的光流被搜索到则将对应点置1 |
OutputArray err | vector,记录每个特征点的误差,如果光流没有被计算出来,不会有误差 |
Size winSize = Size(21,21) | 搜索窗的大小,如前所述为奇数x奇数 |
int maxLevel = 3 | 金字塔的层数 |
TermCriteria criteria = TermCriteria(TermCriteria::COUNT+TermCriteria::EPS, 30, 0.01) | 迭代停止条件,默认设置为30次迭代或者阈值0.01 |
int flags = 0 | 默认值为0,表示不设置此标记。有如下标记可以选择 OPTFLOW_USE_INITIAL_FLOW = 4, OPTFLOW_LK_GET_MIN_EIGENVALS = 8, OPTFLOW_FARNEBACK_GAUSSIAN = 256 |
double minEigThreshold = 1e-4 | 作为阈值可以过滤掉一些不好的特征点以提升性能 |
三、官方例程
-
#include "opencv2/video/tracking.hpp"
-
#include "opencv2/imgproc.hpp"
-
#include "opencv2/videoio.hpp"
-
#include "opencv2/highgui.hpp"
-
#include <iostream>
-
#include <ctype.h>
-
using namespace cv;
-
using namespace std;
-
static void help()
-
{
-
// print a welcome message, and the OpenCV version
-
cout << "\nThis is a demo of Lukas-Kanade optical flow lkdemo(),\n"
-
"Using OpenCV version " << CV_VERSION << endl;
-
cout << "\nIt uses camera by default, but you can provide a path to video as an argument.\n";
-
cout << "\nHot keys: \n"
-
"\tESC - quit the program\n"
-
"\tr - auto-initialize tracking\n"
-
"\tc - delete all the points\n"
-
"\tn - switch the \"night\" mode on/off\n"
-
"To add/remove a feature point click it\n" << endl;
-
}
-
Point2f point;
-
bool addRemovePt = false;
-
static void onMouse( int event, int x, int y, int /*flags*/, void* /*param*/ )
-
{
-
if( event == EVENT_LBUTTONDOWN )
-
{
-
point = Point2f((float)x, (float)y);
-
addRemovePt = true;
-
}
-
}
-
int main( int argc, char** argv )
-
{
-
VideoCapture cap;
-
TermCriteria termcrit(TermCriteria::COUNT|TermCriteria::EPS,20,0.03);
-
Size subPixWinSize(10,10), winSize(31,31);
-
const int MAX_COUNT = 500;
-
bool needToInit = false;
-
bool nightMode = false;
-
help();
-
cv::CommandLineParser parser(argc, argv, "{@input|0|}");
-
string input = parser.get<string>("@input");
-
if( input.size() == 1 && isdigit(input[0]) )
-
cap.open(input[0] - '0');
-
else
-
cap.open(input);
-
if( !cap.isOpened() )
-
{
-
cout << "Could not initialize capturing...\n";
-
return 0;
-
}
-
namedWindow( "LK Demo", 1 );
-
setMouseCallback( "LK Demo", onMouse, 0 );
-
Mat gray, prevGray, image, frame;
-
vector<Point2f> points[2];
-
for(;;)
-
{
-
cap >> frame;
-
if( frame.empty() )
-
break;
-
frame.copyTo(image);
-
cvtColor(image, gray, COLOR_BGR2GRAY);
-
if( nightMode )
-
image = Scalar::all(0);
-
if( needToInit )
-
{
-
// automatic initialization
-
goodFeaturesToTrack(gray, points[1], MAX_COUNT, 0.01, 10, Mat(), 3, 0, 0.04);
-
cornerSubPix(gray, points[1], subPixWinSize, Size(-1,-1), termcrit);
-
addRemovePt = false;
-
}
-
else if( !points[0].empty() )
-
{
-
vector<uchar> status;
-
vector<float> err;
-
if(prevGray.empty())
-
gray.copyTo(prevGray);
-
calcOpticalFlowPyrLK(prevGray, gray, points[0], points[1], status, err, winSize,
-
3, termcrit, 0, 0.001);
-
size_t i, k;
-
for( i = k = 0; i < points[1].size(); i++ )
-
{
-
if( addRemovePt )
-
{
-
if( norm(point - points[1][i]) <= 5 )
-
{
-
addRemovePt = false;
-
continue;
-
}
-
}
-
if( !status[i] )
-
continue;
-
points[1][k++] = points[1][i];
-
circle( image, points[1][i], 3, Scalar(0,255,0), -1, 8);
-
}
-
points[1].resize(k);
-
}
-
if( addRemovePt && points[1].size() < (size_t)MAX_COUNT )
-
{
-
vector<Point2f> tmp;
-
tmp.push_back(point);
-
cornerSubPix( gray, tmp, winSize, Size(-1,-1), termcrit);
-
points[1].push_back(tmp[0]);
-
addRemovePt = false;
-
}
-
needToInit = false;
-
imshow("LK Demo", image);
-
char c = (char)waitKey(10);
-
if( c == 27 )
-
break;
-
switch( c )
-
{
-
case 'r':
-
needToInit = true;
-
break;
-
case 'c':
-
points[0].clear();
-
points[1].clear();
-
break;
-
case 'n':
-
nightMode = !nightMode;
-
break;
-
}
-
std::swap(points[1], points[0]);
-
cv::swap(prevGray, gray);
-
}
-
return 0;
-
}