cube6

extract camera parameters from control points. The merit function is fval1.m

load view2
rgb = imread('view2.jpg');
sz = size(rgb);
% normalize image coordinates to range [ -0.5 : 0.5 ]
% (assuming square image)
p = pts/sz(2) - 0.5;
% starting point for optimization
v = [-45 35 0 -3 -3 3 1 0 -1 0];
[vfit, fmin, exitflag, options] = fminsearch(@fval1,v,optimset('MaxFunEvals',4000),p);
fprintf('%s\n',options.message);
% show results
vfit'
fprintf('camera view angle: %g\n',atan(1/vfit(7))*180/pi);
T = my_camera(vfit)
Optimization terminated:
 the current x satisfies the termination criteria using OPTIONS.TolX of 1.000000e-004 
 and F(X) satisfies the convergence criteria using OPTIONS.TolFun of 1.000000e-004 


ans =

  -31.1054
   28.9825
    0.0009
   -1.0475
   -2.0857
    2.0237
    1.9177
    0.0003
   -1.9190
    0.0003

camera view angle: 27.5403

T =

    0.8562   -0.5166   -0.0000   -0.1806
    0.2503    0.4149    0.8748   -0.6428
    0.4519    0.7490   -0.4845    3.0161

show points on image

figure(1);
imshow(rgb);
idx = [1 2 4 5 6 7 8];
show_loc(vfit,idx,sz(2));

show errors

figure(2);
show_errors(vfit,p,idx,sz(2),2);