You are on page 1of 31

COMPUTER VISION

Assignment#1

Submitted to
Dr. Haroon Yousaf

Submitted by
Behzad Mehmood
17F-MS-EE-09
Izzat Fatima
17F-MS-EE-16

Submission Date: 11th April, 2018

Department of Computer Engineering,


Faculty of Telecommunication and Information Engineering,
University of Engineering and Technology, Taxila
TASK 3:
MATLAB Code:

function main = hd();


clear all;
close all;
clc;

a = imread('E:\Image 1.jpg');
a = rgb2gray(a);
[kk1,m1,n1,d1]=hrisift(a);
[y z]=size(kk1);
for i=1:y
XL(i,1)=kk1(i,1);
XL(i,2)=kk1(i,2);
end

x1=XL(:,1);
y1=XL(:,2);
[v11 p11]=size(d1);

b = imread('E:\Image 2.jpg');
b = rgb2gray(b);
[kk2,m2,n2,d2]=hrisift(b);
[u v]=size(kk2);
for i=1:u
XL1(i,1)=kk2(i,1);
XL1(i,2)=kk2(i,2);
end
x2=XL1(:,1);
y2=XL1(:,2);
[v12 p12]=size(d2);

c = imread('E:\Image 3.jpg');
c = rgb2gray(c);
[kk3,m3,n3,d3]=hrisift(c);
[u1 l1]=size(kk3);
for i=1:u1
XL2(i,1)=kk2(i,1);
XL2(i,2)=kk2(i,2);
end
x3=XL2(:,1);
y3=XL2(:,2);
[v13 p13]=size(d3);

o=input('Enter Part no of the task: 1 or 2 or 3')


