使用cv :: Mat的高效C ++四元数乘法 [英] Efficient C++ quaternion multiplication using cv::Mat

查看:221
本文介绍了使用cv :: Mat的高效C ++四元数乘法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to multiply 2 quaternions, which are stored in a cv::Mat structure. I want the function to be as efficient as possible. I have the following code so far:

/** 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最快的方法吗?

Is this the fastest way with OpenCV? Would it be fastest using fixed-point arithmetic?

推荐答案

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

In this tutorial different ways to access different pixels are covered. The Mat::at function was found to be about 10% slower in comparison to direct pixel access, probably due to the extra check in debug mode.

如果你真的关注性能,你应该用文本中提到的3种不同的方法重写你的方法,然后配置文件找到一个在你的情况下最好的方法。

If you are really off for performance, you should rewrite your method with the 3 different methods mentioned in the text and then profile to find the one which is best in your situation.

这篇关于使用cv :: Mat的高效C ++四元数乘法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