3x1或1x3 cv :: Mat转换为cv :: Point3d? [英] Conversion of 3x1 or 1x3 cv::Mat to cv::Point3d?
问题描述
我正在处理一些代码,其中要做很多3x3矩阵乘法,还使用旋转矩阵等对3d点进行一些平移.我决定将OpenCV核心功能用于数学运算.使用添加到cv::Mat
类中的最新构造函数将cv::Point3d
直接转换为3x1 cv::Mat
的可能性大大减少并简化了代码.
I'm dealing with some code in which a do a lot of 3x3 matrix multiplications an also some translation of 3d points using rotation matrices, etc. I decided to use OpenCV core functionalities for the mathematical operations. The possibility to use the recent constructor added to the cv::Mat
class to convert a cv::Point3d
directly to a 3x1 cv::Mat
reduces and simplifies the code greatly.
我现在想知道的是,是否有一种简单的方法可以将3x1或1x3 cv::Mat
转换为cv::Point3d
?我总是可以做类似的事情:
What I am wondering now is if there is a simple way to convert a 3x1 or 1x3 cv::Mat
to an cv::Point3d
? I always can do something like:
cv::Mat mat(3,1,CV_64FC1);
cv::Point3d p (mat.at<double>(0,0), mat.at<double>(1,0), mat.at<double>(2,0));
或
cv::Mat mat(3,1,CV_64FC1);
const double *data = mat.ptr<double>(0);
cv::Point3d p (data[0], data[1], data[2]);
我非常担心性能(避免对at
方法的3次调用).
I am very worried about the performance (avoid the 3 calls to at
method).
推荐答案
cv::Point3d
具有允许直接从cv::Mat
创建的构造函数:
cv::Point3d
has a constructor which allows direct creation from cv::Mat
:
cv::Mat mat(3,1,CV_64FC1);
cv::Point3d p(mat);
您可能尚未考虑的另一种可能性是对数学运算使用cv::Matx
而不是cv::Mat
.我发现它更易于使用,并提供更多功能,例如Point
类型的乘法而无需转换:
Another possibility you may not have considered is using cv::Matx
instead of cv::Mat
for your mathematical operations. I find it is easier to use, and offers more functionality, like multiplication of Point
types without needing a conversion:
cv::Point3d p(1,2,3);
cv::Matx33d m = cv::Matx33d::eye();
cv::Point3d p2 = m * p;
cv::Matx
也是静态分配的,而不是动态分配的(例如cv::Mat
),以防您确实需要一点额外的性能.但是,就像所有与性能相关的建议一样:通过进行概要分析,确保要优化的内容实际上是瓶颈.
cv::Matx
is also statically allocated, rather than dynamically (like cv::Mat
), in case you really need that extra little bit of performance. However, as in all performance-related advice: make sure what you're optimizing is actually a bottleneck by profiling.
这篇关于3x1或1x3 cv :: Mat转换为cv :: Point3d?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!