script4 (directed homography)

clear
close all
rgb = imread('mod_3.jpg');
imshow(rgb);

% pause
% pts = ginput(4);
% save script4c pts
load script4c
pts = pts';

hold on
plot(pts(1,:),pts(2,:),'bx','MarkerSize',11,'LineWidth',2);
hold off
vi = [0 0 -1];
vk = [1 0 0];
vj = cross(vk,vi);
f = 879.2;

hpts = e2h(pts);

KK = [ f 0 320; 0 f 320; 0 0 1];
R = [vi; vj; vk]
%R = eye(3);

T1 = R/KK;

cp = mean(pts,2);
newcp = h2e(T1*e2h(cp));

fprintf('camera position along wall: %g %g\n',-newcp*3);

%newcp = [0 0]';

M = [300 0 0; 0 300 0; 0 0 1]*[eye(2,2) -newcp ;  0 0 1];
T = M*T1;

newpts = h2e(T*hpts);

plot([newpts(1,:) newpts(1,1)],[newpts(2,:) newpts(2,1)],'k');
axis ij
axis equal


t = maketform('projective',T');

disp('t.tdata.T');
disp(t.tdata.T);

[U V] = tformfwd(t,pts(1,:),pts(2,:));

disp('[U; V] (from tformfwd)');
disp([U; V]);
R =

     0     0    -1
     0     1     0
     1     0     0

camera position along wall: 9.87244 -0.00268002
t.tdata.T
    1.1229   -0.0003    0.0011
         0    0.3412         0
 -659.3245 -109.0926   -0.3640

[U; V] (from tformfwd)
 -108.8053   89.1538   87.3872 -106.1855
   99.3992   99.0667  -99.0145  -99.4619

out = imtransform(rgb,t,'XData',[-400 400],'YData',[-400 400],'XYScale',1);
imshow(out);
tf = projective2d(T');

imageSize = [800 800];
xWorldLimits = [-400 400];
yWorldLimits = [-400 400];

RA = imref2d(imageSize,xWorldLimits,yWorldLimits);

[out, RB] = imwarp(rgb,tf,'OutputView',RA);
imshow(out);
imshow(out);

do_input = false;
if do_input
    hd1 = imdistline;
    hd2 = imdistline;
    pause
    pos1 = hd1.getPosition();
    pos2 = hd2.getPosition();
    load script4c
    save script4c pts pos1 pos2
else
    hd1 = imdistline(gca,pos1(:,1),pos1(:,2));
    hd2 = imdistline(gca,pos2(:,1),pos2(:,2));
end