OpenCV 光流示例程序

#include <opencv2/video/tracking.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/features2d/features2d.hpp>  
#include <iostream>
#include <ctype.h>
using namespace cv;
using namespace std;

Point2f point;
bool addRemovePt = false;

int main(int argc, char** argv)
{
//读取图像
cv::Mat prevGray = cv::imread("D:/images/start.bmp", CV_LOAD_IMAGE_GRAYSCALE);
cv::Mat gray = cv::imread("D:/images/stop.bmp", CV_LOAD_IMAGE_GRAYSCALE);
if (NULL == prevGray.data || NULL == gray.data)
{
cout << "failed to load images" << endl;
return 0;
}

//FAST特征点检测
std::vector<cv::KeyPoint> vecPreKeyPoints;
FastFeatureDetector fast(40); 
fast.detect(prevGray, vecPreKeyPoints);

vector<cv::Point2f> vecPrePoints;
vecPrePoints.reserve(vecPreKeyPoints.size());

for (int i = 0; i < vecPreKeyPoints.size(); i++)
{
vecPrePoints.push_back(vecPreKeyPoints[i].pt);
}

//光流跟踪
if (!vecPreKeyPoints.empty())
{
vector<cv::Point2f> vecCurrentPoints;
vector<uchar> status;
vector<float> err;
TermCriteria termcrit(CV_TERMCRIT_ITER | CV_TERMCRIT_EPS, 20, 0.03);
Size winSize(31, 31);

calcOpticalFlowPyrLK(prevGray, gray, vecPrePoints, vecCurrentPoints, status, err, winSize,
3, termcrit, 0, 0.001);

//显示跟踪结果
cv::Mat objImgShow;
cv::cvtColor(gray, objImgShow, CV_GRAY2BGR);
for (size_t i = 0; i < vecPreKeyPoints.size(); i++)
{
if (err[i] != 0)
{
const int RADIUS = 2;
cv::circle(objImgShow, vecCurrentPoints[i], RADIUS, CV_RGB(255, 0, 0), CV_FILLED);
cv::line(objImgShow, vecPrePoints[i], vecCurrentPoints[i], CV_RGB(0, 255, 0));
}
}

cv::imshow("Optical Flow", objImgShow);
cv::waitKey();
}
else
{
cout << "No FAST feature found" << endl;
}


return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值