1. 程式人生 > 程式設計 >opencv3/C++實現光流點追蹤

opencv3/C++實現光流點追蹤

光流金字塔

calcOpticalFlowPyrLK()函式引數說明:

void calcOpticalFlowPyrLK(
InputArray prevImg,//第一個8位輸入影象或者通過 buildOpticalFlowPyramid()建立的金字塔
InputArray nextImg,//第二個輸入影象或者和prevImg相同尺寸和型別的金字塔
InputArray prevPts,//二維點向量儲存找到的光流;點座標必須是單精度浮點數
InputOutputArray nextPts,//輸出二維點向量(用單精度浮點座標)包括第二幅影象中計算的輸入特徵的新點位置;當OPTFLOW_USE_INITIAL_FLOW 標誌通過,向量必須有和輸入一樣的尺寸。
OutputArray status,//輸出狀態向量(無符號char);如果相應的流特徵被發現,向量的每個元素被設定為1,否則,被置為0.
OutputArray err,//輸出錯誤向量;向量的每個元素被設為相應特徵的一個錯誤,誤差測量的型別可以在flags引數中設定;如果流不被發現然後錯誤未被定義(使用status(狀態)引數找到此情形)。
Size winSize = Size(21,21),//在每個金字塔水平搜尋視窗的尺寸。
int maxLevel = 3,//最大金字塔層數; 如果設定為0,則不使用金字塔(單層),如果設定為1,則使用兩個層次,依此類推; 如果將金字塔傳遞給輸入,則演算法將使用與金字塔一樣多的級別,但不超過maxLevel。
TermCriteria criteria = TermCriteria(TermCriteria::COUNT+TermCriteria::EPS,30,0.01),//指定迭代搜尋演算法的終止標準(指定的最大迭代次數criteria.maxCount或搜尋視窗移動小於criteria.epsilon)
int flags = 0,//操作標誌
double minEigThreshold = 1e-4 //計算光流方程的2×2標準矩陣的最小特徵值除以視窗中的畫素數量;如果這個值小於minEigThreshold,那麼一個相應的特徵被過濾出來,且它的光流不被處理,所以它允許去除壞點提升效能。
);
#include<opencv2/opencv.hpp>
using namespace cv;

//光流跟蹤
Mat frame,gray,pr_frame,pr_gray;
std::vector<Point2f> inPoints;
std::vector<Point2f> fpts[2];
void trackFeature();

int main()
{
  VideoCapture capture;
  capture.open(0);
  if(!capture.isOpened())
  {
    printf("can not open the camear......\n");
    return -1;
  }
  namedWindow("input",CV_WINDOW_AUTOSIZE);
  namedWindow("output",CV_WINDOW_AUTOSIZE);

  while (capture.read(frame))
  {  
    cvtColor(frame,COLOR_BGR2GRAY);
    if (fpts[0].size() < 40)
    {
      imshow("input",frame);
      std::vector<Point2f> features;
      //角點檢測
      goodFeaturesToTrack(gray,features,300,0.01,10);
      fpts[0].insert(fpts[0].end(),features.begin(),features.end());
      inPoints.insert(inPoints.end(),features.end());
    }
    else
      printf("object tracking......\n"); 
    if (pr_gray.empty()) 
      gray.copyTo(pr_gray);
    trackFeature();
    for (int i = 0; i < fpts[0].size(); i++) 
      circle(frame,fpts[0][i],2,Scalar(0,255,0),8,0);
    gray.copyTo(pr_gray);
    frame.copyTo(pr_frame);
    imshow("output",frame);
    waitKey(1);
  }
  waitKey(0);
  capture.release();
  return 0;
}


void trackFeature()
{
  std::vector<uchar> status;
  std::vector<float> errors;
  //計算稀疏特徵集的光流
  calcOpticalFlowPyrLK(pr_gray,fpts[0],fpts[1],status,errors);
  int k = 0;
  for (int i = 0; i < fpts[1].size(); i++)
  {
    double dist = abs(fpts[0][i].x-fpts[1][i].x) + abs(fpts[0][i].y-fpts[1][i].y);
    if (dist > 2 && status[i])
    {
      inPoints[k] = inPoints[i];
      fpts[1][k++] = fpts[1][i];
    }
  }
  inPoints.resize(k);
  fpts[1].resize(k);
  //繪製光流軌跡
  RNG rng(0); 
  for (int i = 0; i < fpts[0].size(); i++)
  {
    Scalar color = Scalar(rng.uniform(0,255),rng.uniform(0,255));
    line(frame,inPoints[i],fpts[1][i],color,2);
    circle(frame,2);
  }
  std::swap(fpts[1],fpts[0]);
}

以上這篇opencv3/C++實現光流點追蹤就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支援我們。