c – 在OpenCv中计算cv :: Mat的外(张量)乘积
有没有办法使用OpenCV中的cv :: Mat数据结构计算外部产品(某些列向量z的z * transpose(z))?
我已经检查了the documentation并且没有内置功能.但是我试图将标准矩阵乘法表达式(*)与cv :: Mat类型的向量一起使用. 这是(伪)代码: cv::Mat tmp = cv::Mat::zeros(9,1,CV_32SC1) cv::Mat outerProduct = tmp * tmp.t(); 外部产品计算提供例外. (是的,我的实际代码中的tmp矩阵中有实际值,但此描述提供了有关所用数据类型的更多信息) 理想情况下,cv :: Mat outerProduct最终应该是9×9矩阵. 我可以使用cv :: Mat的缩放倍增属性来做到这一点(即按列尺寸重复列向量tmp,对于每列,按索引中的值缩放元素 – as in how you may solve this kind of multiplication by hand): cv::Mat outerProduct = cv::repeat(tmp,9); for (int i = 0; i < 9; i++) { outerProduct.col(i) *= tmp.at<int>(i,0); } …但如果有一个更好的方法会很好. 解决方法
请注意,虽然我的答案是正确的,但
@kaanoner’s answer可以提供更好的性能.
他们将这些方法隐藏在你最不期望的地方.这个是在Operations on Arrays年,它被称为mulTransposed. cv::Mat tmp = (Mat_<double>(9,1) << 1,2,3,4,5,6,7,8,9); cv::Mat outerProduct; mulTransposed(tmp,outerProduct,false); 第三个参数是aTa.如果是,则该方法计算aTa.如果它是假的,它会计算aaT. 输出是: tmp = [1; 2; 3; 4; 5; 6; 7; 8; 9] outerProduct = [1,9; 2,10,12,14,16,18; 3,9,15,18,21,24,27; 4,20,28,32,36; 5,25,30,35,40,45; 6,36,42,48,54; 7,49,56,63; 8,64,72; 9,27,45,54,63,72,81] 查看源代码,mulTransposed似乎不支持CV_32S.以下是它们指定的源和目标类型: (stype == CV_8U && dtype == CV_32F) (stype == CV_8U && dtype == CV_64F) (stype == CV_16U && dtype == CV_32F) (stype == CV_16U && dtype == CV_64F) (stype == CV_16S && dtype == CV_32F) (stype == CV_16S && dtype == CV_64F) (stype == CV_32F && dtype == CV_32F) (stype == CV_32F && dtype == CV_64F) (stype == CV_64F && dtype == CV_64F) 这意味着,目标类型始终是浮点类型.即使我指定了CV_16S的dtype,我也得到一个CV_32F的矩阵. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |