目录
- 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;
}
改变
- 首先 stitching 在opencv2 以后 被 模块化,不存在stitcher.hpp 了。
- 故而 createDefault() 无法使用。
- 若要使用 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以上的版本,也可以实现拼接了。