Assignment Babar Ali & Mohsin
Assignment Babar Ali & Mohsin
Submitted To:
Dr. Muhammad Haroon Yousaf
Submitted by:
Babar Ali 17F-MS-EE-15
Syed Mohsin 17F-MS-EE-11
Task 1:
Both of the group members capture their own face snapshot in a
symmetrical pose. In case of individual/veil, you can take
snapshot of any friends or sibling.
Apply image registration algorithm to align both of the images.
Generate Gaussian pyramid of both snapshots and display in the
pattern as in lecture slide # 22.
Generate Laplacian pyramid of both snapshots and display in the
pattern as in lecture slide # 22.
(a).Apply image blending concept discussed in the lecture to blend
your faces w.r.t. vertical and horizontal half i.e. like apple and
orange case.
(b).Apply image blending concept discussed in the lecture and
swap one eye of both of your
faces.
Present your results using GUI.
function imgo=blend_images(imga,imgb,mask,level)
maska= repmat((1.*mask),[1 1 3]);
limga = generate_Pyramid(imga,'lap',level); % the Laplacian pyramid
limgb = generate_Pyramid(imgb,'lap',level);
maskb = 1-maska;
blurh = fspecial('gauss',30,15); % feather the border
maska = imfilter(maska,blurh,'replicate');
maskb = imfilter(maskb,blurh,'replicate');
limgo = cell(1,level); % the blended pyramid
for p = 1:level
[Mp Np ~] = size(limga{p});
maskap = imresize(maska,[Mp Np]);
maskbp = imresize(maskb,[Mp Np]);
limgo{p} = limga{p}.*maskap + limgb{p}.*maskbp;
end
for p = length(limgo)-1:-1:1
limgo{p} = limgo{p}+pyramid_expand(limgo{p+1});
end
imgo = limgo{1};
end
Generate_pyramid.m
Pyramid_expand.m
Simulation Results:
Output results are shown in the form of figures
Task 2:
Read images Search_Image.jpg and Template_Image.jpg.
Write code for template matching scheme to find out the
template_Image.png in the Search_Image.png.
III. Apply template matching algorithms using image pyramids
across 4-levels and propose a computational efficient way of
finding the template image in the search image.
IV. Compare and present your results and computational cost in
case II and III.
Solution:
Proposed template matching schemes are:
Template Matching by using Correlation in frequency domain
Template matching by using Sum of Squared Difference
Correlation in time domain takes a lot of time for finding out the location
of a template in an image. Matlab has some built in function for frequency
domain processing such as FFT and IFFT which are very fast. So processing
image in frequency domain for template matching gives fast results and
the results are also accurate.
MATLAB Code:
Task Template Matching
function Task2_template_matching
clear all;
close all;
clc;
level=4;
I = rgb2gray(imread('Search_Image.png'));
I_pyr = generate_Pyramid_uint8((I),'gauss',level);
% Use a second similar chip as the template
T = rgb2gray(imread('Template_Image.png'));
T_pyr = generate_Pyramid_uint8((T),'gauss',level);
for i=1:level
% Calculate NCC between Template and Image
tic;
I_NCC{i}=template_matching_by_Corr(T_pyr{i},I);
toc;
I_NCC_img=I_NCC{i};
% Find maximum correspondence location in image
[x_Corr{i} y_Corr{i}]=(find(I_NCC_img==max(I_NCC_img(:))));
x1=x_Corr{i};
y1=y_Corr{i};
function [I_SSD]=template_matching_by_SSD(T,I)
% Convert images to double
T=double(T);
I=double(I);
[I_SSD]=template_matching_gray_SSD(T,I);
end
%%
function [I_SSD]=template_matching_gray_SSD(T,I)
% Calculate correlation output size = input size + padding template
T_size = size(T); I_size = size(I);
outsize = I_size + T_size-1;
% Calculate correlation in frequency domain
FT = fft2(rot90(T,2),outsize(1),outsize(2));
FI = fft2(I,outsize(1),outsize(2));
Icorr = real(ifft2(FI.* FT));
% Calculate Local Quadratic sum of Image and Template
LocalQSumI= local_sum(I.*I,T_size);
QSumT = sum(T(:).^2);
% SSD between template and image
I_SSD=LocalQSumI+QSumT-2*Icorr;
% Normalize to range 0..1
I_SSD=I_SSD-min(I_SSD(:));
I_SSD=1-(I_SSD./max(I_SSD(:)));
% Remove padding
I_SSD=unpadarray(I_SSD,size(I));
end
%%
function B=unpadarray(A,Bsize)
Bstart=ceil((size(A)-Bsize)/2)+1;
Bend=Bstart+Bsize-1;
B=A(Bstart(1):Bend(1),Bstart(2):Bend(2));
end
%%
function local_sum_I= local_sum(I,T_size)
% Add padding to the image
B = padarray(I,T_size);
% Calculate for each pixel the sum of the region around it,
% with the region the size of the template.
% 2D localsum
s = cumsum(B,1);
c = s(1+T_size(1):end-1,:)-s(1:end-T_size(1)-1,:);
s = cumsum(c,2);
local_sum_I= s(:,1+T_size(2):end-1)-s(:,1:end-T_size(2)-1);
end
Template_matching_by_Corr.m :
function [I_NCC]=template_matching_by_Corr(T,I)
%%
% Convert images to double
T=double(T);
I=double(I);
[I_NCC]=template_matching_gray_Corr(T,I);
end
%%
function [I_NCC]=template_matching_gray_Corr(T,I)
% Calculate correlation output size = input size + padding template
T_size = size(T); I_size = size(I);
outsize = I_size + T_size-1;
% Calculate correlation in frequency domain
FT = fft2(rot90(T,2),outsize(1),outsize(2));
FI = fft2(I,outsize(1),outsize(2));
Icorr = real(ifft2(FI.* FT));
% Normalized cross correlation STD
LocalSumI= local_sum(I,T_size);
% Calculate Local Quadratic sum of Image and Template
LocalQSumI= local_sum(I.*I,T_size);
% Standard deviation
stdI=sqrt(max(LocalQSumI-(LocalSumI.^2)/numel(T),0) );
stdT=sqrt(numel(T)-1)*std(T(:));
% Mean compensation
meanIT=LocalSumI*sum(T(:))/numel(T);
I_NCC= 0.5+(Icorr-meanIT)./ (2*stdT*max(stdI,stdT/1e5));
% Remove padding
I_NCC=unpadarray(I_NCC,size(I));
end
%%
function B=unpadarray(A,Bsize)
Bstart=ceil((size(A)-Bsize)/2)+1;
Bend=Bstart+Bsize-1;
B=A(Bstart(1):Bend(1),Bstart(2):Bend(2));
end
%%
function local_sum_I= local_sum(I,T_size)
% Add padding to the image
B = padarray(I,T_size);
% Calculate for each pixel the sum of the region around it,
% with the region the size of the template.
% 2D localsum
s = cumsum(B,1);
c = s(1+T_size(1):end-1,:)-s(1:end-T_size(1)-1,:);
s = cumsum(c,2);
local_sum_I= s(:,1+T_size(2):end-1)-s(:,1:end-T_size(2)-1);
end
Simulation Results:
Elapsed time is 1.762435 seconds.
Elapsed time is 0.134010 seconds.
Elapsed time is 0.098576 seconds.
Elapsed time is 0.060992 seconds.
Elapsed time is 0.101479 seconds.
Elapsed time is 0.077654 seconds.
Elapsed time is 0.184681 seconds.
Elapsed time is 0.082164 seconds.
It shows, as we go deeper into sub-sampled level of search image and
template image, results come quickly as there are no. of pixels to process
for template matching. Correlation and SSD gives different location of the
matching template for different sub-sampled levels of template image.
While processing search image at level-1 and template image at level-3,
both SSD and Correlation shows same results for matching template. So
the template is down-sampled to a level-3 and is matched in the search
image
Location of the center of the template in the search image is:
Fig. 2.1: Input Image &Template Image
[r1 c1]=find(Idir(n,o)==90)
zero=[r1 c1]
if isempty(zero)
sum(3)=0;
else
for p=1:numel(zero,1)
sum(3)=sum(3)+Imag(zero(p,:));
end
end
[r1 c1]=find(Idir(n,o)==135)
zero=[r1 c1]
if isempty(zero)
sum(4)=0;
else
for p=1:numel(zero,1)
sum(4)=sum(4)+Imag(zero(p,:));
end
end
[r1 c1]=find(Idir(n,o)==180)
zero=[r1 c1]
if isempty(zero)
sum(5)=0;
else
for p=1:numel(zero,1)
sum(5)=sum(5)+Imag(zero(p,:));
end
end
[r1 c1]=find(Idir(n,o)==225)
zero=[r1 c1]
if isempty(zero)
sum(6)=0;
else
for p=1:numel(r1)
sum(6)=sum(6)+Imag(zero(p,:));
end
end
[r1 c1]=find(Idir(n,o)==270)
zero=[r1 c1]
if isempty(zero)
sum(7)=0;
else
for p=1:numel(zero,1)
sum(7)=sum(7)+Imag(zero(p,:));
end
end
[r1 c1]=find(Idir(n,o)==315)
zero=[r1 c1]
if isempty(zero)
sum(8)=0;
else
for p=1:numel(zero,1)
sum(8)=sum(8)+Imag(zero(p,:)); %sum contain 8 values, no of
cells=16, 16x8=128 feature vector for 1 keypoint
end
end
f(1+n*(0-o))=sum;
end
end
sum=[];
feature=[feature;f]
end
end
Simulation Results:
Fig.0-1
Fig.1-3
Fig.3-5
Fig.6-8
Fig.8-10