gpt4 book ai didi

c++ - OpenCV:与包含 Vec3d 的矩阵和包含 double 的矩阵的矩阵乘法

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:48:55 24 4
gpt4 key购买 nike

我使用 OpenCV 有一段时间了,现在我到了需要这种类型的乘法的地步:

Define a matrix T, which contains elements of the type Vec3d1 . Matrix T has the size: M X N. Matrix T has to be multiplied with a Vector Phi, which has the size: N X 1, containing doubles as values. Each element of the result has to be the result of a matrix multiplication of both matrices.

我不想做分量乘法,而是“真正的”矩阵乘法,例如将 T2 的第一个元素与矩阵 J 的第一个元素相乘,然后将矩阵 T 的第二个元素相乘>3 与矩阵 J 的第二个元素。这样做,直到完成 T 的第一行,然后对结果求和。结果是 M X 1。

例如,如果 T 是一个 3 X 2 矩阵,Phi 是一个 2 X 1 矩阵,那么计算应该是 T_11 * phi_11 + T_12 * phi_21 为结果的第一个值。目前我正在使用两个很慢的 for 循环:

for (int i = 0; i<M; ++i){
cv::Mat summedResult = cv::Mat(3, 1, CV_64F, double(0));
for (uint32 j = 0; j<N; ++j){
summedResult = summedResult +
(cv::Mat(mMatrixT.at<cv::Vec3d>(i, j)) * mMatrixPhi.at<double>(j));
}
// The result matrix contains values of type Vec3d again
mResultMatrix.at<cv::Vec3d>(i) = cv::Vec3d(summedResult);
}

更一般地说:是否可以在 OpenCV 中有效地乘以包含 Vec3dsdouble 的矩阵?


1。包含 double 的三维 vector 。

2。坐标:1,1

3。坐标:1,2

最佳答案

我仍然不知道你期望什么样的结果,但也许试试这个:

假设您有一个 Vec3d 的 MxN 矩阵和一个 double 类型的 Nx1 矩阵,您的结果将是一个 Vec3d 类型的 Mx1 矩阵:

for (int i = 0; i<M; ++i)
{
cv::Vec3d summedResult; // here this must be a Vec3d instead of a matrix, if I assume the right result expection
for (uint32 j = 0; j<N; ++j)
{
summedResult = summedResult + (mMatrixT.at<cv::Vec3d>(i, j) * mMatrixPhi.at<double>(j));
}
// The result matrix contains values of type Vec3d again
mResultMatrix.at<cv::Vec3d>(i) = summedResult;
}

编辑:

啊抱歉,没有读到你提供的代码有效但速度太慢......好吧,我希望没有优化,因为数学上没有定义。您可以做的是将 Vec3d 垫转换为 Mx(3*N) 矩阵并将 Nx1 垫转换为 (3*N)x1 垫(下一个值之前相同值的 3 倍)并使用 OpenCV 矩阵乘积直接地。但可能这并不快,因为两个矩阵的尺寸都大了 3* ;)

编辑:将是不同的结果,因为每个元素都是 Vec3d 元素的总和...

关于c++ - OpenCV:与包含 Vec3d 的矩阵和包含 double 的矩阵的矩阵乘法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32908212/

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