【Matlab】基于特征点的全景图像拼接
2016-07-28 15:37
1261 查看
http://cn.mathworks.com/examples/matlab-computer-vision/mw/vision_product-FeatureBasedPanoramicImageStitchingExample-feature-based-panoramic-image-stitching
概述
步骤一 加载图片
步骤二 图像配准
步骤三 初始化全景图
步骤四 创建全景图
总结
相关论文
在I(n)和I(n-1)之间检测和匹配特征点
估算从I(n)映射到I(n-1)上的几何变换T(n)
计算I(n)在全景图里的变换T(1)*…*T(n-1)*T(n).
% 从图片集中读取第一幅图像
I = read(buildingScene, 1);
% 将图像转为灰度图,再提取I(1)的特征点,用的是surf算法。
grayImage = rgb2gray(I);
points = detectSURFFeatures(grayImage);
[features, points] = extractFeatures(grayImage, points);
% 初始化所有变换的恒等矩阵。
tforms(buildingScene.Count) = projective2d(eye(3));
% Iterate over remaining image pairs
for n = 2:buildingScene.Count
% Store points and features for I(n-1).
% 存储前一图像的特征点坐标和值。
pointsPrevious = points;
featuresPrevious = features;
% Read I(n).
% 读取第n张图片。
I = read(buildingScene, n);
% Detect and extract SURF features for I(n).
%检测和提取surf特征值。
grayImage = rgb2gray(I);
points = detectSURFFeatures(grayImage);
[features, points] = extractFeatures(grayImage, points);
% 匹配I(n)和I(n-1)之间对应的特征点
indexPairs = matchFeatures(features, featuresPrevious, ‘Unique’, true);
matchedPoints = points(indexPairs(:,1), :);
matchedPointsPrev = pointsPrevious(indexPairs(:,2), :);
% 用MSAC算法计算几何变化。
tforms(n) = estimateGeometricTransform(matchedPoints, matchedPointsPrev,…
‘projective’, ‘Confidence’, 99.9, ‘MaxNumTrials’, 2000);
% 计算T(1) * … * T(n-1) * T(n)
tforms(n).T = tforms(n-1).T * tforms(n).T;
end
这里,所有tforms的变换都是相对于第一幅图像的。主要是为了方便图像配准处理代码,这样就可以对所有图像连续处理。但是,用第一张图像做为全景图配准的起点,不能得到最佳的效果,原因是它会把全景图所有的图像都发生畸变。将变换式用中间的场景可以创建比较好的全景图,畸变也最小。
先用
接着,计算每个变换X极限的平均值,找到中间的图像。只用X方向上的极限是由于场景为水平方向上的。如果有其他的图像,X和Y方向上的极限都应当用来查找中心图像。
最后,将中心图像的反变换应用到所有的图像变换中。
用
概述
步骤一 加载图片
步骤二 图像配准
步骤三 初始化全景图
步骤四 创建全景图
总结
相关论文
概述
在机器视觉应用领域里特征检测和匹配是一个很重要的算法,比如图像配准、跟踪和目标检测。这个例子里,我们用基于特征的方法完成图像拼接。处理的方法是先用图像配准特征点。不同于单图像对配准,这里是多图像对的配准完成图像拼接。步骤一 加载图片
% 以图像集的方法加载图片 buildingDir = fullfile(toolboxdir('vision'), 'visiondata', 'building'); buildingScene = imageSet(buildingDir); % 显示要拼接的所有图片 montage(buildingScene.ImageLocation)
步骤二 图像配准
使用下面的步骤来进行图像对配准操作。在I(n)和I(n-1)之间检测和匹配特征点
估算从I(n)映射到I(n-1)上的几何变换T(n)
计算I(n)在全景图里的变换T(1)*…*T(n-1)*T(n).
% 从图片集中读取第一幅图像
I = read(buildingScene, 1);
% 将图像转为灰度图,再提取I(1)的特征点,用的是surf算法。
grayImage = rgb2gray(I);
points = detectSURFFeatures(grayImage);
[features, points] = extractFeatures(grayImage, points);
% 初始化所有变换的恒等矩阵。
tforms(buildingScene.Count) = projective2d(eye(3));
% Iterate over remaining image pairs
for n = 2:buildingScene.Count
% Store points and features for I(n-1).
% 存储前一图像的特征点坐标和值。
pointsPrevious = points;
featuresPrevious = features;
% Read I(n).
% 读取第n张图片。
I = read(buildingScene, n);
% Detect and extract SURF features for I(n).
%检测和提取surf特征值。
grayImage = rgb2gray(I);
points = detectSURFFeatures(grayImage);
[features, points] = extractFeatures(grayImage, points);
% 匹配I(n)和I(n-1)之间对应的特征点
indexPairs = matchFeatures(features, featuresPrevious, ‘Unique’, true);
matchedPoints = points(indexPairs(:,1), :);
matchedPointsPrev = pointsPrevious(indexPairs(:,2), :);
% 用MSAC算法计算几何变化。
tforms(n) = estimateGeometricTransform(matchedPoints, matchedPointsPrev,…
‘projective’, ‘Confidence’, 99.9, ‘MaxNumTrials’, 2000);
% 计算T(1) * … * T(n-1) * T(n)
tforms(n).T = tforms(n-1).T * tforms(n).T;
end
这里,所有tforms的变换都是相对于第一幅图像的。主要是为了方便图像配准处理代码,这样就可以对所有图像连续处理。但是,用第一张图像做为全景图配准的起点,不能得到最佳的效果,原因是它会把全景图所有的图像都发生畸变。将变换式用中间的场景可以创建比较好的全景图,畸变也最小。
先用
projective2d outputLimits方法查找每个变换输出的极限。输出极限再用来自动查找中间场景图像的轮廓。
imageSize = size(I); % 所有的图像尺寸都是一样的 % 对每个投影变化找到输出的空间坐标限制值。 for i = 1:numel(tforms) [xlim(i,:), ylim(i,:)] = outputLimits(tforms(i), [1 imageSize(2)], [1 imageSize(1)]); end
接着,计算每个变换X极限的平均值,找到中间的图像。只用X方向上的极限是由于场景为水平方向上的。如果有其他的图像,X和Y方向上的极限都应当用来查找中心图像。
avgXLim = mean(xlim, 2); [~, idx] = sort(avgXLim); centerIdx = floor((numel(tforms)+1)/2); centerImageIdx = idx(centerIdx);
最后,将中心图像的反变换应用到所有的图像变换中。
Tinv = invert(tforms(centerImageIdx)); for i = 1:numel(tforms) tforms(i).T = Tinv.T * tforms(i).T; end
步骤三 初始化全景图
接下来,创建一个空的全景图用来存放所有图像。用
outputLimits方法计算所有变换中最小和最大输出限制。这个值用来计算全景图的大小。
for i = 1:numel(tforms) [xlim(i,:), ylim(i,:)] = outputLimits(tforms(i), [1 imageSize(2)], [1 imageSize(1)]); end % 找到输出空间限制的最大最小值 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); % 生成空数据的全景图 panorama = zeros([height width 3], 'like', I);
步骤四 创建全景图
用imwarp将图像映射到全景图中,再用
vision.AlphaBlender将图像重叠起来。
blender = vision.AlphaBlender('Operation', 'Binary mask', ... 'MaskSource', 'Input port'); % Create a 2-D spatial reference object defining the size of the panorama. xLimits = [xMin xMax]; yLimits = [yMin yMax]; panoramaView = imref2d([height width], xLimits, yLimits); % Create the panorama. for i = 1:buildingScene.Count I = read(buildingScene, i); % Transform I into the panorama. warpedImage = imwarp(I, tforms(i), 'OutputView', panoramaView); % Overlay the warpedImage onto the panorama. panorama = step(blender, panorama, warpedImage, warpedImage(:,:,1)); end figure imshow(panorama)
总结
这个例子展示了如何使用图像配准方法创建全景图。附录包含了图像融合和对齐的全景图拼接改进方法。相关论文
[1] Matthew Brown and David G. Lowe. 2007. Automatic Panoramic Image Stitching using Invariant Features. Int. J. Comput. Vision 74, 1 (August 2007), 59-73.相关文章推荐
- 解析在main函数之前调用函数以及对设计的作用详解
- 详解Matlab中 sort 函数用法
- java和matlab画多边形闭合折线图示例讲解
- C#调用Matlab生成的dll方法的详细说明
- 简述Matlab中size()函数的用法
- 从java中调用matlab详细介绍
- 稀疏自动编码器 (Sparse Autoencoder)
- 详解Matlab中 sort 函数用法
- 简述Matlab中size()函数的用法
- VC++与Matlab混合编程的快速实现
- Matlab 矩阵运算
- matlab与opencv部分函数的对照
- matlab神经网络工具箱创建神经网络
- Matlab
- MATLAB 入门教程
- matlab函数_连通区域
- MATLAB中函数模式和命令模式的区别
- MATLAB 添加自定义的模块到simulink库浏览器
- Export Figures for LaTeX Writing