首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >基于SIFT键点描述符和同形矩阵的图像拼接

基于SIFT键点描述符和同形矩阵的图像拼接
EN

Code Review用户
提问于 2023-05-25 03:43:59
回答 1查看 172关注 0票数 6

我已经编写了一个代码来缝合2幅图像,使用SIFT键盘描述符和单曲矩阵进行透视变换。是否有任何领域的代码需要改进或优化?

代码语言:javascript
复制
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include
#include
using namespace cv;
using namespace std;
using namespace xfeatures2d;

void get_Images(Mat &img1, Mat &img2){  
    img1 = imread("../1.jpg");
    img2 = imread("../2.jpg");
    }

void get_keyPoints_n_Descriptors(Mat img1, Mat img2, vector &kp1, vector &kp2, Mat &des1, Mat &des2){
    Ptr detector = SiftFeatureDetector::create();
    detector->detectAndCompute(img1, noArray(), kp1, des1);
    detector->detectAndCompute(img2, noArray(), kp2, des2);
}

void get_matches(vector &matches, Mat des1, Mat des2){
    FlannBasedMatcher matcher;
    matcher.match(des1, des2, matches);

    if(matches.size()<4){
        cout<<"too few matches"< &good_matches, Mat des1, Mat des2, vector matches){
    double min_dist = 100;
    double max_dist = 0;

    for(int i = 0; i< des1.rows; i++){
        double dist = matches[i].distance;
        if(dist < min_dist) min_dist = dist;
        if(dist>max_dist) max_dist = dist;
    }

    //Use min distance to find good matches
    int minMatch = 8;
    //vector good_matches;
    for(int i = 0; i< des1.rows; i++){
        if(matches[i].distance < 3*min_dist && matches.size()>minMatch){
            good_matches.push_back(matches[i]);
        }
    }
    //Use all keypoints if number of good matches is less than min matches
    if(good_matches.size() < minMatch){
        for(int i = 0; i < des1.rows; i++){
            if(i < good_matches.size()){
                good_matches[i] = matches[i];
            }
            else{
                good_matches.push_back(matches[i]);
            }
        }
    }

}

void get_homography(Mat &Homography, vector kp1, vector kp2, vector &good_matches){
    //use source and destination to find homography between 2 images
    vector src, dst;
    for(int i = 0; i  kp1, kp2;
    Mat des1, des2;
    get_keyPoints_n_Descriptors(img1, img2, kp1, kp2, des1, des2);

    //get all matches
    vector matches;
    get_matches(matches, des1, des2);

    //get good matches
    vector good_matches;
    find_good_matches(good_matches, des1, des2, matches);

    //Find homography
    Mat Homography;
    get_homography(Homography, kp1, kp2, good_matches);

    //get warped and stitched image
    Mat final;
    final = warp_n_stitch(img1, img2, Homography);
    imshow("Stitched Image", final);
    waitKey();

    return 0;
}
EN

回答 1

Code Review用户

发布于 2023-05-25 07:16:10

我不明白为什么我们需要包括或。

我不喜欢这样:

使用名称空间cv;使用命名空间std;使用命名空间xfeatures2d;

您刚刚放弃了名称空间的所有优点:

  • 现在没有针对定义其他库所用名称的新版本库的保护。
  • 对于读者来说,很难看到每个标识符来自哪个库。

与直接在get_Images中初始化两个cv::Mat对象相比,曾经调用的D11函数并没有提供多少好处:

代码语言:javascript
复制
int main()
{
    auto img1 = cv::imread("../1.jpg");
    auto img2 = cv::imread("../2.jpg");
    ⋮

我们需要更有力地检查这些读取是否成功。如果这两个图像都是空Mat,我们应该打印一条消息到std::cerr并返回EXIT_FAILURE (从)。

最好是从函数返回值,而不是要求用户通过引用传递“”参数:

代码语言:javascript
复制
    auto const matches = get_matches(des1, des2);
    auto const good_matches = find_good_matches(des1, des2, matches);
    auto const Homography = get_homography(kp1, kp2, good_matches);

可能还包括:

代码语言:javascript
复制
    auto const [kp1, kp2, des1, des2] = get_keyPoints_n_Descriptors(img1, img2);
票数 5
EN
页面原文内容由Code Review提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://codereview.stackexchange.com/questions/285153

复制
相关文章

相似问题

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