opencv图片拼接
介绍:两张不同角度的图可以拼接为同一张图,并且消除拼接的痕迹。
思路:
1.读取图片,进行缩放为统一尺度大小
2.进行特征检测+特征点描述
3.用描述子匹配
4.进行仿射变换
5.拼接
不多说,上代码
#include <opencv2/opencv.hpp>
#include <opencv2/xfeatures2d/nonfree.hpp>
using namespace std;
using namespace cv::xfeatures2d;
using namespace cv;
Mat srcimg01,srcimg02;
vector<KeyPoint> kp1, kp2;
Mat dst1, dst2;
void join(vector<DMatch> good_matches)
{
vector<Point2f> pf1, pf2;
for (int i = 0; i < good_matches.size(); i++)
{
pf1.push_back(kp1[good_matches[i].queryIdx].pt);
pf2.push_back(kp2[good_matches[i].trainIdx].pt);
}
Mat homo = findHomography(pf2, pf1, RANSAC);
cout << "变换矩阵:\n " << homo << endl;
struct P
{
Point2f left_top;
Point2f left_bottom;
Point2f right_top;
Point2f right_bottom;
};
P p;
cout << "1" << endl;
//左上角
double v2[] = {
0,0,1 };
double v1[3];
Mat V1 = Mat(3, 1, CV_64FC1, v1);
Mat V2 = Mat(3, 1, CV_64FC1, v2);
V1 = homo * V2;
p.left_top.x = v1[0] / v2[2];
p.left_top.y = v1[1] / v2[2];
//左下角
v2[0]