使用光流进行OpenCV跟踪

Ale*_*pus 18 c++ opencv tracking computer-vision

我使用它作为我的跟踪算法的基础.

    //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
Run Code Online (Sandbox Code Playgroud)

cv::calcOpticalFlowPyrLK将前一图像中的点矢量作为输入,并在下一图像上返回适当的点.假设我在前一个图像上有随机像素(x,y),如何使用OpenCV光流功能计算下一个图像上该像素的位置?

Chr*_*ris 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;
}
Run Code Online (Sandbox Code Playgroud)