OpenCV學習 對富特徵進行光流匹配

2021-08-04 16:27:06 字數 2623 閱讀 5692

《深入理解opencv——實用計算機視覺專案解析》第四章學習。

在富特徵上使用光流匹配的優勢在於處理過程通常較快且能容納更多的匹配點。

將結構keypoint轉化成point2f的**如下:

void keypointstopoints(const

vector

& kps, vector

& ps)

vector

left_keypoints, right_keypoints;

//尋找左右兩張圖中的特徵點

fastfeaturedetector ffd;

ffd.detect(img1, left_keypoints);

ffd.detect(img2, right_keypoints);

vector

left_points;

keypointstopoints(left_keypoints, left_points);

vector

right_points(left_keypoints.size());

keypointstopoints(right_keypoints, right_points);

//保證為灰度圖

mat imggray1, imggray2;

cvtcolor(img1, imggray1, cv_rgb2gray);

cvtcolor(img2, imggray2, cv_rgb2gray);

//計算光流域

vector

vstatus;

vector

verror;

calcopticalflowpyrlk(imggray1, imggray2, left_points, right_points, vstatus, verror);

mat imofkl = img1.clone();

for (int i = 0; i < vstatus.size(); i++)

}namedwindow("光流", window_normal);

imshow("光流", imofkl);

//去除大誤差點

vector

right_points_to_find;

vector

right_points_to_find_back_index;

for (unsigned

int i = 0; i < vstatus.size(); i++)

else

}//檢視每個正確點屬於的特徵

mat right_points_to_find_flat = mat(right_points_to_find).reshape(1, right_points_to_find.size());

vector

right_features;

keypointstopoints(right_keypoints, right_features);

mat right_features_flat = mat(right_features).reshape(1, right_features.size());

//匹配

bfmatcher matcher(cv_l2);

vector

> nearest_neighbors;

matcher.radiusmatch(right_points_to_find_flat, right_features_flat, nearest_neighbors, 2.0f);

//去除距離過近可能導致錯誤的點

setfound_in_right_points;

vector

matches;

for (int i = 0; i < nearest_neighbors.size(); i++)

else

if (nearest_neighbors[i].size()>1)

else

continue;

}else

continue;

if (found_in_right_points.find(_m.trainidx) == found_in_right_points.end())

}cout

<< "pruned"

<< matches.size() << "/"

<< nearest_neighbors.size() << "matches"

<< endl;

mat result;

drawmatches(img1, left_keypoints, img2, right_keypoints, matches, result);

namedwindow("結果", window_normal);

imshow("結果", result);

waitkey(0);

}

匹配結果如下:

1.光流

2.對富特徵進行光流匹配結果

opencv 對指定區域進行腐蝕

include include int main int argc,int argv 建立腐蝕模板 element cvcreatestructuringelementex cols,rows,anchor x,anchor y,cv shape custom,values 對區域性區域腐蝕 cvr...

OpenCV學習筆記(一) 光流法學習

1 calcopticalflowpyrlk 該方法使用的方法是基於影象金字塔的lucas kanande演算法,能夠跟蹤稀疏的光流,影象金字塔的作用是通過不斷地下取樣來減小兩幀之間光流點的運動,從而保證識別的準確性 因為l k演算法只有在位移很小的時候才有作用 具體用法如下 void calcop...

Opencv2 4學習 HOG特徵

一 什麼是hog特徵 二 如何提取hog特徵 三 視窗 win 塊 block 細胞 cell 與畫素的關係 四 特徵向量維度數目的計算 五 關於梯度直方圖的計算 重點 1 影象卷積 影象梯度一般利用影象與梯度運算元卷積實現,關於這部分內容,可以參考opencv2.4學習 影象卷積 2 梯度運算元 ...