首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >OpenCV SURF类错误

OpenCV SURF类错误
EN

Stack Overflow用户
提问于 2014-12-18 07:26:07
回答 1查看 251关注 0票数 2

我发现OpenCV的新类浪花SurfFeatureDetector不同。它有什么问题?

有两个图片的示例:

..................................img_1.....................................................................img_2...................................

./a.out ./img_1.png ./img_2.png一样使用它

代码语言:javascript
复制
// STL
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
// C-Standard
#include <cstdio>
#include <ctime>
#include <cstdlib>
// OpenCV
#include <opencv2/opencv.hpp>
#include <opencv2/legacy/legacy.hpp>
#include <opencv2/nonfree/nonfree.hpp>

void print(const std::string & filename, const std::vector<std::vector<cv::DMatch>> & vec) {
    FILE *file = fopen(filename.c_str(), "w");
    fprintf(file, "{\n");
    for(auto & i : vec)
        fprintf(file, "    { {%d,%d,%f}, {,%d,%d,%f} },\n",
                i[0].queryIdx, i[0].trainIdx, i[0].distance,
                i[1].queryIdx, i[1].trainIdx, i[1].distance);
    fprintf(file, "}\n");
    fclose(file);
}

void test1(const std::string & imgf_1, const std::string & imgf_2) {
    cv::Mat img_1;
    cv::Mat img_2;
    std::vector<cv::KeyPoint> keypoints_1, keypoints_2;
    cv::Mat descriptors_1, descriptors_2;
    std::vector<std::vector<cv::DMatch>> matches;

    img_1 = cv::imread(imgf_1);
    img_2 = cv::imread(imgf_2);
    int minhessin = 400;
    cv::SurfFeatureDetector detector(minhessin);
    cv::SurfDescriptorExtractor extractor;
    cv::BFMatcher bfMatcher(cv::NORM_L2);

    keypoints_1.clear(); keypoints_2.clear();
    detector.detect(img_1, keypoints_1);
    extractor.compute(img_1, keypoints_1, descriptors_1);
    detector.detect(img_2, keypoints_2);
    extractor.compute(img_2, keypoints_2, descriptors_2);
    matches.clear();
    bfMatcher.knnMatch(descriptors_1, descriptors_2, matches, 2);

    print("main_bak.log", matches);
}

void test2(const std::string & imgf_1, const std::string & imgf_2) {
    cv::Mat img_1;
    cv::Mat img_2;
    std::vector<cv::KeyPoint> keypoints_1, keypoints_2;
    cv::Mat descriptors_1, descriptors_2;
    std::vector<std::vector<cv::DMatch>> matches;

    img_1 = cv::imread(imgf_1);
    img_2 = cv::imread(imgf_2);
    const double hessianThreshold = 400;
    cv::SURF detector2(hessianThreshold);
    cv::BFMatcher bfMatcher(cv::NORM_L2);

    keypoints_1.clear(); keypoints_2.clear();
    detector2(img_1, cv::Mat(), keypoints_1, descriptors_1);
    detector2(img_2, cv::Mat(), keypoints_2, descriptors_2);
    matches.clear();
    bfMatcher.knnMatch(descriptors_1, descriptors_2, matches, 2);

    print("main.log", matches);
}

int main(int argc, char * argv[])
{
    if(argc < 3) {
        std::cout << "usage: " << argv[0] << " img_1 img_2" << std::endl;
        return 1;
    }
    test1(argv[1], argv[2]);
    test2(argv[1], argv[2]);

    return 0;
}

日志的标题5行如下所示:

main_bak.log

代码语言:javascript
复制
{
    { {0,0,0.000787}, {,0,2,0.126846} },
    { {1,1,0.001695}, {,1,167,0.353709} },
    { {2,2,0.000860}, {,2,0,0.127105} },
    { {3,3,0.002939}, {,3,5,0.333215} },
    { {4,4,0.001360}, {,4,115,0.294008} },

main.log

代码语言:javascript
复制
{
    { {0,0,0.000900}, {,0,2,0.143810} },
    { {1,1,0.024048}, {,1,107,0.621702} },
    { {2,2,0.003646}, {,2,0,0.144049} },
    { {3,3,0.032238}, {,3,5,0.604136} },
    { {4,4,0.001449}, {,4,87,0.591502} },
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-12-24 17:13:32

cv::SurfFeatureDetectorcv::SurfDescriptorExtractor是类cv::SURF的别名。结果的差异是由于以下原因:

在函数test1中,detectorextractor对象是用不同的参数初始化的。detector使用的是400minHessian值,而提取器使用的是opencv实现定义的默认值。

在函数test2中,使用hessianThreshold值为400的单个cv::SURF对象进行关键点检测和描述符计算。

若要在test2中复制test1的结果,请使用相同的参数初始化两个对象,如下所示:

代码语言:javascript
复制
int minhessin = 400;
cv::SurfFeatureDetector detector(minhessin);
cv::SurfDescriptorExtractor extractor(minHessian);
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27541235

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档