图像拼接:matlab自带的拼接示例程序

图像拼接:matlab自带的拼接示例程序图像拼接

matlab自带的拼接示例函数,可以拼接多张图像,效果一般,用的surf特征

% step1 Load images.
buildingDir =('F:\stitching\imagess\or\lt');%读取图片,把要拼接的图片放到这个文件夹中
buildingScene = imageDatastore(buildingDir);
% Display images to be stitched.
% montage(buildingScene.Files)%显示输入图片

%Step 2 - Register Image Pairs
I = readimage(buildingScene,1);
% Initialize features for I(1)
grayImage = rgb2gray(I);%grayImage = im2gray(I);
points = detectSURFFeatures(grayImage);
[features, points] = extractFeatures(grayImage,points);
numImages = numel(buildingScene.Files);
tforms(numImages) = projective2d(eye(3));
% Initialize variable to hold image sizes.
imageSize = zeros(numImages,2);

% Iterate over remaining image pairs
for n = 2:numImages    
    % Store points and features for I(n-1).
    pointsPrevious = points;
    featuresPrevious = features;
    % Read I(n).
    I = readimage(buildingScene, n);    
    % Convert image to grayscale.
    grayImage = rgb2gray(I);       
    % Save image size.
    imageSize(n,:) = size(grayImage);    
    % Detect and extract SURF features for I(n).
    points = detectSURFFeatures(grayImage);    
    [features, points] = extractFeatures(grayImage, points);
  
    % Find correspondences between I(n) and I(n-1).
    indexPairs = matchFeatures(features, featuresPrevious, 'Unique', true);
       
    matchedPoints = points(indexPairs(:,1), :);
    matchedPointsPrev = pointsPrevious(indexPairs(:,2), :);        
    
    % Estimate the transformation between I(n) and I(n-1).
    [tforms(n),~,~] = estimateGeometricTransform(matchedPoints, matchedPointsPrev,...
        'projective', 'Confidence', 99.9, 'MaxNumTrials', 2000);   
      
    % Compute T(n) * T(n-1) * ... * T(1)
    tforms(n).T = tforms(n).T * tforms(n-1).T; 
end

%Step 3 - Initialize the Panorama对每个投影变化找到输出的空间坐标限制值。
for i = 1:numel(tforms)           
    [xlim(i,:), ylim(i,:)] = outputLimits(tforms(i), [1 imageSize(i,2)], [1 imageSize(i,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 = tforms(i).T * Tinv.T;
end

for i = 1:numel(tforms)           
    [xlim(i,:), ylim(i,:)] = outputLimits(tforms(i), [1 imageSize(i,2)], [1 imageSize(i,1)]);
end

maxImageSize = max(imageSize);
% Find the minimum and maximum output limits. 
%创建一个空的全景图用来存放所有图像。
%用outputLimits方法计算所有变换中最小和最大输出限制。
%这个值用来计算全景图的大小
xMin = min([1; xlim(:)]);
xMax = max([maxImageSize(2); xlim(:)]);

yMin = min([1; ylim(:)]);
yMax = max([maxImageSize(1); ylim(:)]);

% Width and height of panorama.
width  = round(xMax - xMin);
height = round(yMax - yMin);

% Initialize the "empty" panorama.生成空数据的全景图
panorama = zeros([height width 3], 'like', I);

%Step 4 - Create the Panorama
%用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:numImages
    
    I = readimage(buildingScene, i);   
   
    % Transform I into the panorama.
    warpedImage = imwarp(I, tforms(i), 'OutputView', panoramaView);
%     figure;imshow(warpedImage);
                  
    % Generate a binary mask.    
    mask = imwarp(true(size(I,1),size(I,2)), tforms(i), 'OutputView', panoramaView);
    
    % Overlay the warpedImage onto the panorama.
    panorama = step(blender, panorama, warpedImage, mask);
end

figure
imshow(panorama);

结果图像:两张拼接、三张拼接

图像拼接:matlab自带的拼接示例程序

 图像拼接:matlab自带的拼接示例程序

今天的文章图像拼接:matlab自带的拼接示例程序分享到此就结束了,感谢您的阅读,如果确实帮到您,您可以动动手指转发给其他人。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/30020.html

(0)
编程小号编程小号

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注