stitcher 算法
优点: 简单便捷, 可以一次性拼合多张图片。
缺点: 1. 会卡顿一下。
2. 多图拼接的时候可能会丢失掉一些图片。
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
vectorimages;
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"<
SURF:
优点: 拼接的图像效果质量理想。
缺点: 一次性只支持图片两两拼接, 如果多图拼接只能先拼一个再拼下一个。
SURF流程:
使用:
1. 特征点采集
2.保存最优特征点对象
3. 特征点周围匹配
4. 透视转换, 图像融合
5. 优化图像
.
Mat left = imread("C:/Users/59834/Desktop/image/a1.png");
Mat right = imread("C:/Users/59834/Desktop/image/a2.png");
imshow("left",left);
imshow("right",right);
//创建SURF对象
//create 函数参数 海森矩阵阀值
Ptr surf;
surf = SURF::create(800);
//暴力匹配器
BFMatcher matcher;
//特征点容器
vectorkey1,key2;
Mat c,d;
//1、选择特征点
surf->detectAndCompute(left,Mat(),key2,d);
surf->detectAndCompute(right,Mat(),key1,c);
//特征点对比,保存
vector matches;
matcher.match(d,c,matches);
//排序从小到大
sort(matches.begin(),matches.end());
//2、保存最优的特征点对象
vectorgood_matches;
int ptrpoint = std::min(50,(int)(matches.size()*0.15));
for (int i = 0;i < ptrpoint;i++)
{
good_matches.push_back(matches[i]);
}
//2-1、画线 最优的特征点对象连线
Mat outimg;
drawMatches(left,key2,right,key1,good_matches,outimg,
Scalar::all(-1),Scalar::all(-1),
vector(),DrawMatchesFlags::NOT_DRAW_SINGLE_POINTS);
imshow("outimg",outimg);
//3、特征点匹配
vectorimagepoint1,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);
}
//4、透视转换 图形融合
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_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);
waitKey(0);