% Stitch two images together with a multiresolution spline, % using Laplacian pyramids. clear all close all n = 8; % Number of levels in pyramid % First image (image A) IA = imread('apple_real.jpg'); IA = rgb2gray(IA); % Create a Laplacian pyramid (a cell array of images) G{1} = double(IA); % Lowest level of Gaussian pyramid for i=2:n G{i} = imresize(G{i-1}, 0.5); % apply lowpass, then scale by 0.5 Gs = imresize(G{i}, 2,'bicubic'); % scale up by factor of 2 LA{i-1} = G{i-1} - Gs; % Laplacian pyramid end LA{n} = G{n}; % Highest level is just the coarsest resolution image for i=1:n figure, imshow(LA{i}, []); end pause % Second image (image B) IB = imread('apple_logo.jpg'); IB = rgb2gray(IB); % Create a Laplacian pyramid (a cell array of images) G{1} = double(IB); % Lowest level of Gaussian pyramid for i=2:n G{i} = imresize(G{i-1}, 0.5); % apply lowpass, then scale by 0.5 Gs = imresize(G{i}, 2,'bicubic'); % scale up by factor of 2 LB{i-1} = G{i-1} - Gs; % Laplacian pyramid end LB{n} = G{n}; % Highest level is just the coarsest resolution image for i=1:n figure, imshow(LB{i}, []); end pause % Merge pyramids. At each level, use the left half from image A, and the % right half from image B. For the center column, average them. for i=1:n A = LA{i}; B = LB{i}; w = size(A,2); % Image width at this level w2 = round(w/2); % Center column LS{i}(:,1:w2-1) = A(:,1:w2-1); % Use left half from image A LS{i}(:,w2+1:w) = B(:,w2+1:w); % Use right half from image B LS{i}(:,w2) = (A(:,w2) + B(:,w2))/2; % Average the center columns end % Reconstruct image from Laplacian pyramid Ireconstructed = LS{1}; for i=2:n I = imresize(LS{i}, 2^(i-1), 'bicubic'); Ireconstructed = Ireconstructed + I; end figure, imshow(Ireconstructed,[]);