简体   繁体   English

将数学函数从Python转换为C ++

[英]Converting Math functions from Python to c++

I am attempting to convert some opencv code from python to c++, and am a little lost. 我正在尝试将一些opencv代码从python转换为c ++,并且有点迷路。 The python is: python是:

if 0 < R[1,1] < 1:
    # If it gets here, the pose is flipped.

    # Flip the axes. E.g., Y axis becomes [-y0, -y1, y2].
    R *= np.array([
        [ 1, -1,  1],
        [ 1, -1,  1],
        [-1,  1, -1],
    ])

    # Fixup: rotate along the plane spanned by camera's forward (Z) axis and vector to marker's position
    forward = np.array([0, 0, 1])
    tnorm = T / np.linalg.norm(T)
    axis = np.cross(tnorm, forward)
    angle = -2*math.acos(tnorm @ forward)
    R = cv2.Rodrigues(angle * axis)[0] @ R

So far i have: 到目前为止,我有:

cv::Mat R;
if (0 < R.at<double>(1, 1) < 1) {

            // Flip the axes.E.g., Y axis becomes[-y0, -y1, y2].
            float mult[9] = { 1, -1, 1, 1, -1, 1,-1, 1, -1 };

            cv::Mat FlipAxes = cv::Mat(3, 3, CV_32F, mult);

            R *= FlipAxes;

                //# Fixup: rotate along the plane spanned by camera's forward (Z) axis and vector to marker's position
            cv::Vec3d forward(0, 0, 1);
            double tnorm = tvecBest / np.linalg.norm(T)
            axis = np.cross(tnorm, forward)
            angle = -2 * math.acos(tnorm @ forward)
            R = cv2.Rodrigues(angle * axis)[0] * R


        }

I am lost on these lines: 我迷失在这些行上:

double tnorm = tvecBest / np.linalg.norm(T)
axis = np.cross(tnorm, forward)
angle = -2 * math.acos(tnorm @ forward)

what is the equivalent of np.linalg.norm in c++ opencv? 在c ++ opencv中, np.linalg.norm等效于什么?

the np.linalg.norm(T) is just the L2 norm : np.linalg.norm(T)只是L2规范:

sqrt(T[0]*T[0]+T[1]*T[1]+T[2]*T[2]) 

np.cross(tnorm, forward) is the cross product: np.cross(tnorm,forward)是叉积:

axis[0] = tnorm[1]*forward[2]-tnorm[2]*forward[1]
axis[1] = -tnorm[0]*forward[2]+tnorm[2]*forward[0]
axis[2] = tnorm[0]*forward[1]-tnorm[1]*forward[0]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM