slambook2+ch7+orb_cv代码修改

该博客介绍了一个使用ORB(Oriented FAST and Rotated BRIEF)特征进行图像匹配的OpenCV程序。程序从固定路径加载图像,检测和计算ORB特征,然后匹配描述符,并筛选出有效匹配对,最后展示匹配结果。通过CMakeLists.txt文件配置项目依赖OpenCV库。
摘要由CSDN通过智能技术生成

代码部分
int main ( )
取消参数传入

if (argc != 3) {
cout << “usage: feature_extraction img1 img2” << endl;
return 1;
}
删除此段代码,因为程序不需要从外面传入图片了

[ Mat img_1 = imread("/home/slambook2/ch7/1.png", CV_LOAD_IMAGE_COLOR);
直接将图片地址写入,将图片变换为图像矩阵。CV_LOAD_IMAGE_COLOR保留图片的彩色信息

完整代码

#include <iostream>
#include <opencv2/core/core.hpp>
#include <opencv2/features2d/features2d.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <chrono>

using namespace std;
using namespace cv;

int main (  )
{


    //-- 读取图像,assert检查图像是否为空,assert表达式为0时报错
    Mat img_1 = imread("/home/automobile/wcm/slambook2/ch7/1.png", CV_LOAD_IMAGE_COLOR);
    Mat img_2 = imread("/home/automobile/wcm/slambook2/ch7/2.png", CV_LOAD_IMAGE_COLOR);
    assert(img_1.data != nullptr && img_2.data != nullptr);

    //-- 初始化
    std::vector<KeyPoint> keypoints_1, keypoints_2;
    Mat descriptors_1, descriptors_2;
    Ptr<FeatureDetector> detector = ORB::create();
    Ptr<DescriptorExtractor> descriptor = ORB::create();
    Ptr<DescriptorMatcher> matcher = DescriptorMatcher::create("BruteForce-Hamming");

    //-- 第一步:检测 Oriented FAST 角点位置
    //detect为detector结构体类型成员,detector中的detect函数发现的关键点存储在keypoints_1/2中  
    chrono::steady_clock::time_point t1 = chrono::steady_clock::now();
    detector->detect(img_1, keypoints_1);
    detector->detect(img_2, keypoints_2);

    //-- 第二步:根据角点位置计算 BRIEF 描述子
    //compute为descriptor结构体类型成员,descriptor中的compute函数将关键点转换为描述子并存储在descriptors_1/2中               
    descriptor->compute(img_1, keypoints_1, descriptors_1);
    descriptor->compute(img_2, keypoints_2, descriptors_2);
    chrono::steady_clock::time_point t2 = chrono::steady_clock::now();
    chrono::duration<double> time_used = chrono::duration_cast<chrono::duration<double>>(t2 - t1);
    cout << "extract ORB cost = " << time_used.count() << " seconds. " << endl;

    //带有关键点的图片存储在img_1中  
    Mat outimg1;
    drawKeypoints(img_1, keypoints_1, outimg1, Scalar::all(-1), DrawMatchesFlags::DEFAULT);
    imshow("ORB features", outimg1);

    //-- 第三步:对两幅图像中的BRIEF描述子进行匹配,使用 Hamming 距离
    //匹配的descriptors存储在matches中  
    vector<DMatch> matches;
    t1 = chrono::steady_clock::now();
    matcher->match(descriptors_1, descriptors_2, matches);
    t2 = chrono::steady_clock::now();
    time_used = chrono::duration_cast<chrono::duration<double>>(t2 - t1);
    cout << "match ORB cost = " << time_used.count() << " seconds. " << endl;

    //-- 第四步:匹配点对筛选
    // 计算最小距离和最大距离
    //min_max为bool型结构体,结构体中distance成员为double型 
    auto min_max = minmax_element(matches.begin(), matches.end(),
                                  [](const DMatch &m1, const DMatch &m2) { return m1.distance < m2.distance; });
    double min_dist = min_max.first->distance;
    double max_dist = min_max.second->distance;

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

    //当描述子之间的距离大于两倍的最小距离时,即认为匹配有误.但有时候最小距离会非常小,设置一个经验值30作为下限.
    std::vector<DMatch> good_matches;
    for (int i = 0; i < descriptors_1.rows; i++) {
        if (matches[i].distance <= max(2 * min_dist, 30.0)) {
            good_matches.push_back(matches[i]);
        }
    }

    //-- 第五步:绘制匹配结果
    Mat img_match;
    Mat img_goodmatch;
    drawMatches(img_1, keypoints_1, img_2, keypoints_2, matches, img_match);
    drawMatches(img_1, keypoints_1, img_2, keypoints_2, good_matches, img_goodmatch);
    imshow("all matches", img_match);
    imshow("good matches", img_goodmatch);
    waitKey(0);

    return 0;
}

2 CMakeLists.txt部分

project(ch7)
ch7是我的项目名

set(OpenCV_DIR /path/to/opencv/build)
解决CMake找不到opencv库

target_link_libraries(ch7 ${OpenCV_LIBS})
ch7为项目名

CMakeList.txt内容

cmake_minimum_required(VERSION 3.10)
project(ch7)

set(CMAKE_BUILD_TYPE "Release")
add_definitions("-DENABLE_SSE")
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_FLAGS "-std=c++11 -o2 ${SSE_FLAGS} -msse4")
list(APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake)
set(OpenCV_DIR /path/to/opencv/build)

find_package(OpenCV 3 REQUIRED)

include_directories(${OpenCV_INCLUDE_DIRS})


add_executable(ch7  cmake-build-debug/orb_cv.cpp )
target_link_libraries(ch7 ${OpenCV_LIBS})

原文地址
https://blog.csdn.net/qiao_syf/article/details/106448597?ops_request_misc=%25257B%252522request%25255Fid%252522%25253A%252522161201716716780262519246%252522%25252C%252522scm%252522%25253A%25252220140713.130102334.pc%25255Fall.%252522%25257D&request_id=161201716716780262519246&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2allfirst_rank_v2~rank_v29-5-106448597.pc_search_result_cache&utm_term=slambook%252Fch7%252Ffeature_extraction.cpp

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值