失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > 【OpenCV】SURF图像拼接和Stitcher拼接

【OpenCV】SURF图像拼接和Stitcher拼接

时间:2020-08-27 18:36:08

相关推荐

【OpenCV】SURF图像拼接和Stitcher拼接

介绍两种图像拼接的方法,一种是SURF算法的图像拼接,另一种是Stitcher硬拼接

首先先从简单的讲起

一、Stitcher直接拼接

可以实现多张图片一起拼接,只要两行代码就可以实现拼接;

1.首先准备多张图片,放入向量容器中

Mat img1 =imread("a.png");Mat img2 =imread("b.png");Mat img3 =imread("c.png");Mat img4 =imread("d.png");//图片放入容器中vector<Mat>images;images.push_back(img1);images.push_back(img2);images.push_back(img3);images.push_back(img4);

2. 创建Stitcher对象,调用拼接算法

第一行false是表示不使用gpu加速;

//保存最终拼接图Mat result;Stitcher sti=Stitcher::createDefault(false);//将vector容器中所有的图片按顺序进行拼接,输出resultStitcher::Status sta=sti.stitch(images,result);if(sta!=Stitcher::OK){cout<<"拼接失败"<<endl;}imshow("result",result);//显示

注:createDefault的方法在opencv3可用,opencv4改变了调用方式,为Stitcher::create

来看看这种拼接的效果吧

原图:

4张图

第一次输出效果:

第二次输出效果:

可以看到,虽然能够拼接,但是有时候可能会丢失一部分,导致最右边没有拼接上;

虽然使用起来很简单,但是不能每次都达到想要的效果;

这边介绍第二种拼接方法:

二、SURF算法

SURF拼接一次只能拼接两张图片,其大致步骤就是匹配两幅图像中的特征点,找到最优匹配特征点;

根据配对的特征点计算坐标映射矩阵,求出右图的透视转换坐标;然后将右图透视转换后生成的图与左图进行整合,使用copyto将两图拼接

1.查找特征点

Mat left=imread("left.png");Mat right=imread("right.png");imshow("left",left);imshow("right",right);//创建SURF算法对象Ptr<SURF> surf;//create 函数参数 海森矩阵阀值 800特征点以内surf =SURF::create(800);//创建一个暴力匹配器 用于特征点匹配BFMatcher matcher;//特征点容器 存放特征点KeyPoint 两张图准备两个vector<KeyPoint>key1,key2;//保存特征点Mat c,d;//1.查找特征点//左图 右图 识别特征点 是Mat对象 用c d保存surf->detectAndCompute(left,Mat(),key2,d);surf->detectAndCompute(right,Mat(),key1,c);//特征点对比vector<DMatch> matches;matcher.match(d,c,matches);//特征点匹配过后存入matchers容器//将匹配过后的特征点排序 从小到大,找到特征点连线sort(matches.begin(),matches.end());

2.保存最优匹配的特征点对象,进行划线

vector<DMatch>best_matches;int prtpoint=std::min(50,(int)(matches.size()*0.15));for(int i=0;i<prtpoint;i++){best_matches.push_back(matches[i]);}//2.1进行划线,连接两个最优特征点对象//NOT_DRAW_SINGLE_POINTS不画单个的点Mat outimg;drawMatches(left,key2,right,key1,best_matches,outimg,Scalar::all(-1),Scalar::all(-1),vector<char>(),DrawMatchesFlags::NOT_DRAW_SINGLE_POINTS);

3.特征点匹配

查找所有最优匹配特征点中,右图需要通过透视转换变形,而左图查找基准线

