1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
| #include "opencv2/opencv.hpp"
using namespace std; using namespace cv;
int main() { double matchThresh;
Mat object = imread("box.png"); Mat scene = imread("box_in_scene.png");
int algorithm = 1; Ptr<Feature2D> detector; switch (algorithm) { case 1: detector = BRISK::create(); matchThresh = 350; break;
case 2: detector = KAZE::create(); matchThresh = 0.1; break;
case 3: detector = AKAZE::create(); matchThresh = 250; break; }
vector<KeyPoint> objKPs, scnKPs; detector->detect(object, objKPs); detector->detect(scene, scnKPs);
Mat objDesc, scnDesc; detector->compute(object, objKPs, objDesc); detector->compute(scene, scnKPs, scnDesc);
Ptr<BFMatcher> matcher = BFMatcher::create(); vector<DMatch> matches; matcher->match(objDesc, scnDesc, matches);
vector<DMatch> goodMatches; for (int i = 0; i < objDesc.rows; i++) { if (matches[ i ].distance < matchThresh) goodMatches.push_back(matches[ i ]); }
if (goodMatches.size() > 0) { cout << "Found " << goodMatches.size() << " good matches."; } else { cout << "Didn't find a single good match. Quitting!"; return -1; }
Mat result; drawMatches(object, objKPs, scene, scnKPs, goodMatches, result, Scalar(0, 255, 0), Scalar::all(-1), vector<char>(), DrawMatchesFlags::NOT_DRAW_SINGLE_POINTS);
vector<Point2f> goodP1, goodP2; for (int i = 0; i < goodMatches.size(); i++) { goodP1.push_back(objKPs[ goodMatches[ i ].queryIdx ].pt); goodP2.push_back(scnKPs[ goodMatches[ i ].trainIdx ].pt); } Mat homoChange = findHomography(goodP1, goodP2);
vector<Point2f> corners1(4), corners2(4); corners1[ 0 ] = Point2f(0, 0); corners1[ 1 ] = Point2f(object.cols - 1, 0); corners1[ 2 ] = Point2f(object.cols - 1, object.rows - 1); corners1[ 3 ] = Point2f(0, object.rows - 1); perspectiveTransform(corners1, corners2, homoChange);
for (int i = 0; i < 4; i++) corners2[ i ].x += object.cols;
line(result, corners2[ 0 ], corners2[ 1 ], Scalar::all(255), 2); line(result, corners2[ 1 ], corners2[ 2 ], Scalar::all(255), 2); line(result, corners2[ 2 ], corners2[ 3 ], Scalar::all(255), 2); line(result, corners2[ 3 ], corners2[ 0 ], Scalar::all(255), 2);
imshow("Result", result); waitKey();
return 0; }
|