码迷,mamicode.com
首页 > 其他好文 > 详细

视觉(3)blepo

时间:2014-12-13 00:53:42      阅读:445      评论:0      收藏:0      [点我收藏+]

标签:blog   http   io   ar   os   sp   for   on   div   

视觉(3)blepo

把matlab转成c程序有好办法了,从网上下载了一个函数库blepo,转换为c几乎是一行对一行,openCv经常涉及到的内存申请和释放这里都不用管。高兴!
看看这段程序比较一下差别
matlab的

bubuko.com,布布扣function [A,R,t]=art(P,fsign)
bubuko.com,布布扣%ART  Factorize camera matrix into intrinsic and extrinsic matrices
bubuko.com,布布扣%
bubuko.com,布布扣%   [A,R,t] = art(P,fsign)  factorize the projection matrix P 
bubuko.com,布布扣%   as P=A*[R;t] and enforce the sign of the focal lenght to be fsign.
bubuko.com,布布扣%   By defaukt fsign=1.
bubuko.com,布布扣
bubuko.com,布布扣% Author: A. Fusiello, 1999
bubuko.com,布布扣%
bubuko.com,布布扣% fsign tells the position of the image plane wrt the focal plane. If it is
bubuko.com,布布扣% negative the image plane is behind the focal plane.
bubuko.com,布布扣
bubuko.com,布布扣
bubuko.com,布布扣
bubuko.com,布布扣% by default assume POSITIVE focal lenght
bubuko.com,布布扣if nargin == 1
bubuko.com,布布扣    fsign = 1;
bubuko.com,布布扣end
bubuko.com,布布扣
bubuko.com,布布扣s = P(1:3,4);
bubuko.com,布布扣Q = inv(P(1:3, 1:3));
bubuko.com,布布扣[U,B] = qr(Q);
bubuko.com,布布扣
bubuko.com,布布扣% fix the sign of B(3,3). This can possibly change the sign of the resulting matrix,
bubuko.com,布布扣% which is defined up to a scale factor, however.
bubuko.com,布布扣sig = sign(B(3,3));
bubuko.com,布布扣B=B*sig;
bubuko.com,布布扣s=s*sig;
bubuko.com,布布扣
bubuko.com,布布扣% if the sign of the focal lenght is not the required one, 
bubuko.com,布布扣% change it, and change the rotation accordingly.
bubuko.com,布布扣
bubuko.com,布布扣if fsign*B(1,1) < 0
bubuko.com,布布扣     E= [-1     0     0
bubuko.com,布布扣         0    1     0
bubuko.com,布布扣         0     0     1];
bubuko.com,布布扣     B = E*B;
bubuko.com,布布扣     U = U*E;
bubuko.com,布布扣 end
bubuko.com,布布扣 
bubuko.com,布布扣 if fsign*B(2,2) < 0
bubuko.com,布布扣     E= [1     0     0
bubuko.com,布布扣         0    -1     0
bubuko.com,布布扣         0     0     1];
bubuko.com,布布扣     B = E*B;
bubuko.com,布布扣     U = U*E;
bubuko.com,布布扣 end
bubuko.com,布布扣 
bubuko.com,布布扣% if U is not a rotation, fix the sign. This can possibly change the sign
bubuko.com,布布扣% of the resulting matrix, which is defined up to a scale factor, however.
bubuko.com,布布扣if det(U)< 0 
bubuko.com,布布扣    U = -U;
bubuko.com,布布扣    s= - s;
bubuko.com,布布扣end
bubuko.com,布布扣
bubuko.com,布布扣  
bubuko.com,布布扣% sanity check 
bubuko.com,布布扣if (norm(Q-U*B)>1e-10) & (norm(Q+U*B)>1e-10) 
bubuko.com,布布扣    error(‘Something wrong with the QR factorization.‘); end
bubuko.com,布布扣
bubuko.com,布布扣R = U‘;
bubuko.com,布布扣t = B*s;
bubuko.com,布布扣A = inv(B);
bubuko.com,布布扣A = A ./A(3,3);
bubuko.com,布布扣
bubuko.com,布布扣
bubuko.com,布布扣% sanity check 
bubuko.com,布布扣if det(R) < 0 error(‘R is not a rotation matrix‘); end
bubuko.com,布布扣if A(3,3) < 0 error(‘Wrong sign of A(3,3)‘); end
bubuko.com,布布扣% this guarantee that the result *is* a factorization of the given P, up to a scale factor
bubuko.com,布布扣W = A*[R,t];
bubuko.com,布布扣if (rank([P(:), W(:)]) ~= 1 )
bubuko.com,布布扣    error(‘Something wrong with the ART factorization.‘); end
bubuko.com,布布扣
bubuko.com,布布扣
bubuko.com,布布扣
bubuko.com,布布扣


