Professional Documents
Culture Documents
Assignment#1
Submitted to
Dr. Haroon Yousaf
Submitted by
Behzad Mehmood
17F-MS-EE-09
Izzat Fatima
17F-MS-EE-16
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);
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
end
end
b=im2double(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 ;
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
end
end
end
Results:
I)
Figure 1: Interst Points on Image1
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'));
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
pmd = cell(1,level);
pmd{1} = (a);
for i = 2:level
pmd{i} = pr(pmd{i-1});
end
if strcmp(type,'gauss'), return;
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));
% 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
Results:
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
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT
% --- 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)
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
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT
% --- 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)
%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');
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
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
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT
% --- 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)
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