最近在学习opencv,第一个小任务就是利用光流法,跟踪视频中指定物体。在写程序的过程中,最纠结的一点就是怎么选取calcOpticalFlowPyrLK()的特征点,因为很多时候都是通过goodFeaturesToTrack()函数,找到图像中的强角点,再把强角点当作calcOpticalFlowPyrLK的特征点。我也很自然想到在感兴趣区域寻找角点,当作光流分析的特征点,但问题来了,在这些角点里,究竟那些点最能代表想要跟踪的物体呢,怎么选才能保证跟踪效果。。。为此我纠结了两天,后来与同学聊这事,才恍然大悟,其实完全不用去选择角点,把感兴趣区域的中心点当作特征点去追踪,就可以啦。这样跟踪方便,画图也很方便。所以在学习过程中,真的应该多和身边人交流,这样会学的更轻松,更有效果。
这个程序功能是这样的:程序首先调用电脑摄像头,咱们通过鼠标,选择视频中感兴趣的区域(比如说选择��或者��)并用矩形框框起来,选中好后,当我们移动时,矩形框始终跟着最开始选中的区域移动。并且可以画出特征点粗略的运动轨迹。因为是学习嘛,又在这跟踪的基础上,增加了绘制矩形框内图像直方图的功能。下面是程序啦:
#include "opencv2/video/tracking.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
#include
#include
using namespace cv;
using namespace std;
//--------------------------------【help( )函数】----------------------------------------------
// 描述:输出帮助信息
//-------------------------------------------------------------------------------------------------
static void help()
{
//输出欢迎信息和OpenCV版本
cout <<"\n\n\t\t\t非常感谢使用BPI-OpenCV!\n"
cout << "\n\t程序默认从摄像头读入视频,可以按需改为从视频文件读入图像\n";
cout << "\n\t操作说明: \n"
"\t\t通过点击在图像中添加/删除特征点\n"
"\t\tESC - 退出程序\n"
"\t\tr -自动进行追踪\n"
"\t\tc - 删除所有点\n"
"\t\tn - 开/光-夜晚模式\n"<< endl;
}
Point2f point;
bool addRemovePt = false;
//--------------------------------【onMouse( )回调函数】------------------------------------
// 描述:鼠标操作回调
//-------------------------------------------------------------------------------------------------
static void onMouse( int event, int x, int y, int /*flags*/, void* /*param*/ )
{
//此句代码的OpenCV2版为:
if( event == CV_EVENT_LBUTTONDOWN )
//此句代码的OpenCV3版为:
//if( event == EVENT_LBUTTONDOWN )
{
point = Point2f((float)x, (float)y);
addRemovePt = true;
}
}
//-----------------------------------【main( )函数】--------------------------------------------
// 描述:控制台应用程序的入口函数
//-------------------------------------------------------------------------------------------------
int main( int argc, char** argv )
{
help();
VideoCapture cap;
//此句代码的OpenCV2版为:
TermCriteria termcrit(CV_TERMCRIT_ITER|CV_TERMCRIT_EPS, 20, 0.03);
//此句代码的OpenCV3版为:
//TermCriteria termcrit(TermCriteria::MAX_ITER|TermCriteria::EPS, 20, 0.03);
Size subPixWinSize(10,10), winSize(31,31);
const int MAX_COUNT = 500;
bool needToInit = false;
bool nightMode = false;
cap.open(0);
if( !cap.isOpened() )
{
cout << "Could not initialize capturing...\n";
return 0;
}
namedWindow( "LK Demo", 1 );
setMouseCallback( "LK Demo", onMouse, 0 );
Mat gray, prevGray, image;
vector points[2];
for(;;)
{
Mat frame;
cap >> frame;
if( frame.empty() )
break;
frame.copyTo(image);
cvtColor(image, gray, COLOR_BGR2GRAY);
if( nightMode )
image = Scalar::all(0);
if( needToInit )
{
// 自动初始化
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 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 tmp;
tmp.push_back(point);
//此句代码的OpenCV2版为:
cornerSubPix( gray, tmp, winSize, cvSize(-1,-1), termcrit);
//此句代码的OpenCV3版为:
//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;
}