script2

clear
close all
load filelist
k1 = 4;
file1 = files{k1};
rgb = imread(file1);
% imshow(rgb);
% pt1 = ginput(1);
% pt2 = ginput(1);
% pt3 = ginput(1);
% pt4 = ginput(1);
% gpts = [pt1;pt2;pt3;pt4]
% save script2 gpts
load script2
imshow(rgb);
hold on
plot(gpts(:,1),gpts(:,2),'y','LineWidth',2);
hold off
Warning: Image is too big to fit on screen; displaying
at 25% 
load tform
[x, y] = transformPointsForward(tf, gpts(:,1),gpts(:,2));
x = x/20/12;
y = y/20/12;
[x y]
plot(x(1:4),y(1:4),'k','LineWidth',2);
axis ij
axis equal
grid

kv1 = [x(2)-x(1) y(2)-y(1)];
kv2 = [x(4)-x(3) y(4)-y(3)];
dist(1) = norm(kv1);
dist(2) = norm(kv2);
kv1 = kv1/dist(1);
kv2 = kv2/dist(2);
phi1 = atan2(kv1(2),kv1(1))*180/pi
phi2 = atan2(kv2(2),kv2(1))*180/pi
theta = phi2 - phi1

D = det([kv1; kv2]);
theta = asin(D)*180/pi;
fprintf('theta %g deg\n',theta);

kp = [-kv1(2) kv1(1)];
p = [x(4)-x(1) y(4)-y(1)];
kp*p'


dist
ans =

   1.0e+03 *

   -0.0044    0.0034
   -0.0515   -1.0383
    0.0080   -2.4603
    0.0020    0.0033


phi1 =

  -92.5899


phi2 =

   90.1403


theta =

  182.7302

theta -2.7302 deg

ans =

    6.3288


dist =

   1.0e+03 *

    1.0428    2.4636

sz = size(rgb);
nx = sz(2);
ny = sz(1);
cc = [nx ny]/2;
fc = 6600;

pts = (gpts - ones(4,1)*cc)/fc
pts =

   -0.2154    0.1962
    0.0268   -0.1143
    0.0796   -0.1172
    0.3221    0.1961

hpts = e2h(gpts');

% R = [vi; vj; vk]
%R = eye(3);
phi = 81.5;
phit = phi*pi/180;

R = [ 1 0 0; 0 cos(phit) -sin(phit); 0 sin(phit) cos(phit) ]

KK = eye(3);
KK = [ fc 0 cc(1); 0 fc cc(2); 0 0 1];
T1 = R*inv(KK);

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

newcp = [0 0]';

scl =3.82*10;
M = [scl 0 0; 0 scl 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
R =

    1.0000         0         0
         0    0.1478   -0.9890
         0    0.9890    0.1478


newpts =

   1.0e+03 *

   -0.0241    0.0295    0.0952    0.0360
   -0.1073   -1.1059   -1.2045   -0.1073

x = newpts(1,:);
y = newpts(2,:);

kv1 = [x(2)-x(1) y(2)-y(1)];
kv2 = [x(3)-x(4) y(3)-y(4)];
dist(1) = norm(kv1);
dist(2) = norm(kv2);
kv1 = kv1/dist(1);
kv2 = kv2/dist(2);
phi1 = atan2(kv1(2),kv1(1))*180/pi
phi2 = atan2(kv2(2),kv2(1))*180/pi
theta = phi2 - phi1

D = det([kv1; kv2]);
theta = asin(D)*180/pi;
fprintf('theta %g deg\n',theta);

kp = [-kv1(2) kv1(1)];
p = [x(4)-x(1) y(4)-y(1)];
kp*p'

dist
phi1 =

  -86.9319


phi2 =

  -86.9104


theta =

    0.0215

theta 0.0215088 deg

ans =

   59.9844


dist =

   1.0e+03 *

    1.0001    1.0988

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

t.tdata.T

[U V] = tformfwd(t,gpts(:,1),gpts(:,2))
% [out, XData, YData] = imtransform(rgb,t);
out = imtransform(rgb,t,'XData',[-100 100],'YData',[-1200 0]);
% out = imtransform(rgb,t,'XData',[-500 500],'Data',[-500 500],'XYScale',1);
imshow(out);
hold on
plot(U+100,V+1200,'y','LineWidth',2);
hold off
ans =

    0.0058         0         0
         0    0.0009    0.0001
  -13.3353  -38.8891   -0.0464


U =

  -24.0671
   29.4586
   95.2253
   36.0043


V =

   1.0e+03 *

   -0.1073
   -1.1059
   -1.2045
   -0.1073

plot(U,V,'k','LineWidth',2);
axis ij
axis equal
tf2 = t;
save script2 gpts tf2 phi
tf3 = projective2d(T');

[U2, V2] = transformPointsForward(tf3, gpts(:,1), gpts(:,2));

norm([U2-U V2-V])

imageSize = [2400 400];
xWorldLimits = [-100 100];
yWorldLimits = [-1200 0];
ref = imref2d(imageSize,xWorldLimits,yWorldLimits);
ref
out = imwarp(rgb,tf3,'OutputView',ref);
imshow(out);
[x, y] = transform_to_warp(tf3,ref,gpts(:,1), gpts(:,2));
hold on
plot(x,y,'y','LineWidth',2);
hold off
ans =

     0


ref = 

  imref2d with properties:

           XWorldLimits: [-100 100]
           YWorldLimits: [-1200 0]
              ImageSize: [2400 400]
    PixelExtentInWorldX: 0.5000
    PixelExtentInWorldY: 0.5000
    ImageExtentInWorldX: 200
    ImageExtentInWorldY: 1200
       XIntrinsicLimits: [0.5000 400.5000]
       YIntrinsicLimits: [0.5000 2.4005e+03]

file2 = files{1};
rgb2 = imread(file2);

imageSize = [400 400];
xWorldLimits = [-100 100];
yWorldLimits = [-300 -100];
ref2 = imref2d(imageSize,xWorldLimits,yWorldLimits);
ref2
out = imwarp(rgb2,tf3,'OutputView',ref2);
imshow(out);
ref2 = 

  imref2d with properties:

           XWorldLimits: [-100 100]
           YWorldLimits: [-300 -100]
              ImageSize: [400 400]
    PixelExtentInWorldX: 0.5000
    PixelExtentInWorldY: 0.5000
    ImageExtentInWorldX: 200
    ImageExtentInWorldY: 200
       XIntrinsicLimits: [0.5000 400.5000]
       YIntrinsicLimits: [0.5000 400.5000]

imageSize = [400 400];
xWorldLimits = [10 50];
yWorldLimits = [-150 -110];
ref3 = imref2d(imageSize,xWorldLimits,yWorldLimits);
ref3
out = imwarp(rgb2,tf3,'OutputView',ref3);
imshow(out);
ref3 = 

  imref2d with properties:

           XWorldLimits: [10 50]
           YWorldLimits: [-150 -110]
              ImageSize: [400 400]
    PixelExtentInWorldX: 0.1000
    PixelExtentInWorldY: 0.1000
    ImageExtentInWorldX: 40
    ImageExtentInWorldY: 40
       XIntrinsicLimits: [0.5000 400.5000]
       YIntrinsicLimits: [0.5000 400.5000]