資源簡介
基于opencv實現,包括特征檢測,光流法,圖像金字塔實現大運動跟蹤
代碼片段和文件信息
#include?“opencv2/opencv.hpp“
#include?
#include?
using?namespace?std;
using?namespace?cv;
class?frameProcessor;
//幀處理基類
class?frameProcessor{
????public:
????????virtual?void?process(Mat?&inputMat?&ouput)=0;
};
//特征跟蹤類,繼承自幀處理基類
class?FeatureTracker?:??public?frameProcessor{
????Mat?gray;??//當前灰度圖
????Mat?gray_prev;??//之前的灰度圖
????vector?points[2];//前后兩幀的特征點
????vector?initial;//初始特征點
????vector?features;//檢測到的特征
????int?max_count;?//要跟蹤特征的最大數目
????double?qlevel;?//特征檢測的指標
????double?minDist;//特征點之間最小容忍距離
????vector?status;?//特征跟蹤狀態
????vector?err;?//跟蹤時的錯誤
public:
????FeatureTracker():max_count(500)qlevel(0.01)minDist(10.){}
????void?process(Mat?&frameMat?&output){
????????//得到灰度圖
????????cvtColor?(framegrayCV_BGR2GRAY);
????????frame.copyTo?(output);
????????//特征點太少了,重新檢測特征點
????????if(addNewPoint()){
????????????detectFeaturePoint?();
????????????//插入檢測到的特征點
????????????points[0].insert?(points[0].end?()features.begin?()features.end?());
????????????initial.insert?(initial.end?()features.begin?()features.end?());
????????}
????????//第一幀
????????if(gray_prev.empty?()){
????????????????gray.copyTo?(gray_prev);
????????}
????????//根據前后兩幀灰度圖估計前一幀特征點在當前幀的位置
????????//默認窗口是15*15
????????calcOpticalFlowPyrLK?(
????????????????gray_prev//前一幀灰度圖
????????????????gray//當前幀灰度圖
????????????????points[0]//前一幀特征點位置
????????????????points[1]//當前幀特征點位置
????????????????status//特征點被成功跟蹤的標志
????????????????err);//前一幀特征點點小區域和當前特征點小區域間的差,根據差的大小可刪除那些運動變化劇烈的點
????????int?k?=?0;
????????//去除那些未移動的特征點
????????for(int?i=0;i ????????????if(acceptTrackedPoint?(i)){
????????????????initial[k]=initial[i];
????????????????points[1][k++]?=?points[1][i];
????????????}
????????}
????????points[1].resize?(k);
????????initial.resize?(k);
????????//標記被跟蹤的特征點
????????handleTrackedPoint?(frameoutput);
????????//為下一幀跟蹤初始化特征點集和灰度圖像
????????std::swap(points[1]points[0]);
????????cv::swap(gray_prevgray);
????}
????void?detectFeaturePoint(){
????????goodFeaturesToTrack?(gray//圖片
?????????????????????????????????features//輸出特征點
?????????????????????????????????max_count//特征點最大數目
?????????????????????????????????qlevel//質量指標
?????????????????????????????????minDist);//最小容忍距離
????}
????bool?addNewPoint(){
????????????//若特征點數目少于10,則決定添加特征點
????????return?points[0].size?()<=10;
????}
????//若特征點在前后兩幀移動了,則認為該點是目標點,且可被跟蹤
????bool?acceptTrackedPoint(int?i){
????????return?status[i]&&
????????????????(abs(points[0][i].x-points[1][i].x)+
??????????????????abs(points[0][i].y-points[1][i].y)?>2);
????}
????//畫特征點
????void??handleTrackedPoint(Mat?&frameMat?&output){
????????????for(int?i=0;i ????????????????//當前特征點到初始位置用直線表示
????????????????line(outputinitial[i]points[1][i]Scalar::all?(0));
????????????????//當前位置用圈標出
????????????????circle(outputpoints[1][i]3Scalar::all(0)(-1));
????????????}
????????}
};
class?VideoProcessor{
private:
????VideoCapture?caputure
- 上一篇:數字圖像處理撲克牌識別程序
- 下一篇:劍靈輔助源碼
評論
共有 條評論