代码
#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等成员函数。
- Stitcher::createDefault
Stitcher Stitcher::createDefault(bool try_use_gpu=false)
Parameters: | try_use_gpu -指示GPU是否应该在任何可能的情况下使用的标志。 |
---|---|
Returns: | Stitcher class instance. |
- Vector<类型>标识符:创建一个Mat类型的数组
Notes: 向量(Vector)是一个封装了动态大小数组的顺序容器(Sequence Container)。跟任意其它类型容器一样,它能够存放各种类型的对象。可以简单的认为,向量是一个能够存放任意类型的动态数组。 - imread函数
Mat imread(const String& filename,int flags = IMREAD_COLOR);
参数filename: 待打开图片的绝对地址,需要注意的是,并不是所有文件都可以用它打开,它支持的文件如下;函数识别不是依靠文件的后缀名,而是依靠内容的编码格式;
Notes: 需要注意的是imread读取数据时会重新排列数据。
参数flags:打开的参数,这个非常重要,因为如果设置不合适的话,很容易出现预想之外的效果。它可以指导将原图读取时进行一定的转换。默认值是IMREAD_LOAD_GDAL。因此,如果是想直接处理原图,应该设置为IMREAD_UNCHANED。
Notes:
IMREAD_UNCHANGED | If set, return the loaded image as is (with alpha channel, otherwise it gets cropped). |
---|---|
IMREAD_GRAYSCALE | If set, always convert image to the single channel grayscale image. |
IMREAD_COLOR | If set, always convert image to the 3 channel BGR color image. |
IMREAD_ANYDEPTH | If set, return 16-bit/32-bit image when the input has the corresponding depth, otherwise convert it to 8-bit. |
IMREAD_ANYCOLOR | If set, the image is read in any possible color format. |
IMREAD_LOAD_GDAL | If set, use the gdal driver for loading the image. |
IMREAD_REDUCED_GRAYSCALE_2 | If set, always convert image to the single channel grayscale image and the image size reduced 1/2. |
IMREAD_REDUCED_COLOR_2 | If set, always convert image to the 3 channel BGR color image and the image size reduced 1/2. |
IMREAD_REDUCED_GRAYSCALE_4 | If set, always convert image to the single channel grayscale image and the image size reduced 1/4. |
IMREAD_REDUCED_COLOR_4 | If set, always convert image to the 3 channel BGR color image and the image size reduced 1/4. |
IMREAD_REDUCED_GRAYSCALE_8 | If set, always convert image to the single channel grayscale image and the image size reduced 1/8. |
IMREAD_REDUCED_COLOR_8 | If 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
- void push_back(const T& x):向量尾部增加一个元素X