【Matlab】基于特征点的全景图像拼接

来源:互联网 发布:淘宝海外版apk 编辑:程序博客网 时间:2024/04/28 00:53

http://cn.mathworks.com/examples/matlab-computer-vision/mw/vision_product-FeatureBasedPanoramicImageStitchingExample-feature-based-panoramic-image-stitching

  • 概述
  • 步骤一 加载图片
  • 步骤二 图像配准
  • 步骤三 初始化全景图
  • 步骤四 创建全景图
  • 总结
  • 相关论文

概述

在机器视觉应用领域里特征检测和匹配是一个很重要的算法,比如图像配准、跟踪和目标检测。这个例子里,我们用基于特征的方法完成图像拼接。处理的方法是先用图像配准特征点。不同于单图像对配准,这里是多图像对的配准完成图像拼接。

步骤一 加载图片

% 以图像集的方法加载图片buildingDir = fullfile(toolboxdir('vision'), 'visiondata', 'building');buildingScene = imageSet(buildingDir);% 显示要拼接的所有图片montage(buildingScene.ImageLocation)

步骤二 图像配准

使用下面的步骤来进行图像对配准操作。

  1. 在I(n)和I(n-1)之间检测和匹配特征点
  2. 估算从I(n)映射到I(n-1)上的几何变换T(n)
  3. 计算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));endfigureimshow(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.

0 0