电子说
光流是由物体或照相机的运动引起的两个连续帧之间图像物体视在运动的模式。它是2D向量场,其中每个向量都是位移向量,显示点从第一帧到第二帧的运动。
光流在以下领域有许多应用:
光流的工作基于以下几个假设:
在OpenCV中实现光流的步骤
1 查找一些关键点
在例子中我们用goodFeaturesToTrack函数来查找关键点。函数原型:
void cv::goodFeaturesToTrack( InputArray _image, OutputArray _corners,
int maxCorners, double qualityLevel, double minDistance,
InputArray _mask, int blockSize,
bool useHarrisDetector, double harrisK )
2 迭代跟踪这些点
例子中用calcOpticalFlowPyrLK来计算光流。函数原型:
void cv::calcOpticalFlowPyrLK( InputArray _prevImg, InputArray _nextImg,
InputArray _prevPts, InputOutputArray _nextPts,
OutputArray _status, OutputArray _err,
Size winSize, int maxLevel,
TermCriteria criteria,
int flags, double minEigThreshold )
根据我的理解,在当前帧查找一些关键点,然后根据这些关键点,在下一帧进行匹配,得到这些点在下一帧的位置。这样就得到了了一组向量,表示了当前帧和下一帧之前点的移动,也就实现了对一些物体的跟踪。
#include
#include
#include
#include
#include
#include
using namespace cv;
using namespace std;
int main(int argc, char **argv)
{
const string about =
"This sample demonstrates Lucas-Kanade Optical Flow calculation.\\n"
"The example file can be downloaded from:\\n"
" https://www.bogotobogo.com/python/OpenCV_Python/images/mean_shift_tracking/slow_traffic_small.mp4";
const string keys =
"{ h help | | print this help message }"
"{ @image | vtest.avi | path to image file }";
CommandLineParser parser(argc, argv, keys);
parser.about(about);
if (parser.has("help"))
{
parser.printMessage();
return 0;
}
string filename = samples::findFile(parser.get
运行结果截图
审核编辑:刘清
全部0条评论
快来发表一下你的评论吧 !