一、引言
应用场景:车载摄像头、球型摄像头——十字路口、鱼眼摄像头——跑酷
图像拼接的前提条件,图像要有公共的部分
二、图像拼接的三种算法
1、SURF(精准度较高,画面好,计算耗时导致效率较低,清晰度也还行)
- 拼接的图片比较少(2张)
- 拼接的效果比较好
- 可以拼接不规则的图像
2、STITCH(效率高,可以多张图拼接,代码简单,效果差)
- 多图拼接
- 拼接的效果不好,会丢帧、图像失真、计算过程偏暴力,效率高(代码少,实现简单)
- 规则的图像
3、ORB 算法
两张或者多张都可以拼接,需要借助第三方库(SURF、STITCH是opencv自带)
SURF算法执行步骤
(1)准备好左右两张图片
(2) 创建SURF对象,需要添加cx::xfeatures2d命名空间
(3)设置SURF海森矩阵阈值,官方建议600-800
(4)实例化暴力匹配器
(5)寻找特征点
(6)进行特征点对比,保存下来
(7) 将特征点从小到大进行排序
(8)保存距离最短(最优)的特征点对象
(9)最短距离的特征点连线
(10)特征点配准(两张图公共部分的点合成一个点,这样图的公共部分就粘在一起了)
(11)透视转换
(12)优化最终生成的图片,去除黑边
从成像原理上看,你怎么判断两个点是相同的?
通过图像三个通道(RGB)的数值
图像融合——去裂缝(黑边)处理
优化两图的连接处,使得拼接自然。如果是直角则没必要进行图像融合优化
三、代码
#include <iostream>
#include <opencv2/opencv.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/xfeatures2d.hpp>
#include <opencv2/calib3d.hpp>
#include <opencv2/imgproc.hpp>
using namespace std;
using namespace cv;
using namespace cv::xfeatures2d;
typedef struct
{
Point2f left_top;
Point2f left_bottom;
Point2f right_top;
Point2f right_bottom;
}FOUR_CORNERS_T;
FOUR_CORNERS_T corners;
//计算配准图的四个顶点坐标
void CalcCorners(const Mat& H, const Mat& src)
{
double v2[] = { 0, 0, 1 };//左上角
double v1[3];//变换后的坐标值
Mat V2 = Mat(3, 1, CV_64FC1, v2); //列向量
Mat V1 = Mat(3, 1, CV_64FC1, v1); //列向量
V1 = H * V2;
//左上角(0,0,1)
cout << "V2: " << V2 << endl;
cout << "V1: " << V1 << endl;
corners.left_top.x = v1[0] / v1[2];
corners.left_top.y = v1[1] / v1[2];
//左下角(0,src.rows,1)
v2[0] = 0;
v2[1] = src.rows;
v2[2] = 1;
V2 = Mat(3, 1, CV_64FC1, v2); //列向量
V1 = Mat(3, 1, CV_64FC1, v1); //列向量
V1 = H * V2;
corners.left_bottom.x = v1[0] / v1[2];
corners.left_bottom.y = v1[1] / v1[2];
//右上角(src.cols,0,1)
v2[0] = src.cols;
v2[1] = 0;
v2[2] = 1;
V2 = Mat(3, 1, CV_64FC1, v2); //列向量
V1 = Mat(3, 1, CV_64FC1, v1); //列向量
V1 = H * V2;
corners.right_top.x = v1[0] / v1[2];
corners.right_top.y = v1[1] / v1[2];
//右下角(src.cols,src.rows,1)
v2[0] = src.cols;
v2[1] = src.rows;
v2[2] = 1;
V2 = Mat(3, 1, CV_64FC1, v2); //列向量
V1 = Mat(3, 1, CV_64FC1, v1); //列向量
V1 = H * V2;
corners.right_bottom.x = v1[0] / v1[2];
corners.right_bottom.y = v1[1] / v1[2];
}
//优化两图的连接处,使得拼接自然
void OptimizeSeam(Mat& img1, Mat& trans, Mat& dst)
{
int start = MIN(corners.left_top.x, corners.left_bottom.x);//开始位置,即重叠区域的左边界
double processWidth = img1.cols - start;//重叠区域的宽度
int rows = dst.rows;
int cols = img1.cols; //注意,是列数*通道数
double alpha = 1;//img1中像素的权重
for (int i = 0; i < rows; i++)
{
uchar* p = img1.ptr<uchar>(i); //获取第i行的首地址
uchar* t = trans.ptr<uchar>(i);
uchar* d = dst.ptr<uchar>(i);
for (int j = start; j < cols; j++)
{
//如果遇到图像trans中无像素的黑点,则完全拷贝img1中的数据
if (t[j * 3] == 0 && t[j * 3 + 1] == 0 && t[j * 3 + 2] == 0)
{
alpha = 1;
}
else
{
//img1中像素的权重,与当前处理点距重叠区域左边界的距离成正比,实验证明,这种方法确实好
alpha = (processWidth - (j - start)) / processWidth;
}
d[j * 3] = p[j * 3] * alpha + t[j * 3] * (1 - alpha);
d[j * 3 + 1] = p[j * 3 + 1] * alpha + t[j * 3 + 1] * (1 - alpha);
d[j * 3 + 2] = p[j * 3 + 2] * alpha + t[j * 3 + 2] * (1 - alpha);
}
}
}
void example()
{
Mat img1 = imread("C:/Users/59834/Desktop/image/a.png");
Mat img2 = imread("C:/Users/59834/Desktop/image/b.png");
Mat img3 = imread("C:/Users/59834/Desktop/image/c.png");
Mat img4 = imread("C:/Users/59834/Desktop/image/d.png");
imshow("img1",img1);
imshow("img2",img2);
imshow("img3",img3);
imshow("img4",img4);
//带顺序容器vector
vector<Mat>images;
images.push_back(img1);
images.push_back(img2);
images.push_back(img3);
images.push_back(img4);
//用来保存最终拼接图
Mat result;
//false 不使用GPU加速
Stitcher sti = Stitcher::createDefault(false);
//将向量容器中所有的图片按照顺序进行拼接,结果保存在result中
Stitcher::Status sta = sti.stitch(images,result);
if(sta != Stitcher::OK)
{
cout<<"canot Stitcher"<<endl;
}
imshow("result",result);
waitKey(0);
}
int main()
{
Mat left = imread("D:/ChaunYi/cy3/img/a1.png");
Mat right = imread("D:/ChaunYi/cy3/img/a2.png");
imshow("left",left);
imshow("right",right);
//创建surf对象
//create参数 海森矩阵阀值
Ptr<SURF> surf;
surf = SURF::create(800);
//实例化一个暴力匹配器
BFMatcher matcher;
vector<KeyPoint>key1,key2;
Mat c,d;
//寻找特征点
surf->detectAndCompute(left,Mat(),key2,d);
surf->detectAndCompute(right,Mat(),key1,c);
//进行特征点对比 ,保存下来
vector<DMatch> matches;
matcher.match(d,c,matches);
//排序 从小到大
sort(matches.begin(),matches.end());
//保存距离最短(最优)的特征点对象
vector<DMatch>good_matches;
int ptrPoint = std::min(50,(int)(matches.size()*0.15));
for (int i = 0;i < ptrPoint;i++)
{
good_matches.push_back(matches[i]);
}
//最短距离的特征点连线
Mat outimg;
drawMatches(left,key2,right,key1,good_matches,outimg,
Scalar::all(-1),Scalar::all(-1),
vector<char>(),DrawMatchesFlags::NOT_DRAW_SINGLE_POINTS);
imshow("outimg",outimg);
//特征点配准
vector<Point2f>imagepoint1,imagepoint2;
for (int i=0;i<good_matches.size();i++)
{
imagepoint1.push_back(key1[good_matches[i].trainIdx].pt);
imagepoint2.push_back(key2[good_matches[i].queryIdx].pt);
}
//透视转换
Mat homo = findHomography(imagepoint1,imagepoint2,CV_RANSAC);
imshow("homo",homo);
CalcCorners(homo,right);
Mat imageTransForm;
warpPerspective(right,imageTransForm,homo,
Size(MAX(corners.right_top.x,corners.right_bottom.x),left.rows));
imshow("imageTransForm",imageTransForm);
int dst_widht = imageTransForm.cols;
int dst_height = left.rows;
Mat dst(dst_height,dst_widht,CV_8UC3);
dst.setTo(0);
imageTransForm.copyTo(dst(Rect(0,0,imageTransForm.cols,imageTransForm.rows)));
left.copyTo(dst(Rect(0,0,left.cols,left.rows)));
//最终图片优化去除黑边
OptimizeSeam(left,imageTransForm,dst);
imshow("finaly",dst);
waitKey(0);
//example();
return 0;
}
四、实现效果
注意:
图片复制的路径不要复制“属性 ”——“安全”中的路径
复制“常规”中的“位置”即可