《视觉slam十四讲》初学小白笔记(10)

 LK光流的使用

#include
#include
#include
#include
#include
#include
#include
#include
#include

using namespace std;
using namespace cv;

int main(int argc,char** argv){  //argc表示从命令行输入的参数个数,argv表示从命令行输入的参数内容
  if(argc!=2){
    cout<<"usage:LKFlow path_to_dataset"< keypoints;
  
  for(int index=0;index<100;index++){
    fin>>time_rgb>>rgb_file>>time_depth>>depth_file;
    
    color=cv::imread(path_to_dataset+"/"+rgb_file);
    depth=cv::imread(path_to_dataset+"/"+depth_file,-1);//若为0则灰度返回,若为1则原图返回,-1返回8位深度,原通道
  
    if(index==1){		//对第一个点去FAST特征点
      vectorkps;
      cv::Ptr detector=cv::FastFeatureDetector::create("ORB");
      detector->detect(color,kps);
      for(auto kp:kps){
	keypoints.push_back(kp.pt);
      }
      last_color=color;		//将当前帧赋值给上一帧,保留上一帧
      continue;
    }
  
    if(color.data==nullptr||depth.data==nullptr)
      continue;
  
  
    //对其他帧使用LK跟踪
    vector next_keypoints;
    vector prev_keypoints;
    for(auto kp:keypoints)
      prev_keypoints.push_back(kp);
  
    vector status;
    vectorerror;
    chrono::steady_clock::time_point t1=chrono::steady_clock::now();
    //cv::calcOpticalFlowPyrLK(prevImg,nextImg,prevPts,nextPts,status,error);
    //prevImg:就是你需要输入计算光流的前一帧图像
    //nextImg就是下一帧图像(可以看到一次光流就是在两针图像之间找不同)。
    //prevPts是前一帧图像中的特征点,这个特征点必须自己去找,所以在使用calcOpticalFlowPyrLK函数的时候,前面需要有一个找特征点的操作,那么一般就是找图像的角点(第一帧FAST特征点)
    //nextPts参数就是计算特征点在第二幅图像中的新的位置,然后输出。
    //特征点的新位置可能变化了,也可能没有变化,那么这种状态就存放在后一个参数status中。err就是新旧两个特征点位置的误差了,也是一个输出矩阵。
    cv::calcOpticalFlowPyrLK(last_color,color,prev_keypoints,next_keypoints,status,error);
    chrono::steady_clock::time_point t2=chrono::steady_clock::now();
    chrono::duration time_used=chrono::duration_cast>(t2-t1);
    cout<<"LK Flow use time:"<

(没运行出来,编译的时候没有问题,运行报错)

错误提示:

OpenCV Error: Assertion failed ((npoints = prevPtsMat.checkVector(2, CV_32F, true)) >= 0) in calcOpticalFlowPyrLK, file /home/xr/opencv-2.4.11/modules/video/src/lkpyramid.cpp, line 845
terminate called after throwing an instance of 'cv::Exception'
  what():  /home/xr/opencv-2.4.11/modules/video/src/lkpyramid.cpp:845: error: (-215) (npoints = prevPtsMat.checkVector(2, CV_32F, true)) >= 0 in function calcOpticalFlowPyrLK

Aborted (core dumped)

我上网查了一下,没找到解决方法。我猜测可能是c++的float是32位,python中是64位,然后不匹配导致的出错(个人猜测),有大佬看到的话,求解答~

你可能感兴趣的:(《视觉slam十四讲》初学小白笔记(10))