gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 04:34:13 24 4
gpt4 key购买 nike

我正在尝试将一些 opencv 代码从 python 转换为 c++,但有点迷茫。 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

到目前为止我有:

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


}

我迷失在这些线路上:

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

c++ opencv 中 np.linalg.norm 的等价物是什么?

最佳答案

np.linalg.norm(T) 只是 L2 范数:

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

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]

关于python - 将数学函数从 Python 转换为 C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51969992/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com