openCV C++ 图像拼接

openCV C++ 图像拼接

代码

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

using namespace std;
using namespace cv;

void Joint(Mat img1, Mat img2, const string filename)
{
	if (img1.empty() || img2.empty())
	{
		cout << "读取文件失败!" << endl;
		return;
	}

	vector<Mat> Img;
	Img.push_back(img1);
	Img.push_back(img2);
	
	Mat pano;
	Stitcher::Mode mode = Stitcher::PANORAMA;
	Ptr<Stitcher> stitcher = Stitcher::create(mode);//建立拼接器
	Stitcher::Status  status = stitcher->stitch(Img, pano);//进行拼接
	if (status != Stitcher::OK)
	{
		cout << "拼接失败" << endl;
		return;
	}
	imwrite(filename,pano);
	cout << "拼接完成" << endl;
	
}
int main()
{
	Mat img1 = imread("D:/data/2/50051016_0_rec.tif");//拼接图像1
	Mat img2 = imread("D:/data/2/50051017_0_rec.tif");//拼接图象2

	const string filename = "D:/data/2/result2.tif";//结果路径
	Joint(img1, img2, filename);
	return 0;
}

一、思路

将待拼接的两幅图像读取进数组中,创建拼接器进行拼接,最后将结果写入提前设置好的文件中。

二、函数解析

Stitcher是OpenCV的一个类,主要包括createDefault,estimateTransform,composePanorama,stitch等成员函数。
Alt

  1. Stitcher::createDefault

Stitcher Stitcher::createDefault(bool try_use_gpu=false)

Parameters:try_use_gpu -指示GPU是否应该在任何可能的情况下使用的标志。
Returns:Stitcher class instance.
  1. Vector<类型>标识符:创建一个Mat类型的数组
    Notes: 向量(Vector)是一个封装了动态大小数组的顺序容器(Sequence Container)。跟任意其它类型容器一样,它能够存放各种类型的对象。可以简单的认为,向量是一个能够存放任意类型的动态数组。
  2. imread函数
  Mat imread(const String& filename,int flags = IMREAD_COLOR);

参数filename: 待打开图片的绝对地址,需要注意的是,并不是所有文件都可以用它打开,它支持的文件如下;函数识别不是依靠文件的后缀名,而是依靠内容的编码格式;
Notes: 需要注意的是imread读取数据时会重新排列数据。

参数flags:打开的参数,这个非常重要,因为如果设置不合适的话,很容易出现预想之外的效果。它可以指导将原图读取时进行一定的转换。默认值是IMREAD_LOAD_GDAL。因此,如果是想直接处理原图,应该设置为IMREAD_UNCHANED。
Notes:

IMREAD_UNCHANGEDIf set, return the loaded image as is (with alpha channel, otherwise it gets cropped).
IMREAD_GRAYSCALEIf set, always convert image to the single channel grayscale image.
IMREAD_COLORIf set, always convert image to the 3 channel BGR color image.
IMREAD_ANYDEPTHIf set, return 16-bit/32-bit image when the input has the corresponding depth, otherwise convert it to 8-bit.
IMREAD_ANYCOLORIf set, the image is read in any possible color format.
IMREAD_LOAD_GDALIf set, use the gdal driver for loading the image.
IMREAD_REDUCED_GRAYSCALE_2If set, always convert image to the single channel grayscale image and the image size reduced 1/2.
IMREAD_REDUCED_COLOR_2If set, always convert image to the 3 channel BGR color image and the image size reduced 1/2.
IMREAD_REDUCED_GRAYSCALE_4If set, always convert image to the single channel grayscale image and the image size reduced 1/4.
IMREAD_REDUCED_COLOR_4If set, always convert image to the 3 channel BGR color image and the image size reduced 1/4.
IMREAD_REDUCED_GRAYSCALE_8If set, always convert image to the single channel grayscale image and the image size reduced 1/8.
IMREAD_REDUCED_COLOR_8If set, always convert image to the 3 channel BGR color image and the image size reduced 1/8.

原文链接:https://blog.csdn.net/firstlai/article/details/70882240

  1. void push_back(const T& x):向量尾部增加一个元素X
  • 0
    点赞
  • 31
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值