{"id":9213,"date":"2016-12-02T07:38:29","date_gmt":"2016-12-02T12:38:29","guid":{"rendered":"httpss:\/\/www.powenko.com\/wordpress\/?p=9213"},"modified":"2016-12-02T07:38:29","modified_gmt":"2016-12-02T12:38:29","slug":"04-flan-%e7%89%b9%e5%be%b5%e9%bb%9e%e3%80%80flannbasedmatcher","status":"publish","type":"post","link":"https:\/\/www.powenko.com\/wordpress\/?p=9213","title":{"rendered":"04 FLAN \u7279\u5fb5\u9ede\u3000FlannBasedMatcher"},"content":{"rendered":"<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n#include &lt;stdio.h&gt;\r\n#include &lt;iostream&gt;\r\n#include &lt;opencv2\/core\/core.hpp&gt;\r\n#include &lt;opencv2\/features2d\/features2d.hpp&gt;\r\n#include &lt;opencv2\/nonfree\/features2d.hpp&gt;\r\n#include &lt;opencv2\/highgui\/highgui.hpp&gt;\r\n#include &lt;opencv2\/nonfree\/nonfree.hpp&gt;\r\n\r\nusing namespace cv;\r\n\r\nvoid readme();\r\n\r\n\/** @function main *\/\r\nint main( int argc, char** argv )\r\n{\r\n    \/\/if( argc != 3 )\r\n    \/\/{ readme(); return -1; }\r\n    \r\n    Mat img_1 = imread( &quot;\/Users\/powenko\/Desktop\/doughnut.png&quot;, CV_LOAD_IMAGE_GRAYSCALE );\r\n    Mat img_2 = imread( &quot;\/Users\/powenko\/Desktop\/doughnuts.png&quot;, CV_LOAD_IMAGE_GRAYSCALE );\r\n    \r\n    if( !img_1.data || !img_2.data )                                                    \/\/\u5982\u679c\u6578\u64da\u70ba\u7a7a\r\n    { std::cout&lt;&lt; &quot; --(!) Error reading images &quot; &lt;&lt; std::endl; return -1; }\r\n    \r\n    \r\n    \r\n    \/\/-- Step 1: Detect the keypoints using SURF Detector \u00a0\u00a0\u00a0\u00a0\/\/\u7b2c\u4e00\u6b65\uff0c\u7528SIFT\u7b97\u5b50\u6aa2\u6e2c\u95dc\u9375\u9ede\r\n    int minHessian = 400;\r\n    \r\n    SurfFeatureDetector detector( minHessian );\r\n    std::vector&lt;KeyPoint&gt; keypoints_1, keypoints_2;\r\n    \r\n    detector.detect( img_1, keypoints_1 );\r\n    detector.detect( img_2, keypoints_2 );\r\n    \r\n    \/\/-- Draw keypoints \u00a0\/\/\u5728\u5716\u50cf\u4e2d\u756b\u51fa\u7279\u5fb5\u9ede\r\n    Mat img_keypoints_1; Mat img_keypoints_2;\r\n    \r\n    drawKeypoints( img_1, keypoints_1, img_keypoints_1, Scalar::all(-1), DrawMatchesFlags::DEFAULT );\r\n    drawKeypoints( img_2, keypoints_2, img_keypoints_2, Scalar::all(-1), DrawMatchesFlags::DEFAULT );\r\n    \r\n    \/\/-- Show detected (drawn) keypoints\r\n    imshow(&quot;Keypoints 1&quot;, img_keypoints_1 );\r\n    imshow(&quot;Keypoints 2&quot;, img_keypoints_2 );\r\n    \r\n    \/\/\u8a08\u7b97\u7279\u5fb5\r\n    SurfDescriptorExtractor extractor;\/\/\u5b9a\u7fa9\u5c0d\u8c61\r\n    \r\n    Mat descriptors_1,descriptors_2;\/\/\u5b58\u653e\u7279\u5fb5\u5411\u91cf\u7684\u8209\u9663\r\n    \r\n    extractor.compute(img_1,keypoints_1,descriptors_1);\/\/\u8a08\u7b97\u7279\u5fb5\u5411\u91cf\r\n    extractor.compute(img_2,keypoints_2,descriptors_2);\r\n    \r\n    \r\n    \/\/-- Step 3: Matching descriptor vectors with a brute force matcher\r\n    BFMatcher matcher(NORM_L2);\r\n    std::vector&lt; DMatch &gt; matches;\r\n    matcher.match( descriptors_1, descriptors_2, matches );\r\n    \r\n    \/\/-- Draw matches\r\n    Mat img_matches;\r\n    drawMatches( img_1, keypoints_1, img_2, keypoints_2, matches, img_matches );\r\n    \r\n    \/\/-- Show detected matches\r\n    imshow(&quot;Matches&quot;, img_matches );\r\n    \r\n    \/\/-- Step 3: Matching descriptor vectors using FLANN matcher\r\n    cv::FlannBasedMatcher matcher2;\r\n    std::vector&lt; DMatch &gt; matches2;\r\n    matcher2.match( descriptors_1, descriptors_2, matches2 );\r\n    \r\n    double max_dist = 0; double min_dist = 100;\r\n    \r\n    \/\/-- Quick calculation of max and min distances between keypoints\r\n    for( int i = 0; i &lt; descriptors_1.rows; i++ )\r\n    { double dist = matches&#x5B;i].distance;\r\n        if( dist &lt; min_dist ) min_dist = dist;\r\n        if( dist &gt; max_dist ) max_dist = dist;\r\n    }\r\n    \r\n    printf(&quot;-- Max dist : %f \\n&quot;, max_dist );\r\n    printf(&quot;-- Min dist : %f \\n&quot;, min_dist );\r\n    \r\n    \/\/-- Draw only &quot;good&quot; matches (i.e. whose distance is less than 2*min_dist )\r\n    \/\/-- PS.- radiusMatch can also be used here.\r\n    std::vector&lt; DMatch &gt; good_matches;\r\n    \r\n    for( int i = 0; i &lt; descriptors_1.rows; i++ )\r\n    { if( matches&#x5B;i].distance &lt; 2*min_dist )\r\n    { good_matches.push_back( matches&#x5B;i]); }\r\n    }\r\n    \r\n    \/\/-- Draw only &quot;good&quot; matches\r\n    Mat img_matches2;\r\n    drawMatches( img_1, keypoints_1, img_2, keypoints_2,\r\n                good_matches, img_matches2, Scalar::all(-1), Scalar::all(-1),\r\n                vector&lt;char&gt;(), DrawMatchesFlags::NOT_DRAW_SINGLE_POINTS );\r\n    \r\n    \/\/-- Show detected matches  \r\n    imshow( &quot;Good Matches&quot;, img_matches2 );\r\n    \r\n    \r\n    waitKey(0);\r\n    \r\n    return 0;\r\n}\r\n\r\n\/** @function readme *\/\r\nvoid readme()\r\n{ std::cout &lt;&lt; &quot; Usage: .\/SURF_detector &lt;img1&gt; &lt;img2&gt;&quot; &lt;&lt; std::endl; }\r\n\r\n<\/pre>\n<p><a href=\"httpss:\/\/www.powenko.com\/wordpress\/wp-content\/uploads\/2016\/12\/Screen-Shot-2016-12-02-at-8.35.42-PM.png\"><img loading=\"lazy\" decoding=\"async\" src=\"httpss:\/\/www.powenko.com\/wordpress\/wp-content\/uploads\/2016\/12\/Screen-Shot-2016-12-02-at-8.35.42-PM-570x335.png\" alt=\"screen-shot-2016-12-02-at-8-35-42-pm\" width=\"570\" height=\"335\" class=\"alignnone size-medium wp-image-9214\" srcset=\"https:\/\/www.powenko.com\/wordpress\/wp-content\/uploads\/2016\/12\/Screen-Shot-2016-12-02-at-8.35.42-PM-570x335.png 570w, https:\/\/www.powenko.com\/wordpress\/wp-content\/uploads\/2016\/12\/Screen-Shot-2016-12-02-at-8.35.42-PM-300x176.png 300w, https:\/\/www.powenko.com\/wordpress\/wp-content\/uploads\/2016\/12\/Screen-Shot-2016-12-02-at-8.35.42-PM-316x186.png 316w, https:\/\/www.powenko.com\/wordpress\/wp-content\/uploads\/2016\/12\/Screen-Shot-2016-12-02-at-8.35.42-PM-120x71.png 120w, https:\/\/www.powenko.com\/wordpress\/wp-content\/uploads\/2016\/12\/Screen-Shot-2016-12-02-at-8.35.42-PM-210x124.png 210w, https:\/\/www.powenko.com\/wordpress\/wp-content\/uploads\/2016\/12\/Screen-Shot-2016-12-02-at-8.35.42-PM-496x292.png 496w, https:\/\/www.powenko.com\/wordpress\/wp-content\/uploads\/2016\/12\/Screen-Shot-2016-12-02-at-8.35.42-PM-140x82.png 140w, https:\/\/www.powenko.com\/wordpress\/wp-content\/uploads\/2016\/12\/Screen-Shot-2016-12-02-at-8.35.42-PM.png 1421w\" sizes=\"(max-width: 570px) 100vw, 570px\" \/><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>#include &lt;stdio.h&gt; #include &lt;iostream&gt; #inc [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":9214,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[338],"tags":[],"class_list":["post-9213","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-ch06-surfsift"],"_links":{"self":[{"href":"https:\/\/www.powenko.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/9213"}],"collection":[{"href":"https:\/\/www.powenko.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.powenko.com\/wordpress\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.powenko.com\/wordpress\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.powenko.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=9213"}],"version-history":[{"count":2,"href":"https:\/\/www.powenko.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/9213\/revisions"}],"predecessor-version":[{"id":9218,"href":"https:\/\/www.powenko.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/9213\/revisions\/9218"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.powenko.com\/wordpress\/index.php?rest_route=\/wp\/v2\/media\/9214"}],"wp:attachment":[{"href":"https:\/\/www.powenko.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=9213"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.powenko.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=9213"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.powenko.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=9213"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}