vector<Point2f>imagepoint1,imagepoint2;for (int i= 0 ;i < best_matches.size();i++){//查找特征点可连接处 右图需要通过透视转换变形imagepoint1.push_back(key1[best_matches[i].trainIdx].pt);//查找特征点可连接处 左图查找基准线imagepoint2.push_back(key2[best_matches[i].queryIdx].pt);}

4.透视转换,图形融合

根据配对的特征点计算坐标映射矩阵,求出透视转换坐标;将右图透视转换后生成的图与左图进行整合,使用copyto将两图拼接

Mat homo = findHomography(imagepoint1,imagepoint2,CV_RANSAC);//根据透视转换矩阵进行计算 右图的四个坐标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_width = imageTransForm.cols;//右转换图的宽int dst_height = left.rows;//左图的高Mat dst(dst_height,dst_width,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)));

5.拼接后图片可能存在拼接处的裂缝,还有扭曲,可以进行一些优化

//图像融合的去裂缝处理操作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);}}}

完整源码:

#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 directly(){Mat img1 =imread("a.png");Mat img2 =imread("b.png");Mat img3 =imread("c.png");Mat img4 =imread("d.png");// imshow("a",img1);// imshow("b",img2);// imshow("c",img3);// imshow("d",img4);//图片放入容器中vector<Mat>images;images.push_back(img1);images.push_back(img2);images.push_back(img3);images.push_back(img4);//保存最终拼接图Mat result;Stitcher sti=Stitcher::createDefault(false);//将vector容器中所有的图片按顺序进行拼接,输出resultStitcher::Status sta=sti.stitch(images,result);if(sta!=Stitcher::OK){cout<<"拼接失败"<<endl;}imshow("result",result);}int main(){Mat left=imread("left.png");Mat right=imread("right.png");imshow("left",left);imshow("right",right);//创建SURF算法对象Ptr<SURF> surf;//create 函数参数 海森矩阵阀值 800特征点以内surf =SURF::create(800);//创建一个暴力匹配器 用于特征点匹配BFMatcher matcher;//特征点容器 存放特征点KeyPoint 两张图准备两个vector<KeyPoint>key1,key2;//保存特征点Mat c,d;//1.查找特征点//左图 右图 识别特征点 是Mat对象 用c d保存surf->detectAndCompute(left,Mat(),key2,d);surf->detectAndCompute(right,Mat(),key1,c);//特征点对比vector<DMatch> matches;matcher.match(d,c,matches);//特征点匹配过后存入matchers容器//将匹配过后的特征点排序 从小到大,找到特征点连线sort(matches.begin(),matches.end());//2.保存最优匹配的特征点对象vector<DMatch>best_matches;int prtpoint=std::min(50,(int)(matches.size()*0.15));for(int i=0;i<prtpoint;i++){best_matches.push_back(matches[i]);}//2.1进行划线,连接两个最优特征点对象//NOT_DRAW_SINGLE_POINTS不画单个的点Mat outimg;drawMatches(left,key2,right,key1,best_matches,outimg,Scalar::all(-1),Scalar::all(-1),vector<char>(),DrawMatchesFlags::NOT_DRAW_SINGLE_POINTS);//imshow("outimg",outimg);//划线图//3.特征点匹配vector<Point2f>imagepoint1,imagepoint2;for (int i= 0 ;i < best_matches.size();i++){//查找特征点可连接处 右图需要通过透视转换变形imagepoint1.push_back(key1[best_matches[i].trainIdx].pt);//查找特征点可连接处 左图查找基准线imagepoint2.push_back(key2[best_matches[i].queryIdx].pt);}//4、透视转换 图形融合//先进行计算坐标映射矩阵Mat homo = findHomography(imagepoint1,imagepoint2,CV_RANSAC);//根据透视转换矩阵进行计算 右图的四个坐标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_width = imageTransForm.cols;//右转换图的宽int dst_height = left.rows;//左图的高Mat dst(dst_height,dst_width,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)));//5、优化图像,中间缝合处理OptimizeSeam(left,imageTransForm,dst);//输出拼接图像imshow("dst",dst);//directly();//直接拼接waitKey(0);return 0;}

感谢观看!!!!

以上就是全部内容,如果对您有帮助,欢迎点赞评论,或者发现有哪里写错的,欢迎指正!

如果觉得《【OpenCV】SURF图像拼接和Stitcher拼接》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。