c++ - 使用 cv::Mat 的高效 C++ 四元数乘法

标签 c++ opencv quaternions

我想乘以 2 个四元数,它们存储在 cv::Mat 结构中。我希望函数尽可能高效。到目前为止,我有以下代码:

/** Quaternion multiplication
 *
 */
void multiplyQuaternion(const Mat& q1,const Mat& q2, Mat& q)
{
    // First quaternion q1 (x1 y1 z1 r1)
    const float x1=q1.at<float>(0);
    const float y1=q1.at<float>(1);
    const float z1=q1.at<float>(2);
    const float r1=q1.at<float>(3);

    // Second quaternion q2 (x2 y2 z2 r2)
    const float x2=q2.at<float>(0);
    const float y2=q2.at<float>(1);
    const float z2=q2.at<float>(2);
    const float r2=q2.at<float>(3);


    q.at<float>(0)=x1*r2 + r1*x2 + y1*z2 - z1*y2;   // x component
    q.at<float>(1)=r1*y2 - x1*z2 + y1*r2 + z1*x2;   // y component
    q.at<float>(2)=r1*z2 + x1*y2 - y1*x2 + z1*r2;   // z component
    q.at<float>(3)=r1*r2 - x1*x2 - y1*y2 - z1*z2;   // r component
}

这是 OpenCV 最快的方法吗?使用定点运算会最快吗?

最佳答案

this教程涵盖了访问不同像素的不同方法。与直接像素访问相比,Mat::at 函数被发现慢了大约 10%,这可能是由于 Debug模式下的额外检查。

如果您真的不在乎性能,您应该使用文中提到的 3 种不同方法重写您的方法,然后分析以找到最适合您情况的方法。

关于c++ - 使用 cv::Mat 的高效 C++ 四元数乘法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10781033/

相关文章:

c++ - 为什么这个函数会产生不正确的值?

Python 和 OpenCV - 有什么方法可以确保在 cv2.createTrackbar 的轨迹栏上滑动会给我一个奇怪的值?

java - 按给定四元数旋转单位 vector

javascript - 在球体上旋转玩家相机 (javascript/three.js)

c# - Unity - 任意角度之间的夹具旋转

c++ - boost thread_group 无限循环

c++ - 具有特定元素类型的通用可迭代类型

python - 通过直方图匹配比较图像

java - RGB 到 HSV 转换

c++ - C++ 中 const 的这两种用法有什么区别?