0% found this document useful (0 votes)
20 views10 pages

Lecture 11+12

The document outlines a process for creating a panorama from multiple images by reading and extracting features, matching them, and estimating geometric transforms. It includes detailed steps for loading images, detecting features, matching them between consecutive images, and finally stitching them together into a single panoramic image. The code provided illustrates the implementation of these steps using image processing techniques.

Uploaded by

22151017
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views10 pages

Lecture 11+12

The document outlines a process for creating a panorama from multiple images by reading and extracting features, matching them, and estimating geometric transforms. It includes detailed steps for loading images, detecting features, matching them between consecutive images, and finally stitching them together into a single panoramic image. The code provided illustrates the implementation of these steps using image processing techniques.

Uploaded by

22151017
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

Lecture 11+12:

Read 2 images
Extract features and matching
Estimate geometric transform
Create panorama

Matched Points
Read more than 2 pictures
Matched Points Between Image 1 and Image 2
Matched Points Between Image 2 and Image 3

Matched Points Between Image 3 and Image 4

Matched Points Between Image 4 and Image 5


Matched Points Between Image 5 and Image 6

Creates larger panorama

Code for 2nd part and 1st part


%% Step 1 - Load Images
clear all;close all;clc
tic
buildingDir = fullfile("New/");
buildingScene = imageDatastore(buildingDir);

% Display images to be stitched


montage(buildingScene.Files)
%% Step 2 - Register Image Pairs
% Read the first image from the image set.
I = readimage(buildingScene, 1);

% Initialize features for I(1)


grayImage = rgb2gray(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

% Compute the output limits for each transform


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

avgXLim = mean(xlim, 2);

[~, idx] = sort(avgXLim);

centerIdx = floor((numel(tforms)+1)/2);

centerImageIdx = idx(centerIdx);

%%
% Finally, apply the center image's inverse transform to all
the others.

Tinv = invert(tforms(centerImageIdx));

for i = 1:numel(tforms)
tforms(i).T = tforms(i).T * Tinv.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

maxImageSize = max(imageSize);

% Find the minimum and maximum output limits


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

% Use |imwarp| to map images into the panorama and use


% |vision.AlphaBlender| to overlay the images together.
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);

% 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)
toc

%% step 1: Load 6 Images (matching point)


imageFiles = {'001.jpg', '002.jpg', '003.jpg', '004.jpg',
'005.jpg', '006.jpg'}; % Replace with your file names
numImages = numel(imageFiles);

% Read all images and convert to grayscale


images = cell(1, numImages);
grayImages = cell(1, numImages);
for i = 1:numImages
images{i} = imread(imageFiles{i});
grayImages{i} = rgb2gray(images{i});
end

% Step 2: Detect and Extract Features


features = cell(1, numImages);
validPoints = cell(1, numImages);

for i = 1:numImages
points = detectSURFFeatures(grayImages{i}); % Detect
SURF features
[features{i}, validPoints{i}] =
extractFeatures(grayImages{i}, points); % Extract features
end

% Step 3: Match Features Between Consecutive Pairs


for i = 1:numImages-1
% Match features between image i and image i+1
indexPairs = matchFeatures(features{i}, features{i+1});

% Retrieve matched points


matchedPoints1 = validPoints{i}(indexPairs(:,
1)).Location;
matchedPoints2 = validPoints{i+1}(indexPairs(:,
2)).Location;

% Step 4: Visualize Matched Points


figure;
showMatchedFeatures(images{i}, images{i+1},
matchedPoints1, matchedPoints2, 'montage');
title(['Matched Points Between Image ', num2str(i), '
and Image ', num2str(i+1)]);
end

You might also like