hallway6

clear, close all
filename = '2011_0328_131452AA.JPG';
rgb = im2double(imread(filename));
rgbsm = imresize(rgb,0.4);
%imshow(rgbsm);
load hallway1_project

% [x y] = ginput(8);
% save hallway1_project filename x y

imshow(rgbsm);
% show hallway edges
hold on
plot(x(1:2),y(1:2),'g','LineWidth',2);
plot(x(3:4),y(3:4),'m','LineWidth',2);
plot(x(5:6),y(5:6),'m','LineWidth',2);
plot(x(7:8),y(7:8),'g','LineWidth',2);
% show image center
sz = size(rgbsm);
cen = sz(1:2)/2;
plot(cen(2),cen(1),'yo','MarkerSize',8);
fprintf('image center %g %g\n',cen(2),cen(1));
hold off
Warning: Image is too big to fit on screen; displaying at 67% 
image center 410 307.5
hold on
% show hallway vanishing points
vp1 = intersect([x(1:2) y(1:2)],[x(7:8) y(7:8)],'gx');
fprintf('  floor vanishing point %g %g\n',vp1);
vp2 = intersect([x(3:4) y(3:4)],[x(5:6) y(5:6)],'mx');
fprintf('ceiling vanishing point %g %g\n',vp2);
hold off
  floor vanishing point 431.898 289.802
ceiling vanishing point 431.898 283.219
load hallway_project2

% show checkerboard edges and vanishing point
pts = [input_points; input_points(1,:)];
hold on
plot(pts(:,1),pts(:,2),'b','LineWidth',2);
intersect(pts(2:3,:),pts(4:5,:),'bx');
hold off
scl = 1/max(cen);
Txy = [scl 0  -cen(2)*scl; 0 -scl cen(1)*scl];
imgpts = [x(1:2) y(1:2) ones(2,1) ; x(7:8) y(7:8) ones(2,1)]*Txy';

plot(imgpts(1:2,1),imgpts(1:2,2),'g');
hold on;
plot(imgpts(3:4,1),imgpts(3:4,2),'r');
plot(0,0,'ko');
hold off
axis equal
v = [-5 -4];
[vf fmin exitflag options] = fminsearch(@fval4a,v,[],imgpts)
vf =

   -2.4717  -83.7318


fmin =

  8.4834e-009


exitflag =

     1


options = 

    iterations: 64
     funcCount: 124
     algorithm: 'Nelder-Mead simplex direct search'
       message: [1x196 char]

vfit = [0 vf(1) 0 vf(2)];
[pto Tn pn] = back_projection(vfit,imgpts);
draw5(pto,[-200 200 0 800]);
tbpts = [pts ones(5,1)]*Txy';
pto2 = back_projection(vfit,tbpts);
hold on;
plot(pto2(:,1),pto2(:,2),'k','LineWidth',2);
hold off;
pto2
dx = pto2(2,1)-pto2(1,1);
dy = pto2(4,2)-pto2(1,2);
fprintf('dx %g dy %g\n',dx,dy);
fprintf('aspect ratio = %g\n',dy/dx);
% aspect ratio should be 0.8
yscale =  0.8*dx/dy;
fprintf('scale factor %g\n',yscale);
pto2 =

  -47.0008  115.2291         0
   65.2771  118.3919         0
   66.8319  156.2587         0
  -47.6109  156.8182         0
  -47.0008  115.2291         0

dx 112.278 dy 41.589
aspect ratio = 0.370412
scale factor 2.15976
load hallway_project2
base_points = [200 420; 600 420; 600 100; 200 100]/5;
T2 = cp2tform(input_points,base_points,'Projective');
out = imtransform(rgbsm,T2,'XData',[0 1200]/5,'YData',[-4000 700]/5);
imshow(out);
Tq = [Txy; 0 0 1];
Tp = Tq'*Tn(1:3,[1 2 4])*[1 0 0; 0 -1 0; 0 0 1];
T = maketform('projective',Tp);
T.tdata.T
%[out XData YData] = imtransform(rgbsm,T);
out = imtransform(rgbsm,T,'XData',[-150 150],'YData',[-500 -100]);
imshow(out);
ans =

   -0.2042         0         0
         0   -0.0088   -0.0024
   83.7318   86.3622    0.7062

% adjust scale so checkboard has approximately the right proportions
sz = size(out);
out1 = imresize(out,[sz(1)*yscale sz(2)]);
imshow(out1);
% adjust angle
delta = diff(pto(1:2,1:2));
phi = angle(delta(2)+1j*delta(1))*180/pi
out2 = imrotate(out1,phi/yscale);
imshow(out2);
phi =

    3.0544

% rotate the image so the sides resemble the floor
rgbr = imrotate(rgbsm,90);
imshow(rgbr);
sz = size(rgbr);
cen = sz(1:2)/2;
scl = 1/max(cen);
Txy = [scl 0  -cen(2)*scl; 0 -scl cen(1)*scl];
imgpts3 = [y(1:2) x(1:2) ones(2,1) ; y(3:4) x(3:4) ones(2,1)]*Txy';
v = [-5 4];
[vf2 fmin exitflag options] = fminsearch(@fval4a,v,[],imgpts3)
vf2 =

    3.3585   65.6684


fmin =

  2.4763e-008


exitflag =

     1


options = 

    iterations: 66
     funcCount: 126
     algorithm: 'Nelder-Mead simplex direct search'
       message: [1x196 char]

vfit = [0 vf2(1) 0 vf2(2)];
[pto3 Tn pn] = back_projection(vfit,imgpts3);

idx = 1:2;
plot(pto3(idx,1),pto3(idx,2),'g','LineWidth',2);
hold on
idx = 3:4;
plot(pto3(idx,1),pto3(idx,2),'r','LineWidth',2);
hold off
axis equal
% transform the hallway sides
Tq = [Txy; 0 0 1];
Tp = Tq'*Tn(1:3,[1 2 4])*[1 0 0; 0 -1 0; 0 0 1];
T = maketform('projective',Tp);
T.tdata.T
%[out XData YData] = imtransform(rgbsm,T);
out = imtransform(rgbr,T,'XData',[-100 400],'YData',[20 900]);
%imshow(out);
imshow(imrotate(out,90));
ans =

    0.1602         0         0
         0   -0.0094   -0.0024
  -49.2513  -61.7085    1.0569

% look at the left side of the hallway
out = imtransform(rgbr,T,'XData',[-100 100],'YData',[80 300]);
out1 = imrotate(out,90);
sz = size(out1);
out2 = imresize(out1,[sz(1) sz(2)*2]);
imshow(out2);
% now look at the other side of the hallway
out = imtransform(rgbr,T,'XData',[-120 40],'YData',[-240 -50]);
imshow(out);
% rearrange view
out1 = imrotate(out,-90);
sz = size(out1);
out2 = imresize(out1,[sz(1) sz(2)*2]);
% the final view still looks wrong left-to-right
imshow(out2);