if(o==1)
figure
imshow(a);
title('Interest points on image 1')
hold on;
plot(n1,m1,'bo');
T1=table(x1,y1)
figure
imshow(b);
title('Interest points on image 2')
hold on;
plot(n2,m2,'bo');
T2=table(x2,y2)
figure
imshow(c);
title('Interest points on image 3')
hold on;
plot(n3,m3,'bo');
T3=table(x3,y3)
end
if(o==2)
fprintf('\n toatl numer of key points and feature vectors extracted from
image 1 are : \n')
length(kk1)
fprintf('\n We are showing only 5th feature vector \n')
d1(6,:)
fprintf('\n toatl numer of key points and feature vectors extracted from
image 2 are : \n')
length(kk2)
fprintf('\n We are showing only 5th feature vector \n')
d2(5,:)
fprintf('\n toatl numer of key points and feature vectors extracted from
image 3 are : \n')
length(kk3)
fprintf('\n We are showing only 5th feature vector \n')
d3(5,:)
end

if(o==3)

figure;
imshow([a b],[]);
title('Interest points on input images 1 and 2')
hold on;
plot(y1,x1,'go');
hold on
plot(size(a,2)+y2,x2,'ro');
hold on
c6=0;
for i=1:v11
for j=1:v12
h1=d1(i,:);
h2=d2(j,:);
c5=0;
for r1=1:128
f1=h1(1,r1)-h2(1,r1);
f1=abs(f1);
if(f1<0.07&&f1>0)
c5=c5+1;
end
end
if(c5==128)
% display(i,'The Feature vector in 1st image')
% display('is equal to the')
% display(j,'feature vector in image2')
% display('The point in Image1 at')
kk1(i,:);
% display('matches to point in image at')
kk2(j,:);
plot([kk1(i,2) size(a,2)+kk2(j,2)],[kk1(i,1) kk2(j,1)]);
c6=c6+1;
end
end
end
display(c6,'Total points matched in 1st and 2nd image are')

figure;
imshow([a c],[]);
title('Interest points on input images 1 and 3')
hold on;
plot(y1,x1,'go');
hold on
plot(size(a,2)+y3,x3,'ro');
hold on
c7=0;
for i=1:v11
for j=1:v13
h1=d1(i,:);
h2=d3(j,:);
c5=0;
for r1=1:128
f1=h1(1,r1)-h2(1,r1);
f1=abs(f1);
if(f1<0.07&&f1>0)
c5=c5+1;
end
end
if(c5==128)
% display(i,'The Feature vector in 1st image')
% display('is equal to the')
% display(j,'feature vector in image3')
% display('The point in Image1 at')
kk1(i,:);
% display('matches to point in image3 at')
kk3(j,:);
plot([kk1(i,2) size(a,2)+kk3(j,2)],[kk1(i,1) kk3(j,1)]);
c7=c7+1;
end
end
end

display(c7,'Total points matched in 1st and 3rd image are')


figure;
imshow([b c],[]);
title('Interest points on input images 2 and 3')
hold on;
plot(y2,x2,'go');
hold on
plot(size(b,2)+y3,x3,'ro');
hold on
c8=0;
for i=1:v12
for j=1:v13
h1=d2(i,:);
h2=d3(j,:);
c5=0;
for r1=1:128
f1=h1(1,r1)-h2(1,r1);
f1=abs(f1);
if(f1<0.07&&f1>0)
c5=c5+1;
end
end
if(c5==128)
% display(i,'The Feature vector in 2nd image')
% display('is equal to the')
% display(j,'feature vector in image3')
% display('The point in Image2 at')
kk2(i,:);
% display('matches to point in image3 at')
kk3(j,:);
plot([kk2(i,2) size(b,2)+kk3(j,2)],[kk2(i,1) kk3(j,1)]);
c8=c8+1;
end
end
end

display(c8,'Total points matched in 2nd and 3rd image are')

end

end

function [key_p,m1,n1,des] = hrisift(a);

b=im2double(a);

dx = [-1 0 1;-1 0 1;-1 0 1];


Ix = filter2(dx,a);

dy = [1 1 1;0 0 0;-1 -1 -1];


Iy = filter2(dy,a);

Ix2 = Ix.^2;
Iy2 = Iy.^2;
Ixy = Ix.*Iy;

g= fspecial('gaussian',7,2);
Ix2 = filter2(g,Ix2);
Iy2 = filter2(g,Iy2);
Ixy = filter2(g,Ixy);
[rows cols] = size(a);

h = zeros(rows,cols);
R = zeros(rows,cols);

Rmax = 20;
for i = 1:rows
for j = 1:cols
M = [Ix2(i,j) Ixy(i,j);Ixy(i,j) Iy2(i,j)];
B=double(M(:,:));
e=eig(B);
c=e';
E1=c(1,1);
E2=c(1,2);
detM=E1*E2;
traceM=E1+E2;
R(i,j)=detM-0.05*(traceM)^2;
if R(i,j) > Rmax
Rmax = R(i,j);
end;
end;
end;
fp = 0;

for i = 2:rows-1
for j = 2:cols-1
if R(i,j) > 0.1*Rmax && R(i,j) > R(i-1,j-1) && R(i,j) > R(i-1,j) && R(i,j) >
R(i-1,j+1) && R(i,j) > R(i,j-1) && R(i,j) > R(i,j+1) && R(i,j) > R(i+1,j-1)
&& R(i,j) > R(i+1,j) && R(i,j) > R(i+1,j+1)
h(i,j) = 1;

fp = fp+1;
end;
end;
end;
R=R*255;
r=1;
key_p=[]; % matrix to store key points
locations
for i=1:rows
for j=1:cols

if h(i,j)==1
% key_p=[key_p,i,j];
key_p(r,1)=i;
key_p(r,2)=j;
r=r+1;

end
end
end
m1=key_p(:,1);
n1=key_p(:,2);
fp ;

%SIFT DESCRIPTOR STARTS HERE

km=[];
ka=[];

for i=1:rows-1
for j=1:cols-1
mag(i,j)=sqrt(((b(i+1,j)-b(i,j))^2)+((b(i,j+1)-b(i,j))^2));
phase(i,j)=atan2(((b(i+1,j)-b(i,j))),(b(i,j+1)-b(i,j)));
end
end

for k=1:length(key_p)
magv_2=[];
m2=key_p(k,1); % extract key points location
n2=key_p(k,2);

if (m2<=8)||(n2<=8)||(m2>=rows-8)||(n2>=cols-8)
continue;
end
temp_mag_d=mag(m2-7:m2+8,n2-7:n2+8); % form 16*16 neifghbour
around key point
temp_phase_d=phase(m2-7:m2+8,n2-7:n2+8);

store_phase=[];
for i=1:4:13
for j=1:4:13
vijj=temp_mag_d(i:i+3,j:j+3);
vijj_phase=temp_phase_d(i:i+3,j:j+3);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
s=1;
bin_m1=zeros(1,8);
for i1=1:4
for j1=1:4
xx= vijj_phase(i1,j1);
if xx<=(pi/8)&&xx>(15*pi/8)
bin_m1(1,1)= bin_m1(1,1)+vijj(i1,j1);
elseif xx<=(3*pi/8)&&xx>(pi/8)
bin_m1(1,2)= bin_m1(1,2)+vijj(i1,j1);
elseif xx<=(5*pi/8)&&xx>(3*pi/8)
bin_m1(1,3)= bin_m1(1,3)+vijj(i1,j1);
elseif xx<=(7*pi/8)&&xx>(5*pi/8)
bin_m1(1,4)= bin_m1(1,4)+vijj(i1,j1);
elseif xx<=(9*pi/8)&&xx>(7*pi/8)
bin_m1(1,5)= bin_m1(1,5)+vijj(i1,j1);
elseif xx<=(11*pi/8)&&xx>(9*pi/8)
bin_m1(1,6)= bin_m1(1,6)+vijj(i1,j1);
elseif xx<=(13*pi/8)&&xx>(11*pi/8)
bin_m1(1,7)= bin_m1(1,7)+vijj(i1,j1);
elseif xx<=(15*pi/8)&&xx>(13*pi/8)
bin_m1(1,8)= bin_m1(1,8)+vijj(i1,j1);
end
end
end

magv_2=[magv_2 bin_m1]; % magnitude of bins


end

end

des(k,:)=magv_2(:,:)'; % kth un-normalized descriptor


a1=sum(sum(des(k,:)));
des(k,:)=des(k,:)/a1; %Normalize each descriptor to 1
for zz=1:128
if des(k,zz)>=0.2
des(k,zz)=0.2;
end
end
a1=sum(sum(des(k,:)));
des(k,:)=des(k,:)/a1;

end
end

Results:

I)
Figure 1: Interst Points on Image1

Figure 2: Interst Points on Image2


Figure 3: Interst Points on Image3

Table of the coordinates of detected interest points

T1 =

x1 y1

___ __

III)
Figure 4: Matched points on image 1 and 2
Figure 5: Matched points on image 1 and 3
Figure 6: : Matched points on image 2 and 3

TASK 2:

MATLAB Code:
function T2 = TMPM();
clear all;
close all;
clc;

a = rgb2gray(imread('E:\Search_Image.png'));
b = rgb2gray(imread('E:\Template_Image.png'));

figure('Name','Searcch and Template Image','NumberTitle','off');


subplot(1,2,1); imshow(a); title('Search Image');
subplot(1,2,2); imshow(b); title('Template');

Tp= GP(b,'guass',4); %

for i=1:4
t(i)=cputime;
A{i}=TM(Tp{i},a); %calculate fist pyramid time
AI=A{i};
[x{i} y{i}]=(find(AI==max(AI(:))));
x1=x{i};
y1=y{i};
hmi1 = vision.MarkerInserter('Size', 10, 'Fill', true, 'FillColor',
'White','Opacity', 0.75);
loc1= int32([y1 x1]);
j1 = step(hmi1, a, loc1);
figure
imshow(j1); impixelinfo; title(strcat('Marked position at Level-
',num2str(i)));
t(i)=cputime-t(i);
time=t(i)
end
end

function [A]=TM(T,S)
T=double(T);
S=double(S);
[A]=TS(T,S);
end

function [ pmd ] = GP(a, type, level)

pmd = cell(1,level);
pmd{1} = (a);

for i = 2:level
pmd{i} = pr(pmd{i-1});
end
if strcmp(type,'gauss'), return;
end

for i = level-1:-1:1 % adjust the image size


osz = size(pmd{i+1})*2-1;
pmd{i} = pmd{i}(1:osz(1),1:osz(2),:);
end
for i = 1:level-1
pmd{i} = pmd{i}-pe(pmd{i+1});
end

end

function [ c ] = pe(a)
%PYR_EXPAND Image pyramid expansion
% B = PYR_EXPAND( A ) If A is M-by-N, then the size of B
% is (2*M-1)-by-(2*N-1). Support gray or rgb image.
% B will be transformed to double class.

a = (a);
sz = size(a(:,:,1));
osz = sz*2-1;
c = zeros(osz(1),osz(2),size(a,3));

kw = 5; % default kernel width


cw = .375; % kernel centre weight
ker1d = [.25-cw/2 .25 cw .25 .25-cw/2];
kernel = kron(ker1d,ker1d')*4;

% Expansion
for p = 1:size(a,3)
img1 = a(:,:,p);
img1ph = padarray(img1,[0 1],'replicate','both'); % horizontally padded
img1pv = padarray(img1,[1 0],'replicate','both'); % horizontally padded
img00 = imfilter(img1,kernel(1:2:kw,1:2:kw),'replicate','same');
img01 = conv2(img1pv,kernel(1:2:kw,2:2:kw),'valid');
img10 = conv2(img1ph,kernel(2:2:kw,1:2:kw),'valid');
img11 = conv2(img1,kernel(2:2:kw,2:2:kw),'valid');

c(1:2:osz(1),1:2:osz(2),p) = img00;
c(2:2:osz(1),1:2:osz(2),p) = img10;
c(1:2:osz(1),2:2:osz(2),p) = img01;
c(2:2:osz(1),2:2:osz(2),p) = img11;
end
c=uint8(c);
end

function [ c ] = pr(b)
b = (b);
sz = size(b);
c = [];

kernelWidth = 5; % default
cw = .375; % kernel centre weight
kd = [.25-cw/2 .25 cw .25 .25-cw/2];
kernel = kron(kd,kd');

%Reduction
for p = 1:size(b,3) %1 for RGB image if grey loop runs for one time
img1 = b(:,:,p);
imgFiltered = imfilter(img1,kernel,'replicate','same');
c(:,:,p) = imgFiltered(1:2:sz(1),1:2:sz(2)); %get odd rows and coloumns
size half
end
c=uint8(c); %unsiinged int
end

function [B1]=TS(T,I)
TtS = size(T);
IS = size(I);
outsize = IS + TtS-1; %size m+n-1
FT = fft2(rot90(T,2),outsize(1),outsize(2)); %flip 90*2 rows and columns
FI = fft2(I,outsize(1),outsize(2)); %
Icorr = real(ifft2(FI.* FT)); %real part
LSI= ls(I,TtS);
LQ= ls(I.*I,TtS);
%standard deviation og image amd templete
stdI=sqrt(max(LQ-(LSI.^2)/numel(T),0) );
stdT=sqrt(numel(T)-1)*std(T(:));
%calculate mean
meanIT=LSI*sum(T(:))/numel(T);
B1= 0.5+(Icorr-meanIT)./ (2*stdT*max(stdI,stdT/1e5));
B1=unpadarray(B1,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= ls(I,T_size)


B = padarray(I,T_size); %padd zeros
s = cumsum(B,1); %image rows ccmmulative sum
c = s(1+T_size(1):end-1,:)-s(1:end-T_size(1)-1,:); %local sum with
respect to rows
s = cumsum(c,2);
local_sum_I= s(:,1+T_size(2):end-1)-s(:,1:end-T_size(2)-1); %local sum
with respect to coloumns
end

Results:

Figure 7: Search and Template Image


Figure 8: Marked Template position at level1

Figure 9: Marked Template position at level2


Figure 10: Marked Template position at level3

Figure 11: Marked Template position at level4


LEVEL TIME
1 1.4820
2 0.6864
3 0.4992
4 0.4680

Observations:

1) Template is matched at level 3, because the size of the template image was greater than
the matched template in the ‘Search_Image’.
2) Computational time continues to decrease at each level.

TASK 1:

MATLAB Code:

Guassian Pyramid
function varargout = GPGUI(varargin)
% GPGUI MATLAB code for GPGUI.fig
% GPGUI, by itself, creates a new GPGUI or raises the existing
% singleton*.
%
% H = GPGUI returns the handle to a new GPGUI or the handle to
% the existing singleton*.
%
% GPGUI('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in GPGUI.M with the given input arguments.
%
% GPGUI('Property','Value',...) creates a new GPGUI or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before GPGUI_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to GPGUI_OpeningFcn via varargin.
%
% *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one
% instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES

% Edit the above text to modify the response to help GPGUI

% Last Modified by GUIDE v2.5 11-Apr-2018 05:35:36

% Begin initialization code - DO NOT EDIT


gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @GPGUI_OpeningFcn, ...
'gui_OutputFcn', @GPGUI_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT

% --- Executes just before GPGUI is made visible.


function GPGUI_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% varargin command line arguments to GPGUI (see VARARGIN)

% Choose default command line output for GPGUI


handles.output = hObject;

% Update handles structure


guidata(hObject, handles);

% UIWAIT makes GPGUI wait for user response (see UIRESUME)


% uiwait(handles.figure1);

% --- Outputs from this function are returned to the command line.
function varargout = GPGUI_OutputFcn(hObject, eventdata, handles)
% varargout cell array for returning output args (see VARARGOUT);
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)

% Get default command line output from handles structure


varargout{1} = handles.output;

% --- Executes on button press in pushbutton1.


function pushbutton1_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
level=4;
I=imread('D:\j.jpg');
g = fspecial('gaussian',7,26);
%pyramid
for i = 1:level
if i == 1
im = imfilter(I,g,'conv');
G(i).img = im(:,:,1);
else
im = imfilter(G(i-1).img,g,'conv');
im1 = im(:,1:2:size(G(i-1).img,2));
im2 = im1(1:2:size(G(i-1).img,1),:);
G(i).img = im2;
end
end
axes(handles.axes1);
imshow(G(1).img,[]);
title('Level-1');

axes(handles.axes2);
imshow(G(2).img,[]);
title('Level-2');

axes(handles.axes3);
imshow(G(3).img,[]);
title('Level-3');

axes(handles.axes4);
imshow(G(4).img,[]);
title('Level-4');

Laplacian Pyramid
function varargout = LPLUI(varargin)
% LPLUI MATLAB code for LPLUI.fig
% LPLUI, by itself, creates a new LPLUI or raises the existing
% singleton*.
%
% H = LPLUI returns the handle to a new LPLUI or the handle to
% the existing singleton*.
%
% LPLUI('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in LPLUI.M with the given input arguments.
%
% LPLUI('Property','Value',...) creates a new LPLUI or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before LPLUI_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to LPLUI_OpeningFcn via varargin.
%
% *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one
% instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES

% Edit the above text to modify the response to help LPLUI


% Last Modified by GUIDE v2.5 11-Apr-2018 05:58:45

% Begin initialization code - DO NOT EDIT


gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @LPLUI_OpeningFcn, ...
'gui_OutputFcn', @LPLUI_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT

% --- Executes just before LPLUI is made visible.


function LPLUI_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% varargin command line arguments to LPLUI (see VARARGIN)

% Choose default command line output for LPLUI


handles.output = hObject;

% Update handles structure


guidata(hObject, handles);

% UIWAIT makes LPLUI wait for user response (see UIRESUME)


% uiwait(handles.figure1);

% --- Outputs from this function are returned to the command line.
function varargout = LPLUI_OutputFcn(hObject, eventdata, handles)
% varargout cell array for returning output args (see VARARGOUT);
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)

% Get default command line output from handles structure


varargout{1} = handles.output;

% --- Executes on button press in pushbutton1.


function pushbutton1_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
level=5;
I=imread('D:\j.jpg');
g = fspecial('gaussian',7,26);

%pyramid
for i = 1:level
if i == 1
im = imfilter(I,g,'conv');
G(i).img = im(:,:,1);
else
im = imfilter(G(i-1).img,g,'conv');
im1 = im(:,1:2:size(G(i-1).img,2));
im2 = im1(1:2:size(G(i-1).img,1),:);
G(i).img = im2;
if(i<level)
K(i).img=im2double(G(i).img);
G(i-1).img=im2double(G(i-1).img);
L(i).img=G(i-1).img - GPExpand(K(i).img); %calculation for
Laplacian
end
if(i==level)
L(i).img=G(i).img;
end
end
end
axes(handles.axes1);
imshow(L(2).img,[]);
title('Level-1');

axes(handles.axes2);
imshow(L(3).img,[]);
title('Level-2');

axes(handles.axes3);
imshow(L(4).img,[]);
title('Level-3');

axes(handles.axes4);
imshow(L(5).img,[]);
title('Level-4');

Function To Expand an Image


function IResult = GPExpand(I)

Wt = [0.0005 0.00025 0.0004 0.00025 0.00005];

dim = size(I);
newdim = dim*2;
IResult = zeros(newdim,class(I)); % Initialize the array in the beginning ..
m = [-2:2];n=m;
switch length(dim)
case 1
I = [ I(1) ; I ; I(dim(1))];
for i = 0 : newdim(1) - 1
pixeli = (i - m)/2 + 2; idxi = find(floor(pixeli)==pixeli);
A = I(pixeli(idxi)) .* Wt(m(idxi)+3);
IResult(i + 1)= 2 * sum(A(:));
end
case 2

I = [ I(1,:) ; I ; I(dim(1),:) ]; % Pad the top and bottom rows.


I = [ I(:,1) I I(:,dim(2)) ]; % Pad the left and right
columns.
Wt2 = Wt'*Wt;

for i = 0 : newdim(1) - 1
for j = 0 : newdim(2) - 1
pixeli = (i - m)/2 + 2; idxi = find(floor(pixeli)==pixeli);
pixelj = (j - m)/2 + 2; idxj = find(floor(pixelj)==pixelj);
A = I(pixeli(idxi),pixelj(idxj)) .* Wt2(m(idxi)+3,m(idxj)+3);
IResult(i + 1, j + 1)= 4 * sum(A(:));
end
end
case 3
Wt3 = ones(5,5,5);
for i = 1:5
Wt3(i,:,:) = Wt3(i,:,:) * Wt(i);
Wt3(:,i,:) = Wt3(:,i,:) * Wt(i);
Wt3(:,:,i) = Wt3(:,:,i) * Wt(i);
end

I2 = zeros(dim+2,class(I));
I2(2:1+dim(1),2:1+dim(2),2:1+dim(3)) = I;
I2(1,:,:)=I2(2,:,:);I2(end,:,:)=I2(end-1,:,:);
I2(:,1,:)=I2(:,2,:);I2(:,end,:)=I2(:,end-1,:);
I2(:,:,1)=I2(:,:,2);I2(:,:,end)=I2(:,:,end-1);
I=I2; clear I2;

for i = 0 : newdim(1) - 1
for j = 0 : newdim(2) - 1
for k = 0 : newdim(3) - 1
pixeli = (i - m)/2 + 2; idxi =
find(floor(pixeli)==pixeli);
pixelj = (j - m)/2 + 2; idxj =
find(floor(pixelj)==pixelj);
pixelk = (k - m)/2 + 2; idxk =
find(floor(pixelk)==pixelk);
A = I(pixeli(idxi),pixelj(idxj),pixelk(idxk)) .*
Wt3(m(idxi)+3,m(idxj)+3,m(idxk)+3);
IResult(i + 1, j + 1,k+1)= 8 * sum(A(:));
end
end
end
end
Registration Algorithm
function R = RI1();
a = imread('D:\x11.jpg');
b = imread('D:\y11.jpg');
a=rgb2gray(a);
b=rgb2gray(b);
[x,y]=imregconfig('multimodal');
z=imregister(b,a,'affine',x,y);
x.InitialRadius=x.InitialRadius/3.8;
z=imregister(b,a,'affine',x,y);
figure
imshowpair(z,a);
title('Image after alignment')
imwrite(z, 'D:\xx2.jpg');
end

Blending
function varargout = Blend(varargin)
% BLEND MATLAB code for Blend.fig
% BLEND, by itself, creates a new BLEND or raises the existing
% singleton*.
%
% H = BLEND returns the handle to a new BLEND or the handle to
% the existing singleton*.
%
% BLEND('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in BLEND.M with the given input arguments.
%
% BLEND('Property','Value',...) creates a new BLEND or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before Blend_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to Blend_OpeningFcn via varargin.
%
% *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one
% instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES

% Edit the above text to modify the response to help Blend

% Last Modified by GUIDE v2.5 11-Apr-2018 06:20:21

% Begin initialization code - DO NOT EDIT


gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @Blend_OpeningFcn, ...
'gui_OutputFcn', @Blend_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT

% --- Executes just before Blend is made visible.


function Blend_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% varargin command line arguments to Blend (see VARARGIN)

% Choose default command line output for Blend


handles.output = hObject;

% Update handles structure


guidata(hObject, handles);

% UIWAIT makes Blend wait for user response (see UIRESUME)


% uiwait(handles.figure1);

% --- Outputs from this function are returned to the command line.
function varargout = Blend_OutputFcn(hObject, eventdata, handles)
% varargout cell array for returning output args (see VARARGOUT);
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)

% Get default command line output from handles structure


varargout{1} = handles.output;

% --- Executes on button press in pushbutton1.


function pushbutton1_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
a = imread('D:\x11.jpg');
c = imread('D:\xx2.jpg');

g = fspecial('gaussian',7,26);
for i = 1:3
if i == 1
v = imfilter(a,g,'conv');
G(i).img = v(:,:,1);
w = imfilter(c,g,'conv');
G1(i).img = w(:,:,1);
else
v = imfilter(G(i-1).img,g,'conv');
w = imfilter(G1(i-1).img,g,'conv');
im1 = v(:,1:2:size(G(i-1).img,2));
im1b = w(:,1:2:size(G1(i-1).img,2));
im2 = im1(1:2:size(G(i-1).img,1),:);
im2b = im1b(1:2:size(G1(i-1).img,1),:);
G(i).img = im2;
G1(i).img = im2b;
if(i<3)
K(i).img=im2double(G(i).img);
G(i-1).img=im2double(G(i-1).img);
L(i-1).img=G(i-1).img - GPExpand(K(i).img);

K1(i).img=im2double(G1(i).img);
G1(i-1).img=im2double(G1(i-1).img);
L1(i-1).img=G1(i-1).img - GPExpand(K1(i).img);
end
end

end
u=L1(1).img;
v=L(1).img;
[m n]=size(v)
for i=1:m
for j=1:n
if(i<m/2)
L2(i,j)=v(i,j);
end
if(i==m/2)
L2(i,j)=(v(i,j)+u(i,j))/2;
end
if(i>m/2)
L2(i,j)=u(i,j);
end
end
end

for i=1:m
for j=1:n
if(j<n/2)
L3(i,j)=v(i,j);
end
if(j==n/2)
L3(i,j)=(v(i,j)+u(i,j))/2;
end
if(j>n/2)
L3(i,j)=u(i,j);
end
end
end

a=rgb2gray(a);
axes(handles.axes1);
imshow(a,[]);
title('1st Image');

axes(handles.axes2);
imshow(c,[]);
title('2nd Image');

axes(handles.axes3);
imshow(L2,[]);
title('Horizontally Blended');

axes(handles.axes4);
imshow(L3,[]);
title('Vertically Blended');

Results:
Figure 12: Guassian Pyramid

Figure 13: Laplacian Pyramid


Figure 14: Aligned Image
Figure 15: Blended Images

You might also like