视觉(3)blepo

视觉(3)blepo

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

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

c++的:

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

原文链接: https://www.cnblogs.com/timssd/p/4160728.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

    视觉(3)blepo

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/158075

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月11日 下午5:48
下一篇 2023年2月11日 下午5:49

相关推荐