OpenCV 4.12.0
開源計算機視覺
載入中...
搜尋中...
無匹配項
平面物件檢測

前一篇教程: Features2D + 單應性查詢已知物體
下一篇教程: AKAZE 區域性特徵匹配

原始作者Victor Eruhimov
相容性OpenCV >= 3.0

本教程的目標是學習如何使用 features2dcalib3d 模組來檢測場景中已知的平面物體。

測試資料:使用您資料資料夾中的影像,例如 box.png 和 box_in_scene.png。

  • 建立一個新的控制檯專案。讀取兩張輸入影像。
    Mat img1 = imread(argv[1], IMREAD_GRAYSCALE);
    Mat img2 = imread(argv[2], IMREAD_GRAYSCALE);
    
  • 檢測兩張影像中的關鍵點,並計算每個關鍵點的描述符。
    // 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
    
  • 現在,找到第一張影像的描述符與第二張影像描述符之間的最接近匹配:
    // matching descriptors
    BruteForceMatcher<L2<float> > matcher;
    vector<DMatch> matches;
    matcher.match(descriptors1, descriptors2, matches);
    
  • 視覺化結果:
    // 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);
    
  • 建立一組內點匹配並繪製它們。 使用 perspectiveTransform 函式用單應性對映點

    Mat points1Projected; perspectiveTransform(Mat(points1), points1Projected, H);

  • 使用 drawMatches 繪製內點。