全景图像拼接

全景图像拼接(opencv3.4.2)

原图:

   

     图一                                                                                             图二                                                                               图三

   

                            图四                                                                                        图五

 

opencv3.4.2   SIFT特征点提取:

#include <opencv2/opencv.hpp>
#include <opencv2/xfeatures2d.hpp>
#include <iostream>

using namespace cv;
using namespace std;
using namespace cv::xfeatures2d;

int main(int argc, char** argv) {
    Mat src = imread("L:/opencv_picture/10.jpg");
    if (src.empty()) {
        printf("could not load image...\n");
        return -1;
    }
    namedWindow("input image", CV_WINDOW_AUTOSIZE);
    imshow("input image", src);

    int numFeatures = 400;
    Ptr<SIFT> detector = SIFT::create(numFeatures);
    vector<KeyPoint> keypoints;
    detector->detect(src, keypoints, Mat());
    printf("Total KeyPoints : %d\n", keypoints.size());

    Mat keypoint_img;
    drawKeypoints(src, keypoints, keypoint_img, Scalar::all(-1), DrawMatchesFlags::DEFAULT);
    namedWindow("SIFT KeyPoints", CV_WINDOW_AUTOSIZE);
    imshow("SIFT KeyPoints", keypoint_img);

    waitKey(0);
    return 0;
}

   

 

SIFT特征点匹配:

#include <opencv2/opencv.hpp>
#include <opencv2/xfeatures2d.hpp>
#include <iostream>
#include <math.h>

using namespace cv;
using namespace std;
using namespace cv::xfeatures2d;

int main(int argc, char** argv) {
    Mat img1 = imread("L:opencv_picture/9.jpg");
    Mat img2 = imread("L:opencv_picture/10.jpg");
    if (!img1.data || !img2.data) {
        return -1;
    }
    imshow("object image", img1);
    imshow("object in scene", img2);

    // surf featurs extraction
    int minHessian = 800;
    Ptr<SURF> detector = SURF::create(minHessian);
    vector<KeyPoint> keypoints_obj;
    vector<KeyPoint> keypoints_scene;
    Mat descriptor_obj, descriptor_scene;
    detector->detectAndCompute(img1, Mat(), keypoints_obj, descriptor_obj);
    detector->detectAndCompute(img2, Mat(), keypoints_scene, descriptor_scene);

    // matching
    FlannBasedMatcher matcher;
    vector<DMatch> matches;
    matcher.match(descriptor_obj, descriptor_scene, matches);

    // find good matched points
    double minDist = 1000;
    double maxDist = 0;
    for (int i = 0; i < descriptor_obj.rows; i++) {
        double dist = matches[i].distance;
        if (dist > maxDist) {
            maxDist = dist;
        }
        if (dist < minDist) {
            minDist = dist;
        }
    }
    printf("max distance : %f\n", maxDist);
    printf("min distance : %f\n", minDist);
    vector<DMatch> goodMatches;
    for (int i = 0; i < descriptor_obj.rows; i++) {
        double dist = matches[i].distance;
        if (dist < max(4 * minDist, 0.02)) {
            goodMatches.push_back(matches[i]);
        }
    }

    Mat matchesImg;
    drawMatches(img1, keypoints_obj, img2, keypoints_scene, goodMatches, matchesImg, Scalar::all(-1),
        Scalar::all(-1), vector<char>(), DrawMatchesFlags::NOT_DRAW_SINGLE_POINTS
    );
    imshow("Flann Matching Result", matchesImg);

    waitKey(0);
    return 0;
}

 

 

 

两张图像拼接:

 

#include <iostream>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/stitching.hpp>
#include "windows.h"

using namespace std;
using namespace cv;

bool try_use_gpu = false;
vector<Mat> imgs;
string result_name = "dst1.jpg";
int main(int argc, char * argv[])
{
    Mat img1 = imread("L:/opencv_picture/9.jpg");
    Mat img2 = imread("L:/opencv_picture/10.jpg");
    imshow("p1", img1);
    imshow("p2", img2);

    long t0 = GetTickCount();

    if (img1.empty() || img2.empty())
    {
        cout << "Can't read image" << endl;
        return -1;
    }
    imgs.push_back(img1);
    imgs.push_back(img2);

    Stitcher stitcher = Stitcher::createDefault(try_use_gpu);
    // 使用stitch函数进行拼接
    Mat pano;
    Stitcher::Status status = stitcher.stitch(imgs, pano);
    if (status != Stitcher::OK)
    {
        cout << "Can't stitch images, error code = " << int(status) << endl;
        return -1;
    }
    long t1 = GetTickCount();
    imwrite(result_name, pano);
    Mat pano2 = pano.clone();
    // 显示源图像,和结果图像
    imshow("全景图像", pano);

    cout << "Time: " << t1 - t0 << endl;

    if (waitKey() == 27)
        return 0;
 }

 

 

五张图像全景图像拼接结果:

 

posted @ 2020-12-11 14:05  量子与太极  阅读(447)  评论(0编辑  收藏  举报