OpenCV檢測平面物體

2018-10-13 10:58 更新

本教程的目標(biāo)是學(xué)習(xí)如何使用features2d和calib3d模塊來檢測場景中已知的平面對象。

測試數(shù)據(jù):使用數(shù)據(jù)文件夾中的圖像,例如box.png和box_in_scene.png。

  • 創(chuàng)建一個新的控制臺項目。讀取兩個輸入圖像:
Mat img1 = imread(argv [1],IMREAD_GRAYSCALE);
Mat img2 = imread(argv [2],IMREAD_GRAYSCALE);

  • 檢測兩個圖像中的關(guān)鍵點,并為每個關(guān)鍵點計算描述符。:

// detecting keypoints
Ptr<Feature2D> surf = SURF::create();
vector<KeyPoint> keypoints1;
Mat descriptors1;
surf->detectAndCompute(img1, Mat(), keypoints1, descriptors1);

... // do the same for the second image

  • 現(xiàn)在,找到從第一個圖像到第二個圖像之間的描述符之間的最接近的匹配:

// matching descriptors
BruteForceMatcher<L2<float> > matcher;
vector<DMatch> matches;
matcher.match(descriptors1, descriptors2, matches);

  • 可視化結(jié)果:

// drawing the results
namedWindow("matches", 1);
Mat img_matches;
drawMatches(img1, keypoints1, img2, keypoints2, matches, img_matches);
imshow("matches", img_matches);
waitKey(0);

  • 找到兩組點之間的單變圖:

vector<Point2f> points1, points2;
// fill the arrays with the points
....
Mat H = findHomography(Mat(points1), Mat(points2), RANSAC, ransacReprojThreshold);
  • 創(chuàng)建一組inlier匹配并繪制它們。使用perspectiveTransform函數(shù)映射點與單應(yīng)性:

      Mat點1投影; 透視變換(Mat(points1),points1Projected,H);

  • 使用drawMatches繪制inliers。


以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號