请注意,尽管我的答案是正确的,
@kaanoner的答案 提供了更好的性能。
他们会在你最不经意的地方悄悄使用这些方法。这个方法在
数组操作 中,被称为
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
。如果为 true,该方法计算 a
Ta。如果为 false,则计算 aa
T。
输出为:
tmp =
[1; 2; 3; 4; 5; 6; 7; 8; 9]
outerProduct =
[1, 2, 3, 4, 5, 6, 7, 8, 9;
2, 4, 6, 8, 10, 12, 14, 16, 18;
3, 6, 9, 12, 15, 18, 21, 24, 27;
4, 8, 12, 16, 20, 24, 28, 32, 36;
5, 10, 15, 20, 25, 30, 35, 40, 45;
6, 12, 18, 24, 30, 36, 42, 48, 54;
7, 14, 21, 28, 35, 42, 49, 56, 63;
8, 16, 24, 32, 40, 48, 56, 64, 72;
9, 18, 27, 36, 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
的矩阵。