OpenCV跟踪使用光流 [英] OpenCV tracking using optical flow
问题描述
我使用它作为我的跟踪算法的基础。
I use this to functions as a base of my tracking algorithm.
//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光流函数计算这个像素在下一个图像上的位置?
cv::calcOpticalFlowPyrLK
takes vector of points from the previous image as input, and returns appropriate points on the next image. Suppose I have random pixel (x, y) on the previous image, how can I calculate position of this pixel on the next image using OpenCV optical flow function?
推荐答案
在编写时, cv :: goodFeaturesToTrack
将图像作为输入,生成一个它认为跟踪良好的点的向量。这些是基于他们从周围环境中脱颖而出的能力来选择的,并且是基于哈里斯角的形象。跟踪器通常通过将第一图像传递给goodFeaturesToTrack并获得一组要跟踪的特征来初始化。这些特征然后可以被传递到 cv :: calcOpticalFlowPyrLK
作为先前的点,以及序列中的下一个图像,并且它将产生下一个点作为输出,然后变成
As you write, cv::goodFeaturesToTrack
takes an image as input and produces a vector of points which it deems "good to track". These are chosen based on their ability to stand out from their surroundings, and are based on Harris corners in the image. A tracker would normally be initialised by passing the first image to goodFeaturesToTrack and obtaining a set of features to track. These features could then be passed to cv::calcOpticalFlowPyrLK
as the previous points, along with the next image in the sequence and it will produce the next points as output, which then become input points in the next iteration.
如果你想尝试跟踪一组不同的像素(而不是 cv :: goodFeaturesToTrack
或类似的函数),然后简单地提供这些 cv :: calcOpticalFlowPyrLK
以及下一个图像。
If you want to try to track a different set of pixels (rather than features generated by cv::goodFeaturesToTrack
or a similar function), then simply provide these to cv::calcOpticalFlowPyrLK
along with the next image.
很简单,在代码中:
// 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;
}
这篇关于OpenCV跟踪使用光流的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!