opencv3/C++ FLANN特征匹配

使用函数detectAndCompute()检测关键点并计算描述符
函数detectAndCompute()参数说明:

void detectAndCompute( 
InputArray image, //图像
InputArray mask, //掩模
CV_OUT std::vector& keypoints,//输出关键点的集合
OutputArray descriptors,//计算描述符(descriptors[i]是为keypoints[i]的计算描述符)
bool useProvidedKeypoints=false //使用提供的关键点
);

match()从查询集中查找每个描述符的最佳匹配。
参数说明:

void match( 
InputArray queryDescriptors, //查询描述符集
InputArray trainDescriptors, //训练描述符集合
CV_OUT std::vector& matches, //匹配
InputArray mask=noArray() //指定输入查询和描述符的列表矩阵之间的允许匹配的掩码
) const;

FLANN特征匹配示例:

#include
#include
using namespace cv;
using namespace cv::xfeatures2d;

//FLANN对高维数据较快
int main()
{
    Mat src1,src2;
    src1 = imread("E:/image/image/card2.jpg");
    src2 = imread("E:/image/image/cards.jpg");
    if (src1.empty() || src2.empty())
    {
        printf("can ont load images....\n");
        return -1;
    }
    imshow("image1", src1);
    imshow("image2", src2);

    int minHessian = 400;
    //选择SURF特征
    Ptrdetector = SURF::create(minHessian);
    std::vectorkeypoints1;
    std::vectorkeypoints2;
    Mat descriptor1, descriptor2;
    //检测关键点并计算描述符
    detector->detectAndCompute(src1, Mat(), keypoints1, descriptor1);
    detector->detectAndCompute(src2, Mat(), keypoints2, descriptor2);

    //基于Flann的描述符匹配器
    FlannBasedMatcher matcher;
    std::vectormatches;
    //从查询集中查找每个描述符的最佳匹配
    matcher.match(descriptor1, descriptor2, matches);
    double minDist = 1000;
    double maxDist = 0;
    for (int i = 0; i < descriptor1.rows; i++)
    {
        double dist = matches[i].distance;
        printf("%f \n", dist);
        if (dist > maxDist)
        {
            maxDist = dist;
        }
        if (dist < minDist)
        {
            minDist = dist;
        }

    }
    //DMatch类用于匹配关键点描述符的
    std::vectorgoodMatches;
    for (int i = 0; i < descriptor1.rows; i++)
    {
        double dist = matches[i].distance;
        if (dist < max(2.5*minDist, 0.02))
        {
            goodMatches.push_back(matches[i]);
        }
    }
    Mat matchesImg;
    drawMatches(src1, keypoints1, src2, keypoints2, goodMatches, matchesImg, Scalar::all(-1), Scalar::all(-1), std::vector<char>(), DrawMatchesFlags::NOT_DRAW_SINGLE_POINTS);
    imshow("output", matchesImg);

    waitKey();
    return 0;
}

opencv3/C++ FLANN特征匹配_第1张图片opencv3/C++ FLANN特征匹配_第2张图片opencv3/C++ FLANN特征匹配_第3张图片

你可能感兴趣的:(OpenCV)