dlt2d.m
上传用户:kellyonhid
上传日期:2013-10-12
资源大小:932k
文件大小:5k
源码类别:

3D图形编程

开发平台:

Visual C++

  1. function [pos,foc,u0,v0,b1,b2]=dlt2d(sys,data)
  2. %DLT2D Direct linear transform for coplanar control points, which
  3. %calculates the initial camera position for nonlinear optimization.
  4. %
  5. %Usage:
  6. %   [pos,foc,u0,v0,b1,b2] = dlt2d(sys,data)         
  7. %
  8. %where       
  9. %   sys  = system configuration information (see configc.m) 
  10. %   data = matrix that contains the 3-D coordinates of the
  11. %          control points (in fixed right-handed frame) and corresponding
  12. %          image observations (in image frame, origo in the upper left
  13. %          corner and the y-axis downwards). 
  14. %          Dimensions: (n x 5) matrix, format: [wx wy 0 ix iy]
  15. %          NOTE: the calibration frame should be selected so that
  16. %                the z-coordinates of the control points are zero
  17. %   pos  = camera position and orientation [x y z w p r]
  18. %   foc  = effective focal length (obtained from sys)
  19. %   u0, v0 = principal point (obtained from sys)
  20. %   b1, b2 = linear distortion coefficients
  21. %Reference:
  22. %   Trond Melen: Geometrical modelling and calibration of video
  23. %   cameras for underwater navigation, Ph.D.-thesis, 
  24. %   ITK-rapport 1994:103-W, NTH, Norway.           
  25. %
  26. %   Version 2.0  15.5.-97
  27. %   Janne Heikkila, University of Oulu, Finland
  28. NDX=sys(1); NDY=sys(2); Sx=sys(3); Sy=sys(4); f0=sys(5);
  29. wx=data(:,1); wy=data(:,2); wz=data(:,3);
  30. num=size(data,1);
  31. u0=Sx/2; v0=Sy/2;
  32. u = Sx*data(:,4)/NDX;
  33. v = Sy*data(:,5)/NDY;
  34. Lu=[wx wy 0*u+1 0*u 0*u 0*u -wx.*u -wy.*u];
  35. Lv=[0*v 0*v 0*v wx wy 0*v+1 -wx.*v -wy.*v];
  36. L=reshape([Lu';Lv'],8,2*num)';
  37. l=reshape([u';v'],2*num,1);
  38. a=pinv(L)*l;
  39. a(9)=1;
  40. A=reshape(a,3,3)';
  41. a11=(A(3,1)^2-A(3,2)^2)*(u0^2+v0^2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2)+A(2,2)*A(3,1))*v0+A(1,1)*A(1,2)+A(2,1)*A(2,2);
  57. if a11 & a21
  58.   t12=a12/a11; t13=a13/a11; t14=a14/a11;
  59.   s22=a22/a21; s23=a23/a21; s24=a24/a21;
  60.   at=4*(t12-s22)^2+4*(t13-s23)^2;
  61.   bt=4*(t13*t14-t13*s24-s23*t14+s23*s24+2*t12^2*s23-2*t12*s22*t13-...
  62.   2*t12*s22*s23+2*t13*s22^2);
  63.   ct=t14^2-2*t14*s24+s24^2-4*t14*t12*s22+4*t14*s22^2+4*t12^2*s24-4*t12*s22*s24;
  64.   b2a=(-bt-sqrt(bt^2-4*at*ct))/(2*at);
  65.   b2b=(-bt+sqrt(bt^2-4*at*ct))/(2*at);
  66.   b1a=-(2*(a13*a21-a23*a11)*b2a+a14*a21-a24*a11)/(2*a12*a21-2*a22*a11);
  67.   b1b=-(2*(a13*a21-a23*a11)*b2b+a14*a21-a24*a11)/(2*a12*a21-2*a22*a11);
  68.   if abs(b1a)<abs(b1b)
  69.     b1=b1a; b2=b2a;
  70.   else
  71.     b1=b1b; b2=b2b;
  72.   end
  73. else
  74.   if ~a11 & ~a21
  75.     b2=1/2*(a12*a24-a22*a14)/(-a12*a23+a22*a13);
  76.     b1=-1/2*(a13*a24-a14*a23)/(-a12*a23+a22*a13);
  77.   else
  78.     if a11
  79.       at=4*a23^2*a11+4*a11*a22^2;
  80.       bt=-8*a12*a22*a23+4*a24*a23*a11+8*a13*a22^2;
  81.       ct=-4*a12*a22*a24+a11*a24^2+4*a14*a22^2;
  82.       b2a=(-bt-sqrt(bt^2-4*at*ct))/(2*at);
  83.       b2b=(-bt+sqrt(bt^2-4*at*ct))/(2*at);
  84.       b1a=-1/2*(2*a23*b2a+a24)/a22;
  85.       b1b=-1/2*(2*a23*b2b+a24)/a22;
  86.     else
  87.       at=4*a21*a13^2+4*a21*a12^2;
  88.       bt=-8*a12*a22*a13+4*a21*a13*a14+8*a23*a12^2;
  89.       ct=-4*a12*a22*a14+a21*a14^2+4*a24*a12^2;
  90.       b2a=(-bt-sqrt(bt^2-4*at*ct))/(2*at);
  91.       b2b=(-bt+sqrt(bt^2-4*at*ct))/(2*at);
  92.       b1a=-1/2*(2*a13*b2a+a14)/a12;
  93.       b1b=-1/2*(2*a13*b2b+a14)/a12;
  94.     end
  95.     if abs(b1a)<abs(b1b)
  96.       b1=b1a; b2=b2a;
  97.     else
  98.       b1=b1b; b2=b2b;
  99.     end
  100.   end
  101. end
  102.     
  103. m11=(A(1,1)*(1+b1)+A(2,1)*b2-A(3,1)*(1+b1)*u0-A(3,1)*b2*v0)/f0;
  104. m21=(A(1,1)*b2+A(2,1)*(1-b1)-A(3,1)*b2*u0-A(3,1)*(1-b1)*v0)/f0;
  105. m31=A(3,1);
  106. m12=(A(1,2)*(1+b1)+A(2,2)*b2-A(3,2)*(1+b1)*u0-A(3,2)*b2*v0)/f0;
  107. m22=(A(1,2)*b2+A(2,2)*(1-b1)-A(3,2)*b2*u0-A(3,2)*(1-b1)*v0)/f0;
  108. m32=A(3,2);
  109. lambda=sqrt(m11^2+m21^2+m31^2);
  110. m1=[m11 m21 m31]'/lambda;
  111. m2=[m12 m22 m32]'/lambda;
  112. m3=cross(m1,m2);
  113. M=[m1 m2 m3];
  114. t0=(A(1,3)*((1+b1)*M(1,:)+b2*M(2,:))+A(2,3)*(b2*M(1,:)+(1-b1)*M(2,:))-...
  115. A(3,3)*((1+b1)*M(1,:)*u0+b2*M(2,:)*u0+b2*M(1,:)*v0+(1-b1)*M(2,:)*v0-...
  116. M(3,:)*f0))/(lambda*f0);
  117. t=M*t0';
  118. if t(3)<0, t0(3)=-t0(3); t=M*t0'; end
  119. pa=asin(M(3,1));
  120. wa=atan2(-M(3,2)/cos(pa),M(3,3)/cos(pa));
  121. ra=atan2(-M(2,1)/cos(pa),M(1,1)/cos(pa));
  122. pos=[t' -[wa pa ra]*180/pi];
  123. foc=f0; u0=NDX/2; v0=NDY/2;