OpenCV1.0接口参考:OpenCV:金字塔Lucas-Kanade光流算法之函数接口cvCalcOpticalFlowPyrLK的使用
void calcOpticallFlowPyrLK (
InuputArray 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
);
#include
#include
#include
#include
#include
const int MAX_CORNERS = 500;
int main()
{
cv::Mat imageA = cv::imread("img00001.png", 1);
cv::Mat imageB = cv::imread("img00002.png", 1);
//使用灰度图像进行角点检测
cv::Mat imageA_gray, imageB_gray;
cv::cvtColor(imageA, imageA_gray, cv::COLOR_BGR2GRAY);
cv::cvtColor(imageB, imageB_gray, cv::COLOR_BGR2GRAY);
//设置角点检测参数
std::vector cornersA;
int max_corners = MAX_CORNERS;
double quality_level = 0.01;
double min_distance = 5.0;
int block_size = 3;
bool use_harris = false;
double k = 0.04;
//角点检测
cv::goodFeaturesToTrack(imageA_gray,
cornersA,
max_corners,
quality_level,
min_distance,
cv::Mat(),
block_size,
use_harris,
k);
cv::Size winSize(10, 10);
cv::TermCriteria criteria = cv::TermCriteria(
cv::TermCriteria::MAX_ITER + cv::TermCriteria::EPS,
20,
0.03);
//亚像素检测
cv::cornerSubPix(imageA_gray, cornersA, winSize, cv::Size(-1, -1), criteria);
std::vector cornersB;
std::vector status;
std::vector err;
cv::calcOpticalFlowPyrLK(imageA_gray, imageB_gray, cornersA, cornersB, status, err, winSize, 5, criteria, 0, 0.001);
cv::Mat imageC = imageB.clone();
for( int i = 0; i< cornersA.size(); i++ )
{
if(status[i] == '0') continue;
cv::Point p0 = cv::Point(cvRound(cornersA[i].x), cvRound(cornersA[i].y ));
cv::Point p1 = cv::Point(cvRound(cornersB[i].x), cvRound( cornersB[i].y));
line( imageC, p0, p1, cv::Scalar(0,0,255), 1, CV_AA);
}
cv::imshow("ImageA",imageA);
cv::imshow("ImageB",imageB);
cv::imshow("LKpyr_OpticalFlow",imageC);
cv::waitKey(0);
return 0;
}
img00001.png
img00002