我用它来作为我的跟踪算法的基础。
//1. detect the features
cv::goodFeaturesToTrack(gray_prev, // the image
features, // the output detected features
max_count, // the maximum number of features
qlevel, // quality level
minDist); // min distance between two features
// 2. track features
cv::calcOpticalFlowPyrLK(
gray_prev, gray, // 2 consecutive images
points_prev, // input point positions in first im
points_cur, // output point positions in the 2nd
status, // tracking success
err); // tracking error
cv::calcOpticalFlowPyrLK
将前一图像中的点矢量作为输入,并在下一图像上返回适当的点。假设我在前一个图像上有随机像素(x,y),如何使用OpenCV光流功能计算下一个图像上该像素的位置?
答案 0 :(得分:29)
在你写作时,cv::goodFeaturesToTrack
将一个图像作为输入并产生一个它认为“很好跟踪”的点矢量。这些是根据他们从周围环境中脱颖而出的能力来选择的,并且基于图像中的哈里斯角落。通常通过将第一个图像传递给goodFeaturesToTrack并获得一组要跟踪的特征来初始化跟踪器。然后,这些特征可以作为前面的点传递给cv::calcOpticalFlowPyrLK
,以及序列中的下一个图像,它将产生下一个点作为输出,然后在下一次迭代中成为输入点。
如果您想尝试跟踪不同的像素集(而不是cv::goodFeaturesToTrack
或类似函数生成的特征),那么只需将这些像素提供给cv::calcOpticalFlowPyrLK
以及下一张图像。
很简单,在代码中:
// Obtain first image and set up two feature vectors
cv::Mat image_prev, image_next;
std::vector<cv::Point> features_prev, features_next;
image_next = getImage();
// Obtain initial set of features
cv::goodFeaturesToTrack(image_next, // the image
features_next, // the output detected features
max_count, // the maximum number of features
qlevel, // quality level
minDist // min distance between two features
);
// Tracker is initialised and initial features are stored in features_next
// Now iterate through rest of images
for(;;)
{
image_prev = image_next.clone();
feature_prev = features_next;
image_next = getImage(); // Get next image
// Find position of feature in new image
cv::calcOpticalFlowPyrLK(
image_prev, image_next, // 2 consecutive images
points_prev, // input point positions in first im
points_next, // output point positions in the 2nd
status, // tracking success
err // tracking error
);
if ( stopTracking() ) break;
}
答案 1 :(得分:1)
cv :: calcOpticalFlowPyrLK(..)函数使用参数:
cv :: calcOpticalFlowPyrLK(prev_gray,curr_gray,features_prev,features_next,status,err);
cv::Mat prev_gray, curr_gray;
std::vector<cv::Point2f> features_prev, features_next;
std::vector<uchar> status;
std::vector<float> err;
在下一帧中查找像素的最简单(部分)代码:
features_prev.push_back(cv::Point(4, 5));
cv::calcOpticalFlowPyrLK(prev_gray, curr_gray, features_prev, features_next, status, err);
如果成功找到像素status[0] == 1
,features_next[0]
将在下一帧中显示像素的坐标。可以在此示例中找到值信息:OpenCV/samples/cpp/lkdemo.cpp