clear all
close all
clc
% find the four points in the two images
z= imread('image1.jpg');
imshow(z);
hold on
axis on
for i=1:8
[x,y]=ginput(1);
plot(x,y,'r*');
Px(i)=x;
Py(i)=y;
end
hold off
z2= imread('image2.jpg');
figure,imshow(z2);
hold on
axis on
for k=1:8
[x2,y2]=ginput(1);
plot(x2,y2,'r*');
Px2(k)=x2;
Py2(k)=y2;
end
hold off
% find the matrix M
A=[Px(1),Py(1),1,0,0,0,-Px2(1)*Px(1),-Px2(1)*Py(1),-Px2(1);...
0,0,0,Px(1),Py(1),1,-Py2(1)*Px(1),-Py(1)*Py2(1),-Py2(1);...
Px(2),Py(2),1,0,0,0,-Px2(2)*Px(2),-Px2(2)*Py(2),-Px2(2);...
0,0,0,Px(2),Py(2),1,-Py2(2)*Px(2),-Py(2)*Py2(2),-Py2(2);...
Px(3),Py(3),1,0,0,0,-Px2(3)*Px(3),-Px2(3)*Py(3),-Px2(3);...
0,0,0,Px(3),Py(3),1,-Py2(3)*Px(3),-Py(3)*Py2(3),-Py2(3);...
Px(4),Py(4),1,0,0,0,-Px2(4)*Px(4),-Px2(4)*Py(4),-Px2(4);...
0,0,0,Px(4),Py(4),1,-Py2(4)*Px(4),-Py(4)*Py2(4),-Py2(4);...
Px(5),Py(5),1,0,0,0,-Px2(5)*Px(5),-Px2(5)*Py(5),-Px2(5);...
0,0,0,Px(5),Py(5),1,-Py2(5)*Px(5),-Py(5)*Py2(5),-Py2(5);...
Px(6),Py(6),1,0,0,0,-Px2(6)*Px(6),-Px2(6)*Py(6),-Px2(6);...
0,0,0,Px(6),Py(6),1,-Py2(6)*Px(6),-Py(6)*Py2(6),-Py2(6);...
Px(7),Py(7),1,0,0,0,-Px2(7)*Px(7),-Px2(7)*Py(7),-Px2(7);...
0,0,0,Px(7),Py(7),1,-Py2(7)*Px(7),-Py(7)*Py2(7),-Py2(7);...
Px(8),Py(8),1,0,0,0,-Px2(8)*Px(8),-Px2(8)*Py(8),-Px2(8);...
0,0,0,Px(8),Py(8),1,-Py2(8)*Px(8),-Py(8)*Py2(8),-Py2(8);...
];
[U,S,V]=svd(A);
mm=V(:,9);
M=reshape(mm,3,3)';
% Analyze that if the matrix M is right
%figure,imshow('assignment2011_image1.jpg');
%hold on;
%axis on;
%[xx,yy]=ginput(1);
%plot(xx,yy,'+r');
%hold off
%figure,imshow('assignment2011_image2.jpg');
%hold on;
%axis on;
%Nz=M*[xx;yy;1];
%Nx=Nz(1,:);
%Ny=Nz(2,:);
%Nw=Nz(3,:);
%Nx=Nx/Nw;
%Ny=Ny/Nw;
%plot(Nx,Ny,'+r');
%hold off
ri=size(z);
li=size(z2);
J=zeros(1024,1536,2);
for k=1:1024
for m=1:1536
Mz=M*[m;k;1];
Mx=Mz(1,:);
My=Mz(2,:);
Mw=Mz(3,:);
Mx=round(Mx/Mw);
My=round(My/Mw);
J(k,m,1)=Mx;
J(k,m,2)=My;
end
end
ForwardImageX=J(:,:,1);
MinX=min(ForwardImageX(:));
MaxX=max(ForwardImageX(:));
ForwardImageY=J(:,:,2);
MinY=min(ForwardImageY(:));
MaxY=max(ForwardImageY(:));
if (MinX>0)
if (MaxX<1536)
MaxX=1536;
end
else
if (MaxX<1536)
MaxX=1536-MinX;
else
MaxX=MaxX-MinX;
end
end
if (MinY>0)
if (MaxY<1024)
MaxY=1024;
end
else
if (MaxY<1024)
MaxY=1024-MinY;
else
MaxY=MaxY-MinY;
end
end
Newimage=uint8(zeros(MaxY,MaxX,3));
for Yaxis=1:1024
for Xaxis=1:1536
for color=1:3
if(MinY<0)
Newimage(Yaxis-MinY,Xaxis,color)=z2(Yaxis,Xaxis,color);
elseif(MinX<0 && MinY<0)
Newimage(Yaxis-MinY,Xaxis-MinX,color)=z2(Yaxis,Xaxis,color);
elseif(MinX<0)
Newimage(Yaxis,Xaxis-MinX,color)=z2(Yaxis,Xaxis,color);
else
Newimage(Yaxis,Xaxis,color)=z2(Yaxis,Xaxis,color);
end
end
end
end
for Y1axis=1:1024
for X1axis=1:1536
for colour=1:3
Newimage(J(Y1axis,X1axis,2)-MinY+1,J(Y1axis,X1axis,1),colour)=z(Y1axis,X1axis,colour);
end
end
end