【问题标题】:FLANN matcher-OPENCV 3-min max disatanceFLANN matcher-OPENCV 3 分钟最大距离
【发布时间】:2015-12-06 07:07:18
【问题描述】:

我有一些代码使用 Flann 匹配器和 ORB 检测器来查找人的两个图像之间的特征。我在ubuntu上使用opencv 3。我有几个疑问。。 代码如下:

  #include <iostream>
#include </home/sruthi/opencv/include/opencv2/opencv.hpp>

using namespace cv;

//void readme();

/** @function main */
int main(int argc, char** argv)
{
   if( argc != 3 )
  { //readme(); 
     return -1; }

  Mat img_object = imread( argv[1], IMREAD_GRAYSCALE );
  Mat img_scene = imread( argv[2], IMREAD_GRAYSCALE );


    if (!img_object.data || !img_scene.data)
    {
        std::cout << " --(!) Error reading images " << std::endl; return -1;
    }

    //-- Step 1: Detect the keypoints using ORB Detector
    Ptr<FeatureDetector> detector = ORB::create();

    std::vector<KeyPoint> keypoints_object, keypoints_scene;

    detector->detect(img_object, keypoints_object);
    detector->detect(img_scene, keypoints_scene);

    //-- Step 2: Calculate descriptors (feature vectors)
    Ptr<DescriptorExtractor> extractor = ORB::create();

    Mat descriptors_object, descriptors_scene;

    extractor->compute(img_object, keypoints_object, descriptors_object);
    extractor->compute(img_scene, keypoints_scene, descriptors_scene);

    descriptors_object.convertTo(descriptors_object,CV_32F);
    descriptors_scene.convertTo(descriptors_scene,CV_32F);


    //-- Step 3: Matching descriptor vectors using FLANN matcher
    Ptr<DescriptorMatcher> matcher = DescriptorMatcher::create("FlannBased");
    std::vector< DMatch > matches;
    matcher->match(descriptors_object, descriptors_scene, matches);

    double max_dist = 0; double min_dist = 100;

    //-- Quick calculation of max and min distances between keypoints
    for (int i = 0; i < descriptors_object.rows; i++)
    {
        double dist = matches[i].distance;
        if (dist < min_dist) min_dist = dist;
        if (dist > max_dist) max_dist = dist;
    }

    printf("-- Max dist : %f \n", max_dist);
    printf("-- Min dist : %f \n", min_dist);

    //-- Draw only "good" matches (i.e. whose distance is less than 3*min_dist )
    std::vector< DMatch > good_matches;

    for (int i = 0; i < descriptors_object.rows; i++)
    {
        if (matches[i].distance < 3 * min_dist)
        {
            good_matches.push_back(matches[i]);
        }
    }

    Mat img_matches;

    drawMatches(img_object, keypoints_object, img_scene, keypoints_scene, good_matches, img_matches, Scalar::all(-1), Scalar::all(-1), std::vector<char>(), DrawMatchesFlags::NOT_DRAW_SINGLE_POINTS);

    //-- Localize the object
    std::vector<Point2f> obj;
    std::vector<Point2f> scene;
    for (int i = 0; i < good_matches.size(); i++)
    {
        //-- Get the keypoints from the good matches
        obj.push_back(keypoints_object[good_matches[i].queryIdx].pt);
        scene.push_back(keypoints_scene[good_matches[i].trainIdx].pt);
    }


    //-- Show detected matches
  imshow( "Good Matches", img_matches );

  for( int i = 0; i < (int)good_matches.size(); i++ )
  { printf( "-- Good Match [%d] Keypoint 1: %d  -- Keypoint 2: %d  \n", i, good_matches[i].queryIdx, good_matches[i].trainIdx ); }

    waitKey(0);
    return 0;


}
  1. 双最大距离 = 0;双 min_dist = 100; 为什么我们将这些距离分别声明为 0 和 100?作为输出我得到 最大距离:488.559113 最小距离:100.000000。好像错了。

  2. if (matches[i].distance

  3. 是否必须有平行线,如官方文档中所示。我没有得到平行线。我无法发布屏幕截图。

【问题讨论】:

  • @miki 你能帮忙吗?

标签: ubuntu opencv3.0 orb flann


【解决方案1】:
  1. 这些初始化似乎已针对编写此代码的特定情况进行了调整。
  2. 这又是由于调整了参数。尝试使用不同的图像集进行试验,您应该会看到结果有所变化。此外,另一个好的做法是使用次优距离来找到合适的匹配项。
  3. 正确的匹配应该有平行线,因为匹配在 2 个图像中的相对位置应该保持不变。任何交叉线都是不正确的匹配项。

【讨论】:

    猜你喜欢
    • 2015-07-21
    • 1970-01-01
    • 2013-03-30
    • 1970-01-01
    • 2023-03-19
    • 2020-01-20
    • 1970-01-01
    • 1970-01-01
    • 2015-12-22
    相关资源
    最近更新 更多