微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决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 举报,一经查实,本站将立刻删除。