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以上的版本,也可以实现拼接了。

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值