% 自动拼接图片
% 输入: 方法:'h','v','m'代表水平、垂直、掩模拼接
% img1:(H*W*3)的图1。
% img2:(H*W*3)的图2。
% fun:'h','v','m'
%
%
% 输出:
% out:img1和img2线性融合的拼接结果。
% M:变换投影矩阵
function [out,M]=auto_cast(img1,img2,fun)
% 角点检测
ww=3;
gray_img1=medfilt2(rgb2gray(img1),[ww ww]);
gray_img2=medfilt2(rgb2gray(img2),[ww ww]);
imageSize=size(gray_img1);
alpha=4;%卷积核大小
p1=detectSURFFeatures(gray_img1, 'NumOctaves',alpha);
p2=detectSURFFeatures(gray_img2,'NumOctaves',alpha);
% 生成描述子
[img1Features, p1] = extractFeatures(gray_img1, p1);
[img2Features, p2] = extractFeatures(gray_img2, p2);
% 匹配角点
boxPairs = matchFeatures(img1Features, img2Features);
matchedimg1Points = p1(boxPairs(:, 1));
matchedimg2Points = p2(boxPairs(:, 2));
% RANSAC
[tform, inlierimg2Points, inlierimg1Points] = estimateGeometricTransform(matchedimg2Points, matchedimg1Points, 'projective');%射影变换,tfrom映射点对1内点到点对2内点
% 该函数使用随机样本一致性(RANSAC)算法的变体MSAC算法实现,去除误匹配点
%%%%%%%%%%%%%%%%自己写的homo矩阵方法
% H1=calc_homography(inlierimg2Points.Location,inlierimg1Points.Location,0);
% tform1=tform;
% tform1.T=H1;
% figure;
% showMatchedFeatures(img1, img2, inlierimg1Points,inlierimg2Points, 'montage');
% title('SURF描述符 RANSAC方法 匹配结果')
% 拼接
[xlim, ylim] = outputLimits(tform, [1 imageSize(2)], [1 imageSize(1)]);
% 找到输出空间限制的最大最小值
xMin = min([1; xlim(:)]);
xMax = max([imageSize(2); xlim(:)]);
yMin = min([1; ylim(:)]);
yMax = max([imageSize(1); ylim(:)]);
% 全景图的宽高
width = round(xMax - xMin);
height = round(yMax - yMin);
% 创建2D空间参考对象定义全景图尺寸
xLimits = [xMin xMax];
yLimits = [yMin yMax];
panoramaView = imref2d([height width ], xLimits, yLimits);
% 变换图片到全景图
gray_img1 = imwarp(img1,projective2d(eye(3)), 'OutputView', panoramaView);
gray_img2 = imwarp(img2, tform, 'OutputView', panoramaView);
% 输出部分
M=tform.T;%矩阵
switch fun
case 'h'
[~,out,~]=linear_blend_horizontal(gray_img1,gray_img2);
case 'v'
[~,out,~]=linear_blend_vertical(gray_img1,gray_img2);
case 'm'
mask= masker(gray_img1,gray_img2);
gray_img1=gray_img1.*(1-0.5*mask);
gray_img2=gray_img2.*(1-0.5*mask);
out=imadd(gray_img1,gray_img2);
otherwise
out=imadd(gray_img1,gray_img2);
end
% figure;
% imshow(imadd(gray_img1,gray_img2))
% figure;
% imshow(outh)
% figure;
% imshow(outv)
end
MATLAB自动图像拼接
需积分: 17 138 浏览量
2022-09-23
16:47:57
上传
评论 1
收藏 4KB ZIP 举报
久保若阳
- 粉丝: 0
- 资源: 2
最新资源
- python-leetcode面试题解之第157题用Read4读取N个字符-题解.zip
- python-leetcode面试题解之第156题上下翻转二叉树-题解.zip
- python-leetcode面试题解之第155题最小栈-题解.zip
- python-leetcode面试题解之第153题寻找旋转排序数组中的最小值-题解.zip
- python-leetcode面试题解之第152题乘积最大子数组-题解.zip
- python-leetcode面试题解之第151题反转字符串中的单词-题解.zip
- python-leetcode面试题解之第150题逆波兰表达式求值-题解.zip
- python-leetcode面试题解之第149题直线上最多的点数-题解.zip
- python-leetcode面试题解之第148题排序链表-题解.zip
- python-leetcode面试题解之第147题对链表进行插入排序-题解.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
评论0