Opencv2以后图像拼接代码

目录

  • Opencv2 以前拼接代码
  • Opencv2 以后拼接代码
  • 改变

Opencv2 以前拼接代码

#include <iostream>
#include <fstream>
#include <sstream>

#include <opencv2/imgproc/imgproc.hpp>
#include "gdal_priv.h"
#include <opencv2/core/core.hpp>
#include<opencv2/stitching/stitcher.hpp>
#include <opencv2/highgui/highgui.hpp>


using namespace cv;
using namespace std;


///
// Functions

bool try_use_gpu = false;
vector<Mat> imgs;
string result_name = "result1.tif";
int main(int argc, char * argv[])
{
	Mat img1 = imread("F:\\liujianchen\\任务数据\\2 影像匹配和拼接\\50051016_0_rec.tif");
	Mat img2 = imread("F:\\liujianchen\\任务数据\\2 影像匹配和拼接\\50051017_0_rec.tif");

	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);
	imwrite(result_name, pano);
	double fScale = 0.1;//缩放系数

						//计算目标图像的大小
	Size dsize = Size(pano.cols*fScale, pano.rows*fScale);
	Mat imagedst = Mat(dsize, CV_32S);
	resize(pano, imagedst, dsize);

	imshow("全景图像", imagedst);
	if (waitKey() == 27)
		return 0;
}

Opencv2 以后拼接代码

#include "pch.h"
#include <iostream>
#include <fstream>
#include <sstream>
#include <opencv2/imgproc/imgproc.hpp>
#include "gdal_priv.h"
#include <opencv2/core/core.hpp>
#include <opencv2/stitching.hpp>
#include "opencv2/imgcodecs.hpp"
#include <opencv2/highgui/highgui.hpp>

using namespace cv;
using namespace std;


Stitcher::Mode mode = Stitcher::PANORAMA;
vector<Mat> imgs;
string result_name = "result1.tif";

int main(int argc, char * argv[])
{
	Mat img1 = imread("X:\\rsimagesss\\任务数据\\2 影像匹配和拼接\\50051016_0_rec.tif");
	Mat img2 = imread("X:\\rsimagesss\\任务数据\\2 影像匹配和拼接\\50051017_0_rec.tif");

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

	Mat pano;
	Ptr<Stitcher> stitcher = Stitcher::create(mode);

    // 使用stitch函数进行拼
	Stitcher::Status status = stitcher->stitch(imgs, pano);

	imwrite(result_name, pano);
	

	double fScale = 0.1;//缩放系数

						//计算目标图像的大小
	Size dsize = Size(pano.cols*fScale, pano.rows*fScale);
	Mat imagedst = Mat(dsize, CV_32S);
	resize(pano, imagedst, dsize);

	imshow("全景图像", imagedst);
	if (waitKey() == 27)
		return 0;
}

改变

  1. 首先 stitching 在opencv2 以后 被 模块化,不存在stitcher.hpp 了。
  2. 故而 createDefault() 无法使用。
  3. 若要使用 opencv2 以后的版本 只需要将
Stitcher stitcher = Stitcher::createDefault(try_use_gpu);
Stitcher::Status status = stitcher.stitch(imgs, pano)

两句 改为 :

Ptr<Stitcher> stitcher = Stitcher::create(mode);
Stitcher::Status status = stitcher->stitch(imgs, pano);

即可。 也需要定义 mode。

这样就可以就算使用opencv2以上的版本,也可以实现拼接了。

  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
以下是一个简单的立体图像拼接代码示例,使用Python和OpenCV库实现: ```python import cv2 import numpy as np # 读取左右图像 img_left = cv2.imread('left.jpg') img_right = cv2.imread('right.jpg') # 转换为灰度图像 gray_left = cv2.cvtColor(img_left, cv2.COLOR_BGR2GRAY) gray_right = cv2.cvtColor(img_right, cv2.COLOR_BGR2GRAY) # 初始化SIFT检测器 sift = cv2.xfeatures2d.SIFT_create() # 使用SIFT检测关键点和描述符 kp_left, des_left = sift.detectAndCompute(gray_left, None) kp_right, des_right = sift.detectAndCompute(gray_right, None) # 使用FLANN匹配器进行关键点匹配 flann = cv2.FlannBasedMatcher() matches = flann.knnMatch(des_left, des_right, k=2) # 根据Lowe's ratio test进行筛选 good_matches = [] for m, n in matches: if m.distance < 0.7 * n.distance: good_matches.append(m) # 从匹配的关键点中提取左右图像的对应点 pts_left = np.float32([kp_left[m.queryIdx].pt for m in good_matches]) pts_right = np.float32([kp_right[m.trainIdx].pt for m in good_matches]) # 计算基础矩阵 F, mask = cv2.findFundamentalMat(pts_left, pts_right, cv2.FM_RANSAC) # 从基础矩阵中提取左右图像的对应点 pts_left = pts_left[mask.ravel() == 1] pts_right = pts_right[mask.ravel() == 1] # 根据对应点计算左右图像的单应性矩阵 H_left, _ = cv2.findHomography(pts_left, pts_right, cv2.RANSAC, 5.0) H_right, _ = cv2.findHomography(pts_right, pts_left, cv2.RANSAC, 5.0) # 将左右图像进行单应性变换 img_left_warped = cv2.warpPerspective(img_left, H_left, (img_left.shape[1], img_left.shape[0])) img_right_warped = cv2.warpPerspective(img_right, H_right, (img_right.shape[1], img_right.shape[0])) # 将两幅图像拼接在一起 result = np.zeros((img_left.shape[0], img_left.shape[1]*2, img_left.shape[2]), dtype=np.uint8) result[:, :img_left.shape[1], :] = img_left_warped result[:, img_left.shape[1]:, :] = img_right_warped # 显示拼接结果 cv2.imshow('Result', result) cv2.waitKey(0) cv2.destroyAllWindows() ``` 这段代码首先读取左右图像,然后将它们转换为灰度图像,并使用SIFT检测器检测关键点和描述符。然后使用FLANN匹配器进行关键点匹配,并根据Lowe's ratio test进行筛选,提取出左右图像的对应点。 接下来,使用OpenCV的`findFundamentalMat`函数计算基础矩阵,并从基础矩阵中提取左右图像的对应点。然后使用`findHomography`函数分别计算左右图像的单应性矩阵,并将它们应用到左右图像上,使它们对齐。 最后,将左右图像拼接在一起,并显示拼接结果。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值