OpenCv C++:图像未正确变形

问题描述

我正在学习如何使用以下链接将两个图像拼接在一起,但无论我如何计算单应性和 warpPerspective,两个图像都不会拼接在一起。

https://learnopencv.com/feature-based-image-alignment-using-opencv-c-python/

以下是图片拼接的源码

包括部分

#include <opencv2/features2d.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/opencv.hpp>
#include <opencv2/core/types.hpp>
#include <opencv2/calib3d/calib3d.hpp>
#include <vector>
#include <iostream>

全局变量


using namespace std;
using namespace cv;

const float inlier_threshold = 2.5f; // distance threshold to identify inliers
const float nn_match_ratio = 0.8f;   // Nearest neighbor matching ratio

主要功能

int main(void)
{
    puts("opening");
    Mat img1 = imread("uttower_right.jpg",IMREAD_GRAYSCALE);   // To be Aligned
    Mat img2 = imread("large2_uttower_left.jpg",IMREAD_GRAYSCALE); // Reference
    Mat img3 = Mat(img2.rows,img2.cols,CV_8UC1);
    //img2.copyTo(img3);

    Mat homography;

    vector<KeyPoint> kpts1,kpts2;
    Mat desc1,desc2;
    puts("Have opened");

    Ptr<AKAZE> akaze = AKAZE::create();
    akaze->detectAndCompute(img1,noArray(),kpts1,desc1);
    akaze->detectAndCompute(img2,kpts2,desc2);

    puts("have commputed akaze");

    BFMatcher matcher(norM_HAMMING);
    vector< vector<DMatch> > nn_matches;
    matcher.knnMatch(desc1,desc2,nn_matches,2);
    puts("Have done match");

    vector<KeyPoint> matched1,matched2;
    vector<Point2f> inliers1,inliers2;

    for (size_t i = 0; i < nn_matches.size(); i++) {
        DMatch first = nn_matches[i][0];
        float dist1 = nn_matches[i][0].distance;
        float dist2 = nn_matches[i][1].distance;

        if (dist1 < nn_match_ratio * dist2) {
            matched1.push_back(kpts1[first.queryIdx]);
            matched2.push_back(kpts2[first.trainIdx]);

            inliers1.push_back(kpts1[first.queryIdx].pt);
            inliers2.push_back(kpts1[first.trainIdx].pt);
        }

    }
    printf("Matches %d %d\n",matched1.size(),matched2.size());

    homography = findHomography(inliers1,inliers2,RANSAC);
    warpPerspective(img1,img3,homography,img2.size());

    //display input and output
    imshow("Input1",img1);
    imshow("Input2",img2);
    imshow("Input3",img3);
    waitKey(0);

    return 0;
}

使用的图片

enter image description here

enter image description here

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)