cmakelists:
cmake_minimum_required(VERSION 3.7)
project(LKFlow)
set(CMAKE_CXX_STANDARD 11)
find_package(OpenCV)
include_directories(${OpenCV_INCLUDE_DIRS})
set(SOURCE_FILES main.cpp)
add_executable(LKFlow ${SOURCE_FILES})
target_link_libraries(LKFlow ${OpenCV_LIBS})
source:
#include <iostream>
#include <fstream>
#include <list>
#include <vector>
#include <chrono>
using namespace std;
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/features2d/features2d.hpp>
#include <opencv2/video/tracking.hpp>
int main(int argc, char** argv)
{
if (argc != 2)
{
cout<<"usage: useLK path_to_data_folder"<<endl;
return 1;
}
string path_to_data_folder=argv[1];
string associate_file=path_to_data_folder+"/associate.txt";
ifstream fin(associate_file);
if(!fin)
{
cerr<<"I cann't find associate.txt!"<<endl;
return 1;
}
string time_rgb, rgb_file, time_depth, depth_file;
list<cv::Point2f> keypoints;
cv::Mat color, depth, last_color;
for(int index=0; index<100; index++)
{
fin>>time_rgb>>rgb_file>>time_depth>>depth_file;
color=cv::imread(path_to_data_folder+"/"+rgb_file);
depth=cv::imread(path_to_data_folder+"/"+depth_file, -1);
if(index==0)
{
vector<cv::KeyPoint> kps;
cv::Ptr<cv::FastFeatureDetector> detector = cv::FastFeatureDetector::create();
detector->detect(color,kps);
for (auto kp:kps)
keypoints.push_back(kp.pt);
last_color = color;
continue;
}
if ( color.data==nullptr || depth.data==nullptr )
{
cout<<"index="<<index<<endl;
continue;
}
vector<cv::Point2f> prev_keypoints;
vector<cv::Point2f> next_keypoints;
for (auto kp:keypoints)
prev_keypoints.push_back(kp);
vector<unsigned char> status;
vector<float> error;
cv::calcOpticalFlowPyrLK( last_color, color, prev_keypoints, next_keypoints, status, error );
int i=0;
for ( auto iter=keypoints.begin(); iter!=keypoints.end(); i++)
{
if ( status[i] == 0 )
{
cout<<next_keypoints[i]<<endl;
iter = keypoints.erase(iter);
continue;
}
*iter = next_keypoints[i];
iter++;
}
cout<<"tracked keypoints: "<<keypoints.size()<<endl;
if (keypoints.size() == 0)
{
cout<<"all keypoints are lost."<<endl;
break;
}
cv::Mat img_show = color.clone();
for ( auto kp:keypoints )
cv::circle(img_show, kp, 3, cv::Scalar(250, 0, 0));
cv::imshow("corners", img_show);
cv::waitKey(500);
last_color = color;
}
return 0;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)