c++的:

bubuko.com,布布扣//P 3*4
bubuko.com,布布扣//A,R 3*3,T 3*1
bubuko.com,布布扣void art(MatDbl P,
bubuko.com,布布扣         OUT MatDbl *A,OUT MatDbl *R,OUT MatDbl *T,
bubuko.com,布布扣         int fsign=1)
bubuko.com,布布扣{
bubuko.com,布布扣    MatDbl s;
bubuko.com,布布扣    MatDbl Q;
bubuko.com,布布扣
bubuko.com,布布扣    s=P.GetSubMat(0,3,3,1);
bubuko.com,布布扣    Q=Inverse(P.GetSubMat(0,0,3,3));
bubuko.com,布布扣//      Display(s,"s");
bubuko.com,布布扣//      Display(Q,"Q");
bubuko.com,布布扣
bubuko.com,布布扣    MatDbl U,B;
bubuko.com,布布扣    Qr(Q,&U,&B);
bubuko.com,布布扣//     PrintF(U,"U");
bubuko.com,布布扣//     PrintF(B,"B");
bubuko.com,布布扣//     PrintF(U*B-Q,"U*B-Q");
bubuko.com,布布扣//     PrintF(U*Transpose(U),"U*U‘");
bubuko.com,布布扣    
bubuko.com,布布扣    if(B(2,2)<0)
bubuko.com,布布扣    {
bubuko.com,布布扣        Negate(B,&B);
bubuko.com,布布扣        Negate(s,&s);
bubuko.com,布布扣    }
bubuko.com,布布扣
bubuko.com,布布扣    if(fsign*B(0,0)<0)
bubuko.com,布布扣    {
bubuko.com,布布扣        double E[9]={-1 ,0 ,0 ,0 ,1 ,0 ,0 ,0 ,1};
bubuko.com,布布扣        MatDbl _E;
bubuko.com,布布扣        _E.FromArray(E,3,3);
bubuko.com,布布扣        B=_E*B;
bubuko.com,布布扣        U=U*_E;
bubuko.com,布布扣    }
bubuko.com,布布扣
bubuko.com,布布扣    if(fsign*B(1,1)<0)
bubuko.com,布布扣    {
bubuko.com,布布扣        double E[9]={1 ,0 ,0 ,0 ,-1 ,0 ,0 ,0 ,1};
bubuko.com,布布扣        MatDbl _E;
bubuko.com,布布扣        _E.FromArray(E,3,3);
bubuko.com,布布扣        B=_E*B;
bubuko.com,布布扣        U=U*_E;
bubuko.com,布布扣    }
bubuko.com,布布扣
bubuko.com,布布扣    if(Determinant(U)<0)
bubuko.com,布布扣    {
bubuko.com,布布扣        Negate(U,&U);
bubuko.com,布布扣        Negate(s,&s);
bubuko.com,布布扣    }
bubuko.com,布布扣
bubuko.com,布布扣//     if(Norm((Q-U*B))>1e-10 && Norm((Q+U*B).ToVector)>1e-10)
bubuko.com,布布扣//         printf("‘Something wrong with the QR factorization.‘\n")    ;
bubuko.com,布布扣
bubuko.com,布布扣    *R=Transpose(U);
bubuko.com,布布扣    *T=B*s;
bubuko.com,布布扣    *A=Inverse(B);
bubuko.com,布布扣    *A= *A * (1.0 / (*A)(2,2));
bubuko.com,布布扣
bubuko.com,布布扣//     PrintF(*A,"A");
bubuko.com,布布扣//     PrintF(*R,"R");
bubuko.com,布布扣//     PrintF(*T,"T");
bubuko.com,布布扣//     PrintF((*A) * (*R),"A*R");
bubuko.com,布布扣//     PrintF((*A) * (*T),"A*T");
bubuko.com,布布扣}
bubuko.com,布布扣
bubuko.com,布布扣//[T1,T2,Pn1,Pn2] = rectify(Po1,Po2,d1,d2)
bubuko.com,布布扣void Rectify(MatDbl Po1,MatDbl Po2,
bubuko.com,布布扣             OUT MatDbl &T1,OUT MatDbl &T2,OUT MatDbl &Pn1,OUT MatDbl &Pn2
bubuko.com,布布扣             /*double d1=0,double d2=0*/)
bubuko.com,布布扣{
bubuko.com,布布扣    MatDbl A1,R1,t1;
bubuko.com,布布扣    MatDbl A2,R2,t2;
bubuko.com,布布扣    art(Po1,&A1,&R1,&t1);
bubuko.com,布布扣    art(Po2,&A2,&R2,&t2);
bubuko.com,布布扣
bubuko.com,布布扣    MatDbl c1,c2;
bubuko.com,布布扣    c1=-Transpose(R1)*Inverse(A1)*Po1.GetSubMat(0,3,3,1);
bubuko.com,布布扣    c2=-Transpose(R2)*Inverse(A2)*Po2.GetSubMat(0,3,3,1);
bubuko.com,布布扣//     PrintF(c1,"c1");
bubuko.com,布布扣//     PrintF(c2,"c2");
bubuko.com,布布扣
bubuko.com,布布扣    MatDbl v1,v2,v3;
bubuko.com,布布扣    v1=c2-c1;
bubuko.com,布布扣    v2=CrossProduct(Transpose(R1.GetSubMat(2,0,1,3)),v1);
bubuko.com,布布扣    v3=CrossProduct(v1,v2);
bubuko.com,布布扣//      Display(v1,"v1");
bubuko.com,布布扣//      Display(v2,"v2");
bubuko.com,布布扣//     Display(v3,"v3");
bubuko.com,布布扣    
bubuko.com,布布扣    v1=(v1 * (1.0/Norm(v1)));
bubuko.com,布布扣    v2=(v2 * (1.0/Norm(v2)));
bubuko.com,布布扣    v3=(v3 * (1.0/Norm(v3)));
bubuko.com,布布扣    double r[9]={v1(0),v1(1),v1(2),
bubuko.com,布布扣        v2(0),v2(1),v2(2),
bubuko.com,布布扣        v3(0),v3(1),v3(2)};
bubuko.com,布布扣    MatDbl R;
bubuko.com,布布扣    R.FromArray(r,3,3);
bubuko.com,布布扣    //Display(R,"R");
bubuko.com,布布扣
bubuko.com,布布扣    MatDbl An1=A2;
bubuko.com,布布扣    An1(1,0)=0;
bubuko.com,布布扣    MatDbl An2=An1;
bubuko.com,布布扣    //PrintF(An1,"An1");
bubuko.com,布布扣
bubuko.com,布布扣//    Display(An1 *(-1.0) * R * c1,"An1 *(-1.0) * R * c1");
bubuko.com,布布扣    Pn1=An1 * PackX(R, (-1.0) * R * c1);
bubuko.com,布布扣    Pn2=An2 * PackX(R, (-1.0) * R * c2);
bubuko.com,布布扣    PrintF(Pn1,"Pn1");
bubuko.com,布布扣    PrintF(Pn2,"Pn2");
bubuko.com,布布扣
bubuko.com,布布扣    T1=Pn1.GetSubMat(0,0,3,3) * Inverse(Po1.GetSubMat(0,0,3,3));
bubuko.com,布布扣    T2=Pn2.GetSubMat(0,0,3,3) * Inverse(Po2.GetSubMat(0,0,3,3));
bubuko.com,布布扣    PrintF(T1,"T1");
bubuko.com,布布扣    PrintF(T2,"T2");
bubuko.com,布布扣}

视觉(3)blepo

标签:blog   http   io   ar   os   sp   for   on   div   

原文地址:http://www.cnblogs.com/x113/p/4160728.